Android App Restarts upon Crash/force close

前端 未结 7 987
名媛妹妹
名媛妹妹 2020-12-07 10:10

My android app is getting restarted after force close, through my entire application which consist of 20 activities, I am relying on static data created on a main activity.

7条回答
  •  一个人的身影
    2020-12-07 10:25

    Well, an application is not only interface(activities). Imagine you have some complex enterprise application, using SQL transactions, security, Web Authentication, etc.. It is almost impossible to make each activity able to recover the whole app context using only Shared Preferences. So in this case, I use this piece of Code:

    public class MyApplication extends Application {
      private static final String TAG = "my.app";   
      public static final String MainActivityName = "my.app.top.activity";
    
      @Override
      public void onCreate() {
    
        try{
            ActivityManager am = (ActivityManager) this .getSystemService(ACTIVITY_SERVICE);
            List taskInfo = am.getRunningTasks(1);
            ComponentName componentInfo = taskInfo.get(0).topActivity;
            if (MainActivityName.length()>0 && !componentInfo.getClassName().equals(MainActivityName)){
                Log.d(TAG, "Partial Restart Not Supported! : " +componentInfo.getClassName());
                android.os.Process.killProcess(android.os.Process.myPid());
                System.exit(0);
                return;
            }else
                Log.d(TAG, "!!! BCSApplication topActivity=" +componentInfo.getClassName());
        }catch(Exception eee){}
    
        super.onCreate();
        /*
        ....
        */
     }
    /*
        ....
    */
    }
    

提交回复
热议问题