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.
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();
/*
....
*/
}
/*
....
*/
}