开发者

How do I 'navigate' to an activity that's already running?

开发者 https://www.devze.com 2023-02-19 21:34 出处:网络
Newbie question... So I have two activities, Cherry and Apple, and each one has a button on it to go to the other one. So back and forth.

Newbie question... So I have two activities, Cherry and Apple, and each one has a button on it to go to the other one. So back and forth.

In class "Cherry" I say this:

intent = new Intent(Cherry.this, Apple.class)
startActivity(intent);

Meaning that it should go to the Apple. There's similar code in th开发者_开发技巧e Apple activity.

What I think I am seeing is, is that each time when I startActivity Apple for example, it's starting a new instance of it instead of just reactivating Apple. I've scoured the doc and can't find the flag or other call that would do what I want.

Any tips would be appreciated!

-- Pito


What about FLAG_ACTIVITY_REORDER_TO_FRONT?

FLAG_ACTIVITY_CLEAR_TOP is also quite useful, finishing activities on the back stack until the target activity is reached.

To be clear, your activity may be restarted even if using the flags above. This would happen if your activity were destroyed in an attempt to free memory. In other words, you still need to make sure your activity can handle being restarted, taking the proper precautions in onPause and onSaveInstanceState.


Intent.FLAG_ACTIVITY_REORDER_TO_FRONT will bring the launched activity back to front if it's already running as given here. You will have to handle the back button yourself so as not to finish the current activity.

0

精彩评论

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

关注公众号