简体   繁体   中英

How to destroy an activity when switching to another activity via intent?

I have 3 activities A, B and C. Activity A has a button which when clicked takes me to Activity B. Activity B has a button which when clicked takes me to Activity C. When I click on button in Activity A it takes me to Activity B but Activity A is paused and not destroyed and the same happens when I click Activity B's button and go to acitivty C. I tried using finish() on activity B, but the problem is when I press the back button in activity C it goes directly to Activity A. What I want is when I click Activity B's button it should destroy Acitivity B and take me to Acitivity C and in Activity C, if I press back button it should take me to newly created activity B

Activity A -> Activity B-> Activity C

You'll be working against the Android framework. That leads to frustrations. Instead, when you go to activity C, don't finish activity B. Instead, in activity B in onResume(), do something based on the fact that you went to activity C. Or starting Activity C using startActivityForResult() may be appropriate.

Edited

The problem is updating the UI after something changed in activity C. The best place to do that in activity B is in the onResume(). Put the logic for updating the UI in onResume() instead of in onCreate(). When an activity is created, onCreate() is invoked, and onResume() is also invoked.

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