Kevin Cruijssen
Kevin Cruijssen

Reputation: 9326

Return to MainActivity with added data from another Activity

I know how to send data from an activity to the Next Activity: [Intent.putExtra(key, value);].

I also know how to return from an activity to the previous one and include data: [see this stackoverflow answer].

What I want to know however is, how I can return to the Home Activity and include data. As an example:

  1. I start the app in Activity A and do some stuff
  2. Then I go to Activity B and do some stuff
  3. Then I go to Activity C and do some stuff
  4. And then I want to return to Activity A (and do different stuff)

In step 4 I want to do something different than normally in the onResume (or another method it returns to), so the data I include is a boolean with a key (preferably using intent.putExtra("myKey", true); (or false if something failed)). In the onResume I then do something like:

@Override
protected void onResume(){
    super.onResume();
    if(intent.getExtras() != null && intent.getAction().toString().equals("myKey")){
        if(intent.getExtras().getBoolean("myKey")){
            // do something else (step 4) on success
        }
        else{
            // do something else (step 4) on fail
        }
    }
    else{
        // Do regular stuff I normally do in the onResume()
    }
}

Ok, I think I've have solved my problem. My HomeActivity already is a BroadcastReceiver, so I just send an intent from my last Activity to my HomeActivity using the Broadcast..

Will test this to see if it works. Though I'm kinda doubting it will when I'm in another Activity. (My HomeActivity is already a BroadcastReceiver for some Http-requests I've had to send and needed the result of, which are AsyncTasks of course.)

Ok, this doesn't work when I'm not already in the MainActvity. So more suggestions are welcome..

Upvotes: 0

Views: 933

Answers (3)

Kevin Cruijssen
Kevin Cruijssen

Reputation: 9326

Ok, I've found a solution (also thanks to shimi_tab's answer and Budius comment about onNewIntent):

In my Activity C when I want to return:

Intent home = new Intent(this, A.class);
home.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
home.putExtra("myKey", true);
startActivity(home);

In my Activity A:

// This allows us to use getIntent to get the latest intent, instead of the first Intent used in the onCreate
@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
}

@Override
protected void onResume(){
    super.onResume();

    if(getIntent().getExtras() != null && getIntent().getExtras().getBoolean("myKey")){
        // Do something else after returning from C
    }
    else{
        // Do regular things on a normal onResume (like back from B or Settings)
    }
}

NOTE (trivial to the original question): In my case I use Google Services in my Activity A, where I had googleApiClient.onConnect(); in the onStart() method. So, I also had to add the following to the onConnected() method:

@Override
public void onConnected(Bundle b){
    Bundle extras = getIntent().getExtras();
    if(extras == null || (extras != null && !extras.getBoolean("myKey"))){
        // Do regular stuff
    }
    else{
        // Do something else after we returned from C
    }
}

Upvotes: 3

shimi_tap
shimi_tap

Reputation: 7962

You should make activity "A" state less. call Activity "A" again with a new intent with the flag FLAG_ACTIVITY_CLEAR_TOP.

Intent intent = new Intent(this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

And all the data you want.

You shouldn't relay on ActivityResult for what you are trying to do.

Upvotes: 1

Budius
Budius

Reputation: 39836

to put data back to the calling activity, you'll use this method:

public final void setResult (int resultCode, Intent data)

on this intent you feel free to fill it up to all the data you need. The activity result method is as follows:

protected void onActivityResult (int requestCode, int resultCode, Intent data)

so that's the same data that you put on the result.

Upvotes: 1

Related Questions