开发者

How to finish() every Activity on the stack associated with the Application?

开发者 https://www.devze.com 2023-03-03 16:00 出处:网络
Is there any way to tell Android, \"If the user exits this Activity by hitting the \'home\' key, immediately finish() every Activity on the stack that has ever been associated with this Application?\"

Is there any way to tell Android, "If the user exits this Activity by hitting the 'home' key, immediately finish() every Activity on the stack that has ever been associated with this Application?" Or, alternatively, a way for an Activity whose onPause() method has been forcibly called by a user hitting '开发者_开发知识库home' to finish() not only itself, but the Activity that started it as well?

The problem I have is workflow. My application's users basically go back and forth between a main Activity and "detail" Activity windows. They see something in the main Activity, tap it, the child detail Activity gets spawned, they use it for a while, then hit back to return to the main Activity. I can't finish() the main Activity after starting the child Activity, because then the back button would quit working. The problem is that now, if the user exits by hitting the home key in a child Activity, it leaves behind a zombie parent Activity. If the user relaunches the application, uses it for a while, then explicitly triggers a shutdown by hitting menu->quit from the main Activity (where I kill the service, then call finish()), the Activity goes away, and the user gets dumped into the zombie Activity from the previous run (which promptly crashes if the user tries to do anything, because the background service it depends on has already been shut down, and all the BroadcastReceivers that would have otherwise been listening have been removed, so its cries for help go unheard).

I'm actually kind of puzzled, because I already have android:launchMode="singleTask" sitting in the manifest tag for my launch activity, and I thought launchMode="singleTask" was explicitly supposed to prevent things like this from happening by making sure that any lingering Activities from a previous run were safely dead before it relaunched.


The easiest way of doing this is:

Intent i =new Intent(CurrentClass.this,HomeClass.class);

i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

startActivity(i);


Perhaps you can mark all the Activities in the stack with clearTaskOnLaunch (see http://developer.android.com/guide/topics/manifest/activity-element.html#clear).You can mark all the Activities that would be in the task with the attribute.

0

精彩评论

暂无评论...
验证码 换一张
取 消