Reputation: 1272
On a button clicked, I am creating a new intent and starting an activity. AFTER the activity returns with a finish(), I want to refresh some of my screen elements. Here is what I am doing
private void onButtonClicked() {
Intent myIntent = new Intent(this, myActivity.class);
this.startActivity(myIntent);
refreshButtons();
}
My problem is that refreshButtons() gets called as the new intent is loading and not when it returns from the activity. How do I get refreshButtons() to run when I return from the activity I start
Upvotes: 0
Views: 1516
Reputation: 4821
Try this
private void onButtonClicked() {
Intent i = new Intent(this, myActivity.class);
startActivityForResult(i, 1);
refreshButtons();
}
In myActivity
Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);
finish();
Now in your FirstActivity class write following code for the onActivityResult() method.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
refreshButtons();
}
if (resultCode == RESULT_CANCELED) {
//Write your code if there's no result
}
}
}//onActivityResult
Upvotes: 1
Reputation: 1385
Use startActivityForResult
when you call an Intent and when finished that intent onActivityResult method will be called.
private void onButtonClicked() {
Intent myIntent = new Intent(this, myActivity.class);
this.startActivityForResult(myIntent);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
refreshButtons();
}
Upvotes: 1