kha
kha

Reputation: 20023

How to find out if the activity has resumed because another activity on top of the stack has called finish()

I have a view pager as a main activity with multiple fragments.

One of the fragments inside this view pager initiates another activity that does things and then finishes (think of it as picking a photo and then saving it to the database).

Once the second activity has finished, I call finish() and this brings me back to my previous fragment inside my view pager.

I however need to know if the activity that contained the view pager was started because the finish() on the second activity was called to do something (load the photo for instance).

I don't know how to pass this information back to the previous activity and I would like to avoid using an Intent with the extra flag (CLEAR_ACTIVITY_TOP) as this would mean I'd have to recreate the state in every other view pager fragment.

Is this possible? Is there a way to find out which activity's finish() caused the current activity to become active?

Many thanks,

Upvotes: 0

Views: 65

Answers (3)

Andrew Brooke
Andrew Brooke

Reputation: 12173

When you first go to the second activity, call it like this.

Intent intent = new Intent(this, YourSecondActivity.class);
startActivityForResult(intent, requestCode);

Request code is an integer that will let you identify which activity comes back with results later on.

Whenever you are done in your second activity, send back the extra data you need before calling finish(), like this

Intent intent = new Intent();
intent.putExtra("yourData", yourVariable);
setResult(RESULT_OK, intent);
finish();

Back in the first activity, override the onActivityResult() method, and do whatever you need to with the data that was returned.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
        if(resultCode == RESULT_OK){
            String result=data.getStringExtra("yourData");
        }
    }
}

Now you should be able to identify what activity is returning, and send back any other data you might need!

Upvotes: 1

harshitpthk
harshitpthk

Reputation: 4136

you would have to use startActivityForResult() and you will get results in onActivityResult in the Parent Activity. something like this.

// Fragment in First Activity
Intent i = new Intent(this, SecondActivity.class);
getActivity().startActivityForResult(i, 1);

//SecondActivity if sending back results
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(RESULT_OK,returnIntent);
finish();

//SecondActivity if not sending results.

Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);
finish();

handling results

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

 if (requestCode == 1) {
    if(resultCode == RESULT_OK){
        String result=data.getStringExtra("result");
    }
    if (resultCode == RESULT_CANCELED) {
        //Write your code if there's no result
    }
 }
}

reference How to manage `startActivityForResult` on Android?

Upvotes: 1

Buddy
Buddy

Reputation: 11038

Take a look at startActivityForResult. More docs under the "Starting Activities and Getting Results" section of the Activity page.

Sometimes you want to get a result back from an activity when it ends. For example, you may start an activity that lets the user pick a person in a list of contacts; when it ends, it returns the person that was selected. To do this, you call the startActivityForResult(Intent, int) version with a second integer parameter identifying the call. The result will come back through your onActivityResult(int, int, Intent) method.

Upvotes: 2

Related Questions