locked
Does Android try to "recover" from a crash? RRS feed

  • Question

  • User394687 posted

    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.

    Tuesday, June 9, 2020 11:45 AM

All replies

  • User382871 posted

    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.

    Wednesday, June 10, 2020 5:21 AM
  • User394687 posted

    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.

    Wednesday, June 10, 2020 11:19 AM
  • User382871 posted

    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.

    Thursday, June 11, 2020 7:59 AM