I have activity A with a listView, a user clicks on any item which starts Activity B. Depending on what the user does in activity B may change the listView on activity A.
So my question:
How can I tell activity A that it was resumed when the user returns to it from activity B?
I want to be able to signal a refresh of the listView in activity A when it is returned to.
Thanks!
Using startActivityForResult
is the most elegant way to do it. OnResume
will happen every time you close and open up the app, which will use a bit more resources, especially if you're refreshing a lot of data.
Declare the request code as a constant at the top of your activity:
public static final int OPEN_NEW_ACTIVITY = 123456;
Put this where you start the new activity:
Intent intent = new Intent(this, NewActivity.class);
startActivityForResult(intent, OPEN_NEW_ACTIVITY);
Do something when the activity is finished. Documentation suggests that you use resultCode
, but depending on the situation, your result can either be RESULT_OK
or RESULT_CANCELED
when the button is pressed. So I would leave it out.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == OPEN_NEW_ACTIVITY) {
// Refresh your ListView here
// ....
}
}
For some reason, I had trouble when putting this in a Fragment. So you will have to put it in the Activity.
You can add the code to refresh listview in the onResume()
of Activity A.
@Override
protected void onResume() {
super.onResume();
//Code to refresh listview
}
I have another way to do this, which might come handy in some situations - I keep private Bundle variable, let's call it state
, and when I start an activity I put a string that says which activity I started:
state.putString("startedActivity", "A");
Intent intent = new Intent(this, ActivityA.class);
startActivity(intent);
I store the state bundle to persevere device changes (rotation etc.):
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putAll(state);
}
In onCreate
I load the state:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
savedInstanceState = new Bundle();
}
state = savedInstanceState;
}
Then in onResume
method, I can check, whether I was just at some Activity. Note that I remove the string from the state immediately, so it doesn't show up again at rotation change etc.
public void onResume() {
super.onResume();
if (state.getString(STARTED_ACTIVITY, null) != null) {
// we just came from another activity!
state.putString(STARTED_ACTIVITY, null); // clear the state
// do whatever you need
}
}
Use startActivityForResult to launch B activity. And check the data from activity B, on activity A s onActivityResult() method. Use notifyDataSetChanged for the listView to update contents.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With