Java 如何刷新片段“onResume?”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20598219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to refresh fragments 'onResume?'
提问by user2883477
I want to refresh fragments from an Actvitiy. (Let's call the activity A) Activity A contains the fragments.
我想从 Activitiy 刷新片段。(我们称之为活动 A)活动 A 包含片段。
When I finish Activity B then Activity A should refresh the fragments.
当我完成活动 B 时,活动 A 应该刷新片段。
I was thinking this would be done in onResume? I tried simply restarting the activity through an intent but that's not usability friendly.
我在想这会在 onResume 中完成吗?我尝试通过意图简单地重新启动活动,但这不是可用性友好的。
So how do I refresh fragments when I resume an activity?
那么如何在恢复活动时刷新 Fragment 呢?
回答by Mehul
Just put this code inside
把这段代码放在里面
onResume()
onResume()
For example you want to update your listview when u return to your fregment.
例如,当你返回你的片段时,你想更新你的列表视图。
@Override
public void onResume() {
super.onResume();
if (allowRefresh)
{
allowRefresh = false;
lst_applist = db.load_apps();
getFragmentManager().beginTransaction().detach(this).attach(this).commit();
}
}
Here getFragmentManager().beginTransaction().detach(this).attach(this).commit();
这里getFragmentManager().beginTransaction().detach(this).attach(this).commit();
This line detach and re-attach the fragment so your content will be updated.
此行分离并重新附加片段,以便更新您的内容。
回答by Jois
I agree with Mehul But u can alse do it this way .if you want to replace your fragment you could just say
我同意 Mehul 但你也可以这样做。如果你想替换你的片段,你可以说
@Override
public void onResume() {
super.onResume();
if (allowRefresh)
{
allowRefresh = false;
lst_applist = db.load_apps();
getFragmentManager().beginTransaction().replace(Fragment_Container,fragment).commit();
}
}
回答by Greenkraftz
The onResume()
get called always before the fragment is displayed. Even when the fragment is created for the first time . So you can simply move your from onViewCreated()
to onResume
.
该onResume()
显示片段之前总是得到调用。即使是第一次创建片段。所以你可以简单地将你的 from 移动onViewCreated()
到onResume
。
@Override
public void onResume() {
super.onResume();
//move your code from onViewCreated() here
}