当活性clearTop完成的setResult不工作活性、工作、clearTop、setResult

2023-09-12 05:35:04 作者:℡﹍習慣單身

问题的概述:ActivityA开始ActivityB与 startActivityForResult 。 ActivityB设置使用的setResult 的结果,然后开始ActivityC与 startActivity 。 ActivityC然后启动 singleTop ActivityA与 CLEAR_TOP 标志设置。我希望这完成ActivityB并调用ActivityA的 onActivityResult 方法。不幸的是这种方法是没有得到要求ActivityA的重启。

Overview of Problem: ActivityA starts ActivityB with startActivityForResult. ActivityB sets its result using setResult and then starts ActivityC with startActivity. ActivityC then starts the singleTop ActivityA with the CLEAR_TOP flag set. I expect this to finish ActivityB and call ActivityA's onActivityResult method. Unfortunately this method is not getting called upon the restart of ActivityA.

ActivityA code :我已经开始一个活动(ActivityB)与 startActivityForResult()从ActivityA以下code

ActivityA Code: I have started an activity (ActivityB) with startActivityForResult() with the following code from ActivityA:

....
final Intent intent = new Intent(this, ActivityB.class);
startActivityForResult(intent, 0);   
....

下面code在ActivityA等待结果:

The following code in ActivityA waits for the result:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == ACTIVITY_RESULT){ 
        doSomethingSpecial();
    }
}

ActivityB code :我有一个确认对话框。当用户点击是,他们会将该活动,其中

ActivityB Code: I have a confirmation dialog. When the user clicks "Yes" they will set the Result of this activity, which

private void showConfirmationDialog(String message, final String username) {
    AlertDialog.Builder alert = new AlertDialog.Builder(this)
    .setMessage(message)
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            Intent intent = new Intent(this, ActivityC.class);

            setResult(ActivityA.ACTIVITY_RESULT);

            startActivity(intent);
        }
    })
    .setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    });
    alert.show();
}

ActivityC code :准备就绪后,下面的code被要求按一下按钮在ActivityC:

ActivityC Code: When ready, the following code is called upon a button click in ActivityC:

private void startActivityA(){
    Intent intent = new Intent(this, ActivityA.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
}

我期待在ActivityA的 doSomethingSpecial()函数被调用,但这没有发生。

I am expecting the doSomethingSpecial() function in ActivityA to be called, but this is not occurring.

NB :正如顺便说一句,如果ActivityB code被替换的,而是开始ActivityC从对话框,完成()被调用,即ActivityC永远不会打开和B简直是封闭的,那么 doSomethingSpecial()确实调用。因此,这个问题似乎一些缺乏后退堆栈和明确的顶部的功能认识到干。

NB: Just as an aside, if the ActivityB code is replaced, and instead of starting ActivityC from the dialog, finish() is called–i.e. ActivityC is never opened and B is simply closed, then doSomethingSpecial() is indeed called. Therefore this problem seems to stem from some lack of understanding of the functionality of the back stack and clear top.

任何援助将pciated,为什么这并不表现为所需的AP $ P $!我想出了一些解决这个黑客,但我,为什么这是发生在为了提高我的活动栈的理解最感兴趣。

Any assistance would be appreciated as to why this is not behaving as desired! I have come up with a number of hacks around this, but am most interested as to why this is occurring in order to improve my understanding of the activity stack.

推荐答案

我认为,从用户的角度来看,会觉得很奇怪,你是开始从后来的活动一个previous活动(A)(C ),而不是仅仅回去吧。

I think that from a user's perspective it will feel very strange that you are "starting" a previous activity (A) from a later activity (C), instead of just "going back" to it.

我相信你应该开始C来自B带 startActivityWithResult()。然后,当活动C被完成呼叫完成()。在B的 onActivityResult()呼叫的setResult()然后完成()

I believe you should start C from B with startActivityWithResult(). Then when activity C is done call finish(). In B's onActivityResult() call setResult() and then finish().