从另一个活动完成一个活动 [英] Finish an activity from another activity

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

问题描述

我想从另一项活动中完成一项活动,例如:

I want to finish one activity from another activity, like:

在活动 [A] 中,单击按钮时,我在未完成活动 [A] 的情况下调用了活动 [B].

In Activity [A], on button click, I am calling Activity [B] without finishing Activity [A].

现在在活动 [B] 中有两个按钮,新建修改.当用户点击修改然后从堆栈中弹出一个活动 [A] 并勾选所有选项..

Now in Activity [B], there are two buttons, New and Modify. When the user clicks on modify then pop an activity [A] from the stack with all the options ticked..

但是当用户点击 Activity [B] 中的 New 按钮时,我将不得不从堆栈中完成 Activity [A] 并将该 Activity [A] 重新加载到堆栈中.

But when the user click on New button from Activity [B], then I will have to finish Activity [A] from the stack and reload that Activity [A] again into the stack.

我正在尝试,但我无法完成堆栈中的活动 [A]...我该怎么做?

I am trying it, but I am not able to finish Activity [A] from the stack... How can I do it?

我使用的代码是:

来自活动 [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);
        }
    }
}

在活动 [B] 中,点击按钮:

And in Activity [B], on button click:

setResult(1);
finish();

推荐答案

  1. 在清单文件中创建您的活动 A:launchMode = "singleInstance"

当用户点击new时,执行FirstActivity.fa.finish();并调用新的Intent.

When the user clicks new, do FirstActivity.fa.finish(); and call the new Intent.

当用户点击修改时,调用新的 Intent 或简单地完成活动 B.

When the user clicks modify, call the new Intent or simply finish activity B.

第一种方式

在你的第一个活动中,像这样声明一个Activity对象

In your first activity, declare one Activity object like this,

public static Activity fa;
onCreate()
{
    fa = this;
}

现在在另一个 Activity 中使用该对象来完成像这样的第一个活动,

now use that object in another Activity to finish first-activity like this,

onCreate()
{
    FirstActivity.fa.finish();
}

第二种方式

在调用您希望在继续前进时立即完成的活动 FirstActivity 时,您可以在调用 FirstActivity

While calling your activity FirstActivity which you want to finish as soon as you move on, You can add flag while calling FirstActivity

intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

但是使用此标志,即使您不希望活动完成,活动也会完成.有时 onBack 如果你想显示 FirstActivity 你将不得不使用意图调用它.

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.

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

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