Forum Xamarin Xamarin.Android

Does Android try to "recover" from a crash?

TojurTojur Member ✭✭
edited June 9 in Xamarin.Android

Hi all,

I am working on a suppose bug that causes the application not to recover properly. I was told that by default Android will try to restart the app when it crashes by relaunching the entry point activity. However just googling around I can't find more about it, better yet the documents tell otherwise.

When an app crashes, Android terminates the app's process and displays a dialog to let the user know that the app has stopped, as shown in figure 1.

From ht_tps://developer.android.com/topic/performance/vitals/crash.

The app doesn't have any custom code written for a recovery process and I was told android itself should be restarting from crash but it fails to do so.

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited June 10

    I was told android itself should be restarting from crash but it fails to do so.

    To make the app restart when crashing, try to customize the Application class to deal with the exception.

    Custom Application class

    public class MyApplication : Application
    {
        public static MyApplication instance;
        public static Activity activity;
        public override void OnCreate()
        {
            base.OnCreate();
            instance = this;
    
            AndroidEnvironment.UnhandledExceptionRaiser += CustomOnUnhandledExceptionRaiser;
        }
    
        private void CustomOnUnhandledExceptionRaiser(object sender, RaiseThrowableEventArgs e)
        {
            var intent = new Intent(activity, typeof(MainActivity));
            intent.PutExtra("crash", true);
            intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.ClearTask | ActivityFlags.NewTask);
    
            var pendingIntent = PendingIntent.GetActivity(MyApplication.instance, 0, intent, PendingIntentFlags.OneShot);
    
            var mgr = (AlarmManager)MyApplication.instance.GetSystemService(Context.AlarmService);
            mgr.Set(AlarmType.Rtc, DateTime.Now.Millisecond + 100, pendingIntent);
    
            activity.Finish();
    
            JavaSystem.Exit(2);
        }
    
        public override Context ApplicationContext => base.ApplicationContext;
    
        public static MyApplication getInstance()
        {
            return instance;
        }
    }
    

    MainActivity class

    public class MainActivity : AppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            ...
            MyApplication.activity = this;
    
            if (Intent.GetBooleanExtra("crash", false))
            {
                Toast.MakeText(this, "App restarted after crash", ToastLength.Short).Show();
            }
        }
        ...
    }
    

    You could google with the keyword as Restarting android application after crash using Xamarin.Android to check more detials.

  • TojurTojur Member ✭✭

    Hi @Jarvan,

    Thanks for the code snippets for restarting the application, however this isn't the issue I am trying to solve. The android itself attempts to restart or recover an application without any requests to do so.

    I have found relevant post on stack overflow giving a highlight to the issue and it still does not have definitive answer.

    ht_tps://stackoverflow.com/questions/7245550/android-app-restarts-automatically-after-a-crash

    Initial thoughts lead me to think that there might be splash activity in back stack that gets brought to foreground when application crashes, however that would go against android crash documentation which states that once application receives a FATAL its entire process is killed.

  • JarvanJarvan Member, Xamarin Team Xamurai

    When an app crashes, Android terminates the app's process and displays a dialog to remind the user that the app has stopped. It'll not restart or recover the app automatically, you need to customize the function.

Sign In or Register to comment.