android - refresh parent Activity when child Activity finishes

Ofek Agmon picture Ofek Agmon · Dec 11, 2015 · Viewed 9.2k times · Source

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?

Answer

Nitesh picture Nitesh · Dec 11, 2015

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.