Return to second activity after clicking home button

戏子无情 提交于 2019-12-08 10:12:15

问题


I've tried all launchMode's yet it does not seem to work:

Suppose App A has various activities, J & K. J is the initial one (to choose app mode), which calls K where the main things happen (after calling it, J finish()es). If i click "Home" button in K, and then open the app again, it opens a new instance of A with activity J.

I'd like it to open the paused K activity instead. Other threads mention an Android bug -- is there a way to fix it? Setting launchMode does not work :S

Thanks a lot.


回答1:


What you want is normal Android behaviour. The fact that it isn't working means that you are probably doing something strange. Do not try to solve this by playing around with launchModes. Please post the relevant parts of your manifest. There is, however, a bug which manifests itself like this. To see if this bug is causing your problem do the following: force-close your app on the phone. Now start your app from the list of apps on the phone. Go from ActivityJ to ActivityK. Press HOME. Open the app again. If it works now, you are just seeing the dreaded Android launch bug.

For more details on the launch bug, see these issues:

http://code.google.com/p/android/issues/detail?id=2373

http://code.google.com/p/android/issues/detail?id=26658

The bug is there on all devices, on all versions of Android (at least up to ICS, haven't tested on JellyBean yet). It all works as it should in the emulator, so you cannot use emulator behaviour as an indication of real device behaviour.




回答2:


Try this

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
    // Activity was brought to front and not created,
    // Thus finishing this will get us to the last viewed activity
    finish();
    return;
}

// Regular activity creation code...
}


来源:https://stackoverflow.com/questions/14059308/return-to-second-activity-after-clicking-home-button

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