prevent task manager from recreating last displayed activity in a task

北战南征 提交于 2019-12-10 17:25:25

问题


The scenario is:

launch app, start activity A, navigate to B, then to C press home button. C gets destroyed. Bring up task manager, press on my app's icon. Activity C is being recreated. How can I make this behaviour cease ? What I would like would be to go back to activity A.

The closest I could get to this was by launching activities B,C with Intent.FLAG_ACTIVITY_NO_HISTORY. But the problem with this flag is that it prevents activities B,C from ever being re-created throughout the app's life (they should be creatable once task manager leads user to A).

EDIT: same behaviour observed as in Intent.FLAG_ACTIVITY_NO_HISTORY case if I use this approach instead:

class C extends Activity
{
...
    @Override
    protected void onDestroy() 
    {
        // last resort
        finish();
        super.onDestroy();      
                //finish();
    }
}

I can not navigate back to activity C once it its onDestroy() has been called once :(

Thanks


回答1:


After a weekend of man vs machine, this is what I have experienced:

The noHistory ="true" activity attribute prevented them from being launched (startActivity()) from another activity that stood below them in the activity stack. Thing was that whole the activity navigation logic passed through a method adding this flag to the intent:

context.startActivity(intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));

Android can not "reorder to front" a 'noHistory' activity, and I take it as a bug. Since the noHistory activity has already been finish()'ed, it should have summoned a new instance of that... What has worked was to use FLAG_ACTIVITY_CLEAR_TOP on activity A (see OP). This way, B and C got instantiated properly in all scenarios. However, this was not enough, I ended up finish()'ing all the activities as I left them because of OOM errors on the one hand, and lifecycle / app logic complications on the other.




回答2:


Edit your Manifest.xml file and specify in the activity you don't want to get recreated this:

android:launchMode="singleTask"

Or based on your requirement use other attributes. Go here for detailed explaination.



来源:https://stackoverflow.com/questions/12548853/prevent-task-manager-from-recreating-last-displayed-activity-in-a-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!