问题
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?
回答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.
回答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, In
tent 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.
回答3:
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.
回答4:
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
回答5:
Try to use navigateUpFromTask()
回答6:
Use NavUtils.navigateUpFromSameTask(this); onBackPressed function like this:
@Override
public void onBackPressed() {
super.onBackPressed();
NavUtils.navigateUpFromSameTask(this);
}
来源:https://stackoverflow.com/questions/34225067/android-refresh-parent-activity-when-child-activity-finishes