0

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

Community
  • 1
  • 1
Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
  • if you save the value of the new action in the SharedPreferences and then check for it on the MainActivity? – Alejandro Cumpa Jun 11 '14 at 15:16
  • @El_Mochiq That's also a possibility, but then it still leaves me with the question what the best approach is to return to the MainActivity. Just `startActivity(new Intent(...));` isn't a good idea, since MainActivity already is started and is now just Paused. – Kevin Cruijssen Jun 12 '14 at 07:29

3 Answers3

3

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
    }
}
Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
  • 1
    Here is a little about the Lifecycle https://stackoverflow.com/questions/8619883/onnewintent-lifecycle-and-registered-listeners – Diblo Dk Feb 25 '19 at 17:29
1

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.

Budius
  • 39,391
  • 16
  • 102
  • 144
  • I didn't knew this worked for going to just a random paused activity (in my case the Home Activity), instead of just the previous one. – Kevin Cruijssen Jun 11 '14 at 14:44
  • So to make this work in my case I need to: Use startActivityForResult to go from Activity A to B, then use startActivityForResult again to go from B to C, then use setResult in the onPause & onDestroy of C. Then I also need to add onActivityResult in A and B (and in B I should automatically finish itself with again setResult in the onPause & onDestroy), so I then arrive at onActivityResult in Activity A.. Is there really no other way :S – Kevin Cruijssen Jun 11 '14 at 15:11
  • that's correct. I know that describing it in English seems to be a lot of work, but honestly, in Java it's just a few of lines of code. – Budius Jun 11 '14 at 15:28
  • 2
    @shimi_tap answer also should work. But you have to be a bit more extra careful with flags and task conditions. If you want that approach, make sure to read regarding the CLEAR_TOP flag and `onNewIntent` activity callback. – Budius Jun 11 '14 at 15:29
1

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.

Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
shimi_tap
  • 7,822
  • 5
  • 23
  • 23
  • I tried this, but then I'm returned to the onCreate of the MainActivity instead of the onResume. Should I just add the if(Intent-boolean) to the onCreate then instead of the onResume, or is there a way to just go to the onResume instead of onCreate? – Kevin Cruijssen Jun 12 '14 at 07:52