[英]Bring activity front if it is running or create new one with backpress
Let me explain detailed;
让我详细解释一下; I have notification and this notification opens
B activity
with two cases. 我有通知,此通知会打开两个案例的
B activity
。
Cases : 案例:
Case-1 情况1
I click to the notification and it opens the B activity
with case-1
. 我点击通知,它用
case-1
打开B activity
。 When i press back
i want to go to the A activity
and kill B activity
. 当我
press back
我想要去A activity
并杀死B activity
。 I dont need B activity
anymore. 我不再需要
B activity
了。 Everything easy from here without using flags
. 一切都很简单,不用
flags
。 When I'm on B activity
and press back two times from here, it goes A activity
and then closes the app
. 当我在
B activity
并从这里按两次后,它会进入A activity
,然后关闭app
。 My trouble here is, if i open the app
from navigation buttons of phone (can't remember the name of this button) app is opening from B activity
. 我的麻烦在于,如果我从手机的导航按钮打开
app
(不记得这个按钮的名称)应用程序是从B activity
打开的。 That's not what i expected. 那不是我的预期。 I want to open
A activity
. 我想打开
A activity
。 Don't want to see B activity
anymore. 不想再看
B activity
了。
Case-2 案例2
I click to the notification and it opens the B activity
with case-2
.When i press back
i want to bring A activity
to the front, without creating anything new. 我点击通知,它用
case-2
打开B activity
。当我press back
我想把A activity
带到前面,而不创建任何新的东西。 If i press back on B activity
, two times and close the app and then again re-open app from navigation button of phone, want to open app from A activity
. 如果我按回
B activity
,两次并关闭应用程序,然后再次从手机的导航按钮重新打开应用程序,想要从A activity
打开应用程序。
So how can i make this correctly, i tried to use flags
(i already read docs) but couldn't get work. 那么我怎样才能正确地做到这一点,我试图使用
flags
(我已经阅读过文档)但是无法正常工作。 What flags
should i use when i open the B activity
and onBackPress
method of B activity
to go A activity
as i wanted 什么
flags
,当我打开我应该使用B activity
和onBackPress
的方法B activity
去A activity
,我想
This should be achievable by adding 这应该可以通过添加来实现
android:launchMode="singleTask"
to the A activity in the Manifest, then you can just open A activity from B activity onBackPressed and you will have A only once in the stack. 对于Manifest中的A活动,那么你可以在B活动onBackPressed上打开一个活动,你将在堆栈中只有一次A.
If it's not working the way you want, you can create an abstract class that extends Activity and handle the stack in a static object, then A & B must extend this new class 如果它不按你想要的方式工作,你可以创建一个扩展Activity并在静态对象中处理堆栈的抽象类,然后A&B必须扩展这个新类
try this 试试这个
Intent intent = new Intent(context, YourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
试试这个
android:launchMode="singleTask" in android manifest file
You can achive this by adding FLAG_ACTIVITY_REORDER_TO_FRONT 您可以通过添加FLAG_ACTIVITY_REORDER_TO_FRONT来实现此目的
Intent i = new Intent(context, Activity.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
You May try this isTaskRoot() Which will return B is root 你可以试试这个isTaskRoot()这将返回B是root
if it is true then launch A 如果确实如此,则启动A.
other wise you may finish B 另外,你可以完成B
B Activity B活动
@Override
public void onBackPressed() {
if (isTaskRoot()) {
//call A which is not exist
Intent i =new Intent(B.this,A.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}else {
//Finish B if A Already Exixt
finish();
}
}
You can call B Activity on Notification Click 您可以在通知单击上调用B活动
if A is present then u can finish B else you can launch A 如果A存在,那么你可以完成B,否则你可以启动A.
If set FLAG_ACTIVITY_CLEAR_TOP, 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.
如果设置了FLAG_ACTIVITY_CLEAR_TOP,则正在启动的活动已经在当前任务中运行,那么将不会启动该活动的新实例,而是关闭其上的所有其他活动,并将此Intent传递给(现在开启) top)旧活动作为新的意图。
Just put this in ActivityB onBackPressed: 把它放在ActivityB onBackPressed中:
Intent i = new Intent(ActivityB.this , ActivityA.calss);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
finish();
finish();
on ActivityB BackPressed method make ActivityB finish after open ActivityA. 在ActivityB BackPressed方法上,在ActivityA打开后使ActivityB完成。 So after opens ActivtyA, ActivityB will shut down.
因此,在打开ActivtyA后,ActivityB将关闭。
With this combination flag, It will do what you want. 有了这个组合标志,它会做你想要的。 It will close all activities in stack and just keep destination activity.
它将关闭堆栈中的所有活动,并保持目标活动。 If instance of activity exist it will use it and calls
OnNewInstance
and if not it will creates new one. 如果存在活动实例,它将使用它并调用
OnNewInstance
,如果不存在,它将创建新的活动。
If this is the only instance of Activity B being used, you can add the flag noHistory to the manifest for Activity B 如果这是使用的活动B的唯一实例,则可以将标志noHistory添加到活动B的清单中
android:noHistory="true"
This will stop Activity B being added to the back stack, this is also possible dynamically by using the Intent Flag FLAG_ACTIVITY_NO_HISTORY when calling Activity B. 这将停止将活动B添加到后台堆栈,这也可以通过在调用活动B时使用Intent Flag FLAG_ACTIVITY_NO_HISTORY来动态实现。
As for having Activity A start when Activity B is killed @Quentin Menini's answer of having a single task activity set in the manifest will work if that is the only way you wish Activity A to be accessed, or the Intent Flag Intent.FLAG_ACTIVITY_REORDER_TO_FRONT as @Naimish Vinchhi has suggested, will have the desired effect in this instance. 至于在活动B被杀死时启动活动A @Quentin Menini的答案是,如果您希望访问活动A,或者意图标记Intent.FLAG_ACTIVITY_REORDER_TO_FRONT为@,那么在清单中设置单个任务活动将会起作用。 Naimish Vinchhi建议,在这种情况下会产生预期的效果。
https://developer.android.com/reference/android/content/Intent.html link to see all possible Intent flags https://developer.android.com/reference/android/content/Intent.html链接以查看所有可能的Intent标志
https://developer.android.com/guide/topics/manifest/activity-element.html link for all possible manifest activity options 所有可能的清单活动选项的https://developer.android.com/guide/topics/manifest/activity-element.html链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.