使用onResume()刷新活动 [英] Using onResume() to refresh activity
问题描述
我不能为我的生活弄清楚如何有一个活动pressing后退按钮后刷新。我现在有一个触发的意图,转到B活性A和行为,同时B上的,如果你preSS后,我想回去作为的,但有它自动刷新。我可以用这个意图刷新当前的活动:
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();
但我不知道如何正确使用onResume()函数将返回后刷新我扮演一个。
But I can't figure out how to properly use the onResume() function to refresh my act A after going back to it.
推荐答案
如果你需要一个特别的行为 ActivityA
从 ActivityB <回来时, / code>,你应该使用
startActivityForResult(意向意图,诠释请求code)
而不是 startActivity(意向意图)
:
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);
这样一来,您就可以检测到 ActivityB
的终止 ActivityA
超载 onActivityResult(INT申请code,INT结果code,意图意图)
:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == REQUEST_CODE) {
doRefresh(); // your "refresh" code
}
}
这个工程,即使你终止 ActivityB
按后退按钮的preSS。该结果code
将 RESULT_CANCELLED
默认情况下,在这种情况下。
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.
这篇关于使用onResume()刷新活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!