Finishing (or Accessing) a specific Activity in Android

↘锁芯ラ 提交于 2019-12-02 15:06:53

问题


As Activities are opened by the user, they're stacked up on the view stack. and as the user finishes an Activity by any means, it is popped out of the view stack.

Now, I have a situation where the user has opened the app's home screen, and has successively opened multiple activities, on top of the home screen. In each activity, there's a control which lets the user see the home screen again.

As i can think, there can be two approaches to get this:

  1. On the press of that control, pop the home screen from the bottom of the view stack and push it on the top of it.
  2. As the control is pressed, start popping each of the current screen until the home screen becomes the current screen.

I know there's some way in Android to do at least one of this, or something like this. I just can't remember what was it.

Please help me choose the better approach, and let me know the way (the code, specifically) to do it.

Thanks a lot :)

(Please edit the title/text if it isn't appropriate)


回答1:


try something like this

Intent i = new Intent();
    i.putExtra(EXTRA_KEY_ARTIST, id);
    i.setClass(this, ArtistActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(i);

That's all when you set the the FLAG_ACTIVITY_SINGLE_TOP property it wont start a new activity but will show your activity intially created if its not destroyed yet,

But if your activity is the starting activity then you can put it like this

 <activity
    android:name=".ArtistActivity"
    android:label="Artist"
    android:launchMode="singleTop">
</activity>


来源:https://stackoverflow.com/questions/4323164/finishing-or-accessing-a-specific-activity-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!