I want my app to recover from task killer actions or cache clearing. Scenario is below;

  1. start app
  2. press home button
  3. kill all apps(or clear cache)
  4. start app from recent task

After this scenario the app will crash. I observed that if an application killed via task killer apps or by clearing cache, app doesn't get properly killed. Some of it's memory gets deleted but the last state is preserved in the recent tasks. So when user tries to open app from recent tasks, app crashes due to loss of some global or static variables.

Annoying thing about this situation; onDestroy() method doesn't called after task killer action or cache clearing. (my app has one main FragmentActivity and other fragments is controlled by this main activity, so if I can detect onDestroy() of my activity I can kill it properly)

Any ideas about how to solve this problem? Thanks in advance.

有帮助吗?

解决方案

I solve this issues by using life cycle differences.

if home button touched onStop() is called, but if app is closed both onStop() and onDestroy() get called which means if task is killed when it's in background onDestroy() isn't called. I keep track of this scenario via sharedPreferences.

@Override
protected void onStop() {
    SharedPreferencesUtilities.putBoolean("isFromOnStop", true);
    super.onStop();
}

@Override
protected void onDestroy() {
    SharedPreferencesUtilities.putBoolean("isFromOnStop", false);
    super.onDestroy();
    android.os.Process.killProcess(android.os.Process.myPid());
}  

Then in onCreate() i checked this sharedPreferences boolean ;

if(SharedPreferencesUtilities.getBoolean("isFromOnStop", false)) {
    SharedPreferencesUtilities.putBoolean("isFromOnStop", false);
        ((MainFragmentActivity)mContext).finish();
        Toast.makeText(mContext, "Ooops app was killed in background", Toast.LENGTH_SHORT).show();
}

其他提示

The problem is the existence of task killer: they are useless, IMHO. See this page for more info.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top