Android:返回上一个活动 [英] Android: Go back to previous activity

查看:37
本文介绍了Android:返回上一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在安卓应用上做一些简单的事情.怎么可能回到以前的活动.

I want to do something simple on android app. How is it possible to go back to a previous activity.

我需要什么代码才能返回上一个活动

What code do I need to go back to previous activity

推荐答案

Android Activity 存储在 Activity 堆栈中.回到之前的活动可能意味着两件事.

Android activities are stored in the activity stack. Going back to a previous activity could mean two things.

  1. 您使用 startActivityForResult 从另一个活动打开了新活动.在这种情况下,您只需从代码中调用 finishActivity() 函数,它就会将您带回到之前的活动.

  1. You opened the new activity from another activity with startActivityForResult. In that case you can just call the finishActivity() function from your code and it'll take you back to the previous activity.

跟踪活动堆栈.每当您使用 Intent 启动新 Activity 时,您都可以指定 Intent 标志,例如 FLAG_ACTIVITY_REORDER_TO_FRONTFLAG_ACTIVITY_PREVIOUS_IS_TOP.您可以使用它在应用程序中的活动之间进行随机播放.不过没怎么用过.看看这里的标志:http://developer.android.com/reference/android/content/Intent.html

Keep track of the activity stack. Whenever you start a new activity with an intent you can specify an intent flag like FLAG_ACTIVITY_REORDER_TO_FRONT or FLAG_ACTIVITY_PREVIOUS_IS_TOP. You can use this to shuffle between the activities in your application. Haven't used them much though. Have a look at the flags here: http://developer.android.com/reference/android/content/Intent.html

正如评论中提到的,如果活动是用 startActivity() 打开的,那么你可以用 finish() 关闭它.如果您想使用向上按钮,您可以在 onOptionsSelected(MenuItem item) 方法中捕获该按钮,并根据 android.R.id.home 不同检查项目 IDR.id.home 如评论中所述.

As mentioned in the comments, if the activity is opened with startActivity() then one can close it with finish(). If you wish to use the Up button you can catch that in onOptionsSelected(MenuItem item) method with checking the item ID against android.R.id.home unlike R.id.home as mentioned in the comments.

这篇关于Android:返回上一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆