Android 从另一个活动完成一个活动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10379134/
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
Finish an activity from another activity
提问by Kanika
I want to finish one activity from another activity, like:
我想从另一项活动中完成一项活动,例如:
In Activity [A], on button click, I am calling Activity [B] without finishing Activity [A].
在活动 [A] 中,单击按钮时,我在未完成活动 [A] 的情况下调用活动 [B]。
Now in Activity [B], there are two buttons, Newand Modify. When the user clicks on modify then pop an activity [A] from the stack with all the options ticked..
现在在 Activity [B] 中,有两个按钮New和Modify。当用户点击修改然后从堆栈中弹出一个活动 [A],并勾选所有选项。
But when the user click on Newbutton from Activity [B], then I will have to finish Activity [A] from the stack and reload that Activity [A] again into the stack.
但是,当用户单击Activity [B] 中的New按钮时,我将不得不从堆栈中完成 Activity [A] 并将该 Activity [A] 重新加载到堆栈中。
I am trying it, but I am not able to finish Activity [A] from the stack... How can I do it?
我正在尝试,但我无法从堆栈中完成 Activity [A] ......我该怎么做?
I am using the code as:
我将代码用作:
From Activity [A]:
从活动 [A]:
Intent GotoB = new Intent(A.this,B.class);
startActivityForResult(GotoB,1);
Another method in same activity
同一活动中的另一种方法
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 1)
{
if (resultCode == 1) {
Intent i = getIntent();
overridePendingTransition(0, 0);
i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(i);
}
}
}
And in Activity [B], on button click:
在活动 [B] 中,单击按钮:
setResult(1);
finish();
回答by MKJParekh
Make your activity A in manifest file:
launchMode = "singleInstance"
When the user clicks new, do
FirstActivity.fa.finish();
and call the new Intent.When the user clicks modify, call the new Intent or simply finish activity B.
在清单文件中创建您的活动 A:
launchMode = "singleInstance"
当用户点击 new 时,执行
FirstActivity.fa.finish();
并调用新的 Intent。当用户点击修改时,调用新的 Intent 或简单地完成活动 B。
FIRST WAY
第一种方式
In your first activity, declare one Activity object
like this,
在你的第一个活动中,声明一个Activity object
这样的,
public static Activity fa;
onCreate()
{
fa = this;
}
now use that object in another Activity
to finish first-activity like this,
现在在另一个对象中使用该对象Activity
来完成这样的第一个活动,
onCreate()
{
FirstActivity.fa.finish();
}
SECOND WAY
第二种方式
While calling your activity FirstActivity
which you want to finish as soon as you move on,
You can add flag while calling FirstActivity
在调用您FirstActivity
想在继续后立即完成的活动时,您可以在调用时添加标志FirstActivity
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
But using this flag the activity will get finished evenif you want it not to. and sometime onBack
if you want to show the FirstActivity
you will have to call it using intent.
但是使用此标志,即使您不希望活动完成,活动也会完成。有时onBack
如果你想显示FirstActivity
你将不得不使用意图调用它。
回答by Bharat Sharma
That you can do, but I think you should not break the normal flow of activity. If you want to finish you activity then you can simply send a broadcast from your activity B to activity A.
你可以这样做,但我认为你不应该破坏正常的活动流程。如果你想完成你的活动,那么你可以简单地从你的活动 B 向活动 A 发送广播。
Create a broadcast receiver before starting your activity B:
在开始您的活动 B 之前创建一个广播接收器:
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent intent) {
String action = intent.getAction();
if (action.equals("finish_activity")) {
finish();
// DO WHATEVER YOU WANT.
}
}
};
registerReceiver(broadcastReceiver, new IntentFilter("finish_activity"));
Send broadcast from activity B to activity A when you want to finish activity A from B
当您想从 B 完成活动 A 时,从活动 B 向活动 A 发送广播
Intent intent = new Intent("finish_activity");
sendBroadcast(intent);
I hope it will work for you...
我希望它对你有用...
回答by Femi
This is a fairly standard communication question. One approach would be to use a ResultReceiver in Activity A:
这是一个相当标准的沟通问题。一种方法是在活动 A 中使用 ResultReceiver:
Intent GotoB=new Intent(A.this,B.class);
GotoB.putExtra("finisher", new ResultReceiver(null) {
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
A.this.finish();
}
});
startActivityForResult(GotoB,1);
and then in Activity B you can just finish it on demand like so:
然后在活动 B 中,您可以像这样按需完成它:
((ResultReceiver)getIntent().getExtra("finisher")).send(1, new Bundle());
Try something like that.
尝试这样的事情。
回答by Dharmendra
There is one approach that you can use in your case.
您可以在您的情况下使用一种方法。
Step1: Start Activity B from Activity A
Step1:从活动A开始活动B
startActivity(new Intent(A.this, B.class));
Step2: If the user clicks on modify button start Activity A using the FLAG_ACTIVITY_CLEAR_TOP
.Also, pass the flag in extra.
Step2:如果用户点击修改按钮使用FLAG_ACTIVITY_CLEAR_TOP
.Also启动Activity A ,在extra 中传递标志。
Intent i = new Intent(B.this, A.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("flag", "modify");
startActivity(i);
finish();
Step3: If the user clicks on Add button start Activity A using the FLAG_ACTIVITY_CLEAR_TOP
.Also, pass the flag in extra. FLAG_ACTIVITY_CLEAR_TOP
will clear all the opened activities up to the target and restart if no launch mode is defined in the target activity
第三步:如果用户点击添加按钮,使用FLAG_ACTIVITY_CLEAR_TOP
.Also启动 Activity A ,额外传递标志。FLAG_ACTIVITY_CLEAR_TOP
如果目标活动中没有定义启动模式,将清除所有打开的活动直到目标并重新启动
Intent i = new Intent(B.this, A.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("flag", "add");
startActivity(i);
finish();
Step4: Now onCreate()
method of the Activity A, need to retrieve that flag.
Step4:现在onCreate()
Activity A的方法,需要检索那个标志。
String flag = getIntent().getStringExtra("flag");
if(flag.equals("add")) {
//Write a code for add
}else {
//Write a code for modifying
}
回答by yaniv maymon
Start your activity with request code :
使用请求代码开始您的活动:
StartActivityForResult(intent,1234);
And you can close it from any other activity like this :
您可以从任何其他活动中关闭它,如下所示:
finishActivity(1234);
回答by DArkO
See my answer to Stack Overflow question Finish All previous activities.
请参阅我对 Stack Overflow 问题的回答完成所有以前的活动。
What you need is to add the Intent.FLAG_CLEAR_TOP
. This flag makes sure that all activities above the targeted activity in the stack are finished and that one is shown.
您需要的是添加Intent.FLAG_CLEAR_TOP
. 此标志确保堆栈中目标活动上方的所有活动都已完成并显示该活动。
Another thing that you need is the SINGLE_TOP
flag. With this one you prevent Android from creating a new activity if there is one already created in the stack.
您需要的另一件事是SINGLE_TOP
标志。有了这个,如果堆栈中已经创建了一个新的 Activity,你就可以阻止 Android 创建一个新的 Activity。
Just be wary that if the activity was already created, the intent with these flags will be delivered in the method called onNewIntent(intent)
(you need to overload it to handle it) in the target activity.
请注意,如果 Activity 已经创建,带有这些标志的 Intent 将onNewIntent(intent)
在目标 Activity 中调用的方法中传递(您需要重载它来处理它)。
Then in onNewIntent
you have a method called restart or something that will call finish()
and launch a new intent toward itself, or have a repopulate()
method that will set the new data. I prefer the second approach, it is less expensive and you can always extract the
onCreate
logic into a separate method that you can call for populate.
然后在onNewIntent
你有一个叫做 restart 的方法或一些会调用finish()
并启动一个新意图的repopulate()
方法,或者有一个方法来设置新数据。我更喜欢第二种方法,它成本较低,而且您始终可以将onCreate
逻辑提取
到一个单独的方法中,您可以调用该方法来进行填充。
回答by koras
I've just applied Nepster's solution and works like a charm. There is a minor modification to run it from a Fragment.
我刚刚应用了 Nepster 的解决方案,并且效果很好。从 Fragment 运行它有一个小的修改。
To your Fragment
给你的片段
// sending intent to onNewIntent() of MainActivity
Intent intent = new Intent(getActivity(), MainActivity.class);
intent.putExtra("transparent_nav_changed", true);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
And to your OnNewIntent() of the Activity you would like to restart.
以及您想要重新启动的 Activity 的 OnNewIntent()。
// recreate activity when transparent_nav was just changed
if (getIntent().getBooleanExtra("transparent_nav_changed", false)) {
finish(); // finish and create a new Instance
Intent restarter = new Intent(MainActivity.this, MainActivity.class);
startActivity(restarter);
}
回答by Jawad Zeb
I think i have the easiest approach... on pressing new in B..
我认为我有最简单的方法......在 B 中按下 new ......
Intent intent = new Intent(B.this, A.class);
intent.putExtra("NewClicked", true);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
and in A get it
并在 A 得到它
if (getIntent().getBooleanExtra("NewClicked", false)) {
finish();// finish yourself and make a create a new Instance of yours.
Intent intent = new Intent(A.this,A.class);
startActivity(intent);
}
回答by Tony Kutzler
I know this is an old question, a few of these methods didn't work for me so for anyone looking in the future or having my troubles this worked for me
我知道这是一个古老的问题,其中一些方法对我不起作用,因此对于将来寻找或遇到麻烦的人来说,这对我有用
I overrode onPause
and called finish()
inside that method.
我覆盖onPause
并调用了finish()
该方法。
回答by Dharmik Ghori
First call startactivity()
then use finish()
首先调用startactivity()
然后使用finish()