[英]How to finish parent activity from child activity
I am new to Android development. 我是Android开发的新手。
I have created a main Activity (->A), which has 4 buttons. 我创建了一个主Activity( - > A),它有4个按钮。 One of the 4 buttons is the EXIT-button.
其中一个按钮是EXIT按钮。
I start another activity (->B), on click of the EXIT-button. 我点击退出按钮开始另一个活动( - > B)。 This opens 'B'Activity via an intent from 'A'Activity.
这通过'A'Activity的意图打开'B'Activity。
Activity 'B' contains - Do you want to exit? 活动'B'包含 - 你要退出吗? Yes-Button & No-Button.
是 - 按钮和无按钮。
If I give finish(), onclick of the button - it exits the 'B'Activity. 如果我给完成(),按下按钮 - 它会退出'B'Activity。 I want to finish 'B' & 'A'.
我想完成'B'和'A'。
I have even tried A.finish() -> this doesn't get recognized and results in syntax/semantic error. 我甚至尝试过A.finish() - >这不会被识别并导致语法/语义错误。
I would appreciate help here. 我很感激这里的帮助。
PS : I am using Android-2.2 version phone, and I do not like to use ActivityManager to resolve this. PS:我使用的是Android-2.2版手机,我不喜欢用ActivityManager来解决这个问题。
Try to launch child activity with 尝试启动子活动
startActivityForResult(intent, REQUEST_EXIT);
In child activity 在儿童活动中
case R.id.quit:
setResult(RESULT_OK, null);
finish();
In parent activity 在父母活动中
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_EXIT) {
if (resultCode == RESULT_OK) {
this.finish();
}
}
}
在活动A中使用startActivityForResult()
获取开始活动B,使用onActivityResult()
在A中finish()
活动A.在活动B上按下按钮刚刚finish()
活动B.
You can finish parent activity from child activity like... 您可以通过子活动完成父活动,例如......
In Parent.... 在父母......
startActivityForResult(new Intent(Parent.this, Child.class), ACTIVITY_CONSTANT);
And override the OnActivityForResult(...) method in Parent . 并覆盖Parent中的OnActivityForResult(...)方法。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == ACTIVITY_CONSTANT)
{
finish();
}
}
When you call finish() on child activity, it finishes parent also. 当您在子活动上调用finish()时,它也会完成父级。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.