区分从主屏幕启动的 Activity 或从应用程序启动的另一个 Activity
我需要知道一种通用方法来区分来自启动器的活动调用和来自应用程序内部另一个活动的调用,或者活动堆栈上的 BACK
有人吗?这已经困扰我很长一段时间了,我需要让它休息......
提前致谢 JQ科雷亚
I need to know a generic way to distinguish between a call of activity from launcher and a call from another activity from inside my app, or a BACK on the activity stack
Anyone? this is bugging me for quite a while now and i need to put it to rest...
Thanks in advance
JQCorreia
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在 Activity 的 onCreate 中,调用
getIntent()
。如果 Activity 从启动器(主屏幕)启动,则getAction()
的值将为android.intent.action.MAIN
和getCategories()< /code> 将返回一个包含 android.intent.category.LAUNCHER 类别的集合。
如果活动是从其他地方启动的,这些值可能为
null
。In the onCreate of your Activity, call
getIntent()
. If the Activity is started from the launcher (home screen) the values forgetAction()
will beandroid.intent.action.MAIN
and thegetCategories()
will return a set which will contain the android.intent.category.LAUNCHER category.If the activity is started from elsewhere these values may be
null
.除了 @advantej 的答案之外,您还可以将每个启动调用扩展到该活动,为启动意图添加额外的内容(例如
intent.putExtra("caller", this.getClass().getSimpleName()
);在活动的 onCreate 方法中,您可以检查@advantej 的建议。
如果启动器不是主屏幕图标,您可以进一步检查是否有
intent.hasExtra("caller" ")
返回 true,如果是,它是什么。In addition to @advantej's answer, you can extend each start-call to that activity adding an extra to the starting intent (e.g.
intent.putExtra("caller", this.getClass().getSimpleName()
);In the activity's
onCreate
method you can check then what @advantej suggests.If the initiator is not the home-screen icon, than you can check further if the
intent.hasExtra("caller")
returns true, and if so, what is it.您可以从意图标志中找到它。
步骤 1:
步骤 2:
You can find it out from intent flag.
step 1:
step 2:
在 2 种情况下 onRestart();称为,1.当活动来自后台时,2.当用户通过后退按钮到达活动时,示例解决方案:
使用 onBackPressed() 函数设置一个标志..所以你知道 onRestart 被调用是因为按下后退按钮...
在 onRestart() 中检查标志并重置它......
in 2 cases the onRestart(); called, 1.when activity come from background, 2.when the user reach the activity by back button then sample solution:
use onBackPressed() function to set a flag.. so u know that onRestart called becouse of back button press...
in onRestart () check the flag and reset it and....
根据 advantej 的回答,这里是一个完整的示例,如果 Activity 是从启动器图标启动的,它也会隐藏 UP 按钮:
Based on advantej's answer, here is a full example that also hides the UP button if the activity was launched from a launcher icon:
这是方便的方法,因此您无需自己编写:
Here's convenience method so you don't need to write it yourself:
我能想到的最简单的方法是在从您自己的活动启动活动时传递一个标志。您还应该检查活动是否已创建或恢复,这可以通过在 onCreate 方法中设置布尔值,然后在 onResume 上检查它来完成。
下面是您可以使用的代码(未测试):
您要检查的 Activity(例如 MainActivity.class):
从其他 Activity 调用 MainActivity 时,请使用以下代码:
The simplest approach that I can think of would be to pass a flag while launching the activity from your own activities. You should also check if the activity was created or resumed, this can be done by setting a boolean in the onCreate method, and then checking it onResume.
Below is the code you can use (not tested):
Activity in which you want to check (say MainActivity.class):
When calling MainActivity from other activities, use the code below: