Ofek Agmon
Ofek Agmon

Reputation: 5198

android - refresh parent Activity when child Activity finishes

I have a parent Activity which starts a child activity.

In the child activity, I do:

toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Ride");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

When I click the Home/Up button, I finish the activity.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        finish();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

And in addition, I do finish In one more place in the Child Activity, and in both places where I have the finish, it goes back to the Parent Activity.

My question:

My problem is that I want to refresh the Parent Activity only in the case of one of the finishes.

When I do the finish not from the Up button - the data in the Parent Activity changes, so - When I press the Back/Up button, there is no need to refresh the Parent, but when I do the other finish, I want to refresh the Parent.

How can I detect that?

Upvotes: 4

Views: 9852

Answers (6)

Houssin Boulla
Houssin Boulla

Reputation: 2859

Use NavUtils.navigateUpFromSameTask(this); onBackPressed function like this:

@Override
public void onBackPressed() {
    super.onBackPressed();
    NavUtils.navigateUpFromSameTask(this);
}

Upvotes: 0

Centell
Centell

Reputation: 409

in this case, use onResume.

@Override
protected void onResume() {
    super.onResume();
    // your code (for example: initialize(); etc.. )
}

If you refer to this article, you will understand why.

activity life cycle Android

Upvotes: 3

Shengfeng Li
Shengfeng Li

Reputation: 644

In Parent Activity

startActivityForResult(Intent intent, int REQUEST_CODE);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
case RESULT_OK:
    //refresh parentAcitivty  
    //get the result from the Up button back
    //e.g adapter.notifydatasetchanged()
case RESULT_CANCEL:
    //Do nothing 
    //return from childActivity by other finishes

In ChildActivity

case 1: //**from the Up button**
   setResult(RESULT_CANCEL);
   finish();
case 2: //**do the other finish**
   setResult(RESULT_OK);
   finish();

I hope the code will give you a hint.

Upvotes: 3

capt.swag
capt.swag

Reputation: 10661

You have finish() method in two places as described. One is the back button on the ActionBar (let it be called AbBack) and the other one in one some other place (RandomBack).

Let A be the main Activity be B be the child Activity.

In A instead of startActivity() method, use

startActivityForResult(Intent intent, int REQUEST_CODE);

This is done so that, Activity B when it finishes can return data to this activity, and depending upon the data you can refresh/not-refresh

So in Activity B,

In the case of AbBack, call this method setResult(RESULT_OK); and for RandomBack, call setResult(RESULT_CANCELED);

Finally in Activity A, you have to override the method

onActivityResult(int requestCode, int resultCode, Intent data)

where you can compare resultCode == RESULT_OK and if so, that means data is from AbBack or if not it's from RandomBack, and do the required refresh.

Upvotes: 2

unlimited101
unlimited101

Reputation: 3803

Try to use navigateUpFromTask()

Upvotes: 0

Nitesh
Nitesh

Reputation: 3903

Start child activity as

startActivityForResult(intent, requestCode)

And in case on up button set result as success in child activity and in case of backpress set result as failure.

And in onActivityResult(..) of parent activity get the status and kill the parent activity in the desired case.

Upvotes: 7

Related Questions