SetResult not working when activity finished by clearTop

亡梦爱人 提交于 2020-01-15 04:59:29

问题


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: 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);   
....

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: 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: 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);
}

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

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.

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.


回答1:


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.

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().



来源:https://stackoverflow.com/questions/12126125/setresult-not-working-when-activity-finished-by-cleartop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!