Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
532 views
in Technique[技术] by (71.8m points)

android - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...