Question

[Solved] Using onResume() to refresh activity

I can’t for the life of me figure out how to have an activity be refreshed after pressing the back button. I currently have activity A that fires an intent to goto B and while on act B if you press back I want to go back to act A but have it refresh itself. I can use this intent to refresh the activity currently:

Intent refresh = new Intent(this, Favorites.class);
    startActivity(refresh);
    this.finish();

But I can’t figure out how to properly use the onResume() function to refresh my act A after going back to it.

Enquirer: Nick

||

Solution #1:

If you need a special behaviour of ActivityA when coming back from ActivityB, you should use startActivityForResult(Intent intent, int requestCode) instead of startActivity(Intent intent):

 startActivityForResult(new Intent(this, ActivityB.class), REQUEST_CODE); 

This way, you will be able to detect ActivityB‘s termination in ActivityA by overloading onActivityResult(int requestCode, int resultCode, Intent intent):

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    if (requestCode == REQUEST_CODE) {
        doRefresh(); // your "refresh" code
    }
}

This works even if you terminate ActivityB by the press of the back button. The resultCode will be RESULT_CANCELLED by default in that case.

Respondent: Shlublu

Solution #2:

use startActivityForResult(intent, requestCode);
to start Activity B from Activity A

then in Activity A override onActivityResult(int requestCode, int resultCode, Intent data)

there you can refresh your Activity A

Respondent: Mohammad Ersan

Solution #3:

You need to place the code that updates the UI of your Activity in the onResume() method. Maybe you should post some more code or explain what exactly are you trying to update.

Respondent: Ovidiu Latcu

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy