开发者

Destroying the Activity stack

I'm building an app that requires a user to have logged in and obtained a user id. This id is a member variable of my extended Application class.

I'd like the application to recognize when/if this variable gets wiped out and, if so, throw up my log in screen and destroy the Activity stack behind it.

Here's what I think I'm going to do:

Extend Activity and ListActivity (currently the only two types of Activities I am using). In the onResume() handler, check to see if the user id is set. If not, throw up the log in screen and destroy the rest of the Activity stack behind it. All of my Activities will derive from these new extended Activities.

My issue here is that I don't know how to destroy the Activity stack behind a particular Activity. Any ideas?

One idea is to have a broadcast receive listening for a particular message that tells Activities to kill themselves, b开发者_StackOverflowut I'm hoping Android already has something in place for a situation like this.

Update:

Also, is there a way to clear the entire Activity stack? I'd like to override the onBackPressed() handler on an Activity and have the Activity stack blown out so the user gets taken back to the Android home screen.


Other choice will be using the noHistory parameter in the AndroidManifest.xml to accomplish this. so your Activity should not be placed in the history stack

<activity android:name=".MyActivity"
          android:noHistory="true" />


I agree with danh32, setting the FLAG_ACTIVITY_CLEAR_TOP on the intent to bring the Login Activity on top of the stack will destroy all activities behind the new activity (hence, the Login Activity). This will leave one activity on the stack.

Intent myIntent = new Intent(view.getContext(), DisplayMenu.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);
finish();

I use this option to allow users to get back to a main screen from a long path through a lot of activities.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜