finish() activity twice in android?

假装没事ソ 提交于 2019-12-10 18:45:44

问题


Okay say your using a app, and you opened a new activity and then opened another, you can end the activity your on by using finish(); and your back one activity, but how can you go back two activities, all the way back to the first one? I know you could use:

Intent savedGameIntent = new Intent(v.getContext(), firstclass.class);
v.getContext().startActivity(savedGameIntent);

But is that the best way to do it?


回答1:


Use the flag Intent.FLAG_ACTIVITY_CLEAR_TOP.

  Intent intent = new Intent(this,A.class);
  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  startActivity(intent);

From the documentation:

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

So effectively, if you have A -> B -> C, and you intent to A with that flag set, B and C will close.




回答2:


I believe that will start a new activity, not back up to the original one. It sounds like you want to finish the last and middle activities. If you start the last activity with startActivityForResult, you can then override onActivityResult in the middle activity, and call finish() from there.



来源:https://stackoverflow.com/questions/10808223/finish-activity-twice-in-android

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