Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 447
Ofek Agmon Avatar asked Oct 21 '25 03:10

Ofek Agmon


1 Answers

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.

like image 74
Nitesh Avatar answered Oct 22 '25 17:10

Nitesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!