开发者

Make new activity take the place of the parent in the stack

开发者 https://www.devze.com 2023-01-28 05:44 出处:网络
I have read about activities, stacks and launch modes, but have a hard time un开发者_运维知识库derstanding how to apply this info to my specific problem.

I have read about activities, stacks and launch modes, but have a hard time un开发者_运维知识库derstanding how to apply this info to my specific problem.

Basically what I want to do is to launch a new activity, and make sure that it takes the parent's place in the stack, rather than being placed on top of it in the stack. I have the following two scenarios:

  1. I have a login-activity. When the user logs in, a new activity is launched. When the user hits "back", I don't want him to be sent back to the login-activity, but rather to exit the program.

  2. I have an activity that is displayed in a tabhost (or rather in a framelayout inside a tabhost, I suppose). This activity has a button, an clicking on this launches a new activity. I would like this new activity to take the parents place. That is, i don't want to open a new full-screen activity, but instead I want it to take the parent's place inside the framelayout in the tabhost. Also, I don't want a press on the back-key to lead the user back to the parent activity.

I would appreciate it if anyone could point me in the right direction. Thanks in advance.


  1. All you need to do is call finish() in your Login Activity. That will remove it from the Activity Stack.

  2. This is a more difficult answer. My best suggestion here would be to either use a ViewFlipper and instead of starting a new Activity you can just manage switching the views out. Alternately, if you need the button to start a new Activity, you could use an ActivityGroup and manage the Activitys that way. They both have their own complexities, so you'll need to investigate which may work better for you.


In the first case, you need to call the finish() in your login activity. Also check this manifest settings to finish the activity when a new task is launched. http://developer.android.com/guide/topics/manifest/activity-element.html#finish

In the second case you may use two views as frames or a ViewFlipper. Then overwrite the keyevent to handle the back button click. http://developer.android.com/reference/android/view/View.html#onKeyDown(int,android.view.KeyEvent) Check if the user clicked when they are in the second view and show your first view. And if they are in the first view you may want to return false on the method that you overwrite to let the system to handle the event. [Probably close that activity]

Alternatively for the second option, it might be possible in some cases to just remove tabs and use the options menu. This way you can use your activities in a normal way.

0

精彩评论

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