Search code examples
androidandroid-activityback-stack

Android: send result to an Activity cleaning the top of BackStack


I have 3 Activities: A, B, C.

I want that Activities work as follows:

A startActivityForResult B; B can start C or send result to A; C can go back to B or send result to A.

When B send result to A, B must be removed from backstack. When C send result to A, B and C must be removed from backstack.

I'm not able to send result to A from C. This means that A must be reusmed (NOT RECREATED) and onActivityResult() must be callled to process result:

I have tryed with this code but A is recreated and onActivityResult() is not called!!

public class C extends Activity{
    sendResultToA(){
        Intent i = new Intent(getActivity(), A.class);
        i.putExtra("dataBean", dataBean);
        i.putExtra("args", "save");
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        getActivity().setResult(Activity.RESULT_OK, i);
        startActivity(i);
    }
}

Any ideas to solve this problem? Thank you


Solution

  • Yes you can solve this problem,

    From Activity A -> startActivityForResult(ActivityB)

    From Activity B -> startActivityForResult(ActivityC)

    If all success set the result by following way.

    Intent i = new Intent();
    i.putExtra("dataBean", dataBean);
    i.putExtra("args", "save");
    setResult(Activity.RESULT_OK,i); //pass your intent
    finish(); // Call finish to remove ActivityC from the stack
    

    If you receive success in ActivityB then do the same. In onActivityResult check whether result successful or cancelled using Activity.RESULT_OK and Activity.RESULT_CANCELED.