Android - 返回调用 Activity

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3277768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:26:20  来源:igfitidea点击:

Android - Return to calling Activity

androidandroid-activity

提问by Ian Vink

Newbie Question from an iPhone developer.

来自 iPhone 开发人员的新手问题。

I have called the startActivity(intent) and the new activity loads. How do I go 'back' to the calling activity once a button is pushed. 'Popping' the activity off the stack basically.

我已经调用了 startActivity(intent) 并加载了新的活动。按下按钮后,如何“返回”调用活动。基本上从堆栈中“弹出”活动。

回答by Tim Kryger

If you had created the new Activity with startActivityyou just need to call finish. If you had spawned the new Activity by calling startActivityForResultsthen you need to call setResultand then finishin order to pass back data to the onActivityResultmethod of the prior Activity.

如果您已经创建了新的 Activity,startActivity您只需要调用finish. 如果您通过调用生成了新的 Activity,startActivityForResults那么您需要调用setResult然后finish将数据传回给onActivityResult先前 Activity的方法。

回答by Andy Zhang

Call finish()on your newly loaded activity. This is assuming you didn't call finish()on your previous activity (in which case you could always restart it).

调用finish()您新加载的活动。这是假设您没有调用finish()之前的活动(在这种情况下,您可以随时重新启动它)。

回答by Vins

Calling finish() closes that activity and gets back to the previous screen(or Activity which called that) , or exits the app if that is the main activity which launched app.

调用 finish() 关闭该活动并返回上一个屏幕(或调用该屏幕的活动),或者如果这是启动应用程序的主要活动,则退出该应用程序。