简体   繁体   中英

Switching between activities

I have a dashboard Activity and a user Activity . On every operation in dashboard I will start user Activity using startActivity( ). If the user pressed back button in user Activity I want to show dashboard Activity again. Please give guidelines to implement this which should also handle when the dashboard Activity is killed due to low memory.

The Android framework will take care of that for you. When you call startActivity() it pushes the new activity on top of the current task's stack (unless you tweak the launch mode or set intent flags)

When the user presses back, it pops the current activity off the stack and returns to the previous activity.

If the framework killed any of the previous activities, they are recreated. If you had any instance data in those activities that you still need, you should override onSaveInstanceState() to store it and onRestoreInstanceState() to recover it (data in system Views that have ids assigned are automatically saved/restored).

Please see Tasks and the Back Stack for details.

The only potentially tricky part is if you jump into the application on the user page via intent, but still want back to go to the dashboard, you will need to be sure to prepare the back stack via a TaskStackBuilder .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM