Skip to content Skip to sidebar Skip to footer

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); getSupportActionB

Solution 1:

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.

Solution 2:

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.

Solution 3:

In Parent Activity

startActivityForResult(Intent intent, int REQUEST_CODE);
@OverrideprotectedvoidonActivityResult(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

case1: //**from the Up button**
   setResult(RESULT_CANCEL);
   finish();
case2: //**do the other finish**
   setResult(RESULT_OK);
   finish();

I hope the code will give you a hint.

Solution 4:

in this case, use onResume.

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

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

activity life cycle Android

Solution 5:

Post a Comment for "Android - Refresh Parent Activity When Child Activity Finishes"