Using onResume () to Update Activity

I can’t understand for my whole life how to update activity after clicking the "Back" button. Currently, I have activity A that shoots the intention to go to B and while in action B, if you click back, I want to go back to action A, but update it. I can use this intent to update activity now:

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

But I cannot figure out how to use the onResume () function to update my action A after returning to it.

+7
source share
3 answers

If you need special ActivityA behavior when returning from ActivityB , you should use startActivityForResult(Intent intent, int requestCode) instead of startActivity(Intent intent) :

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

That way, you can detect the completion of ActivityB 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 end ActivityB by clicking the back button. In this case, resultCode will be RESULT_CANCELLED .

+10
source

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 update Activity A

+3
source

You need to put code that updates the user interface of your activity in the onResume() method. Perhaps you should post some more code or explain what exactly you are trying to update.

+1
source

All Articles