Android,如何从 FragmentActivty 重新启动/刷新片段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11578000/
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
Android, How to restart/refresh a fragment from FragmentActivty?
提问by Hesam
In my FragmentActivity I have a fragment. This fragment loads data from server. I want we user clicks on refresh button then I call a method in fragment and do refreshing process.
在我的 FragmentActivity 我有一个片段。此片段从服务器加载数据。我希望我们用户点击刷新按钮,然后我在片段中调用一个方法并进行刷新过程。
I created an interface in FragmentActivity and set its variable when user clicks on refresh button. The code is like this:
我在 FragmentActivity 中创建了一个界面,并在用户单击刷新按钮时设置其变量。代码是这样的:
public class MainScreen extends FragmentActivity {
public interface OnRefreshSelected {
public void refreshFragment(boolean flag);
}
private OnRefreshSelected onRefreshSelected;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "Try to create MainScreen...");
setContentView(R.layout.activity_main);
onRefreshSelected = (OnRefreshSelected) MainScreen.this;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_refresh:
onRefreshSelected.refreshFragment(true);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
And in Fragment class i implemented this interface:
在 Fragment 类中,我实现了这个接口:
public class CategoryFragment extends Fragment implements OnRefreshSelected {
@Override
public void refreshFragment(boolean flag) {
Log.i(TAG, "refresh requested. Try to reload data for this fragment...");
getData();
}
}
When I run, the application crashes and logcat shows this message:
当我运行时,应用程序崩溃并且 logcat 显示此消息:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.astro.recipe.activities/com.astro.recipe.activities.MainScreen}: java.lang.ClassCastException: com.astro.recipe.activities.MainScreen
and points to this line:
并指向这一行:
onRefreshSelected = (OnRefreshSelected) MainScreen.this;
What is the best way to have access to a method of fragment from its host? any suggestion would be appreciated. Thanks
从其主机访问片段方法的最佳方法是什么?任何建议将不胜感激。谢谢
回答by biegleux
Executing this
执行这个
onRefreshSelected = (OnRefreshSelected) MainScreen.this;
you are assigning your activity in onRefreshSelected
field, trying to cast it to your interface, but your activity doesn't implement the interface, that's why ClassCastException
raised.
您正在onRefreshSelected
现场分配您的活动,试图将其投射到您的界面,但您的活动没有实现该界面,这就是为什么ClassCastException
提出。
Instead use something like this
而是使用这样的东西
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_refresh:
CategoryFragment fragment = (CategoryFragment) getFragmentManager()
.findFragmentById(R.id.category_fragment);
if (fragment != null) {
fragment.getData();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
Your activity can call methods in the fragment by acquiring a reference to the Fragment from FragmentManager, using findFragmentById()or findFragmentByTag().
您的活动可以通过使用findFragmentById()或findFragmentByTag()从 FragmentManager 获取对 Fragment 的引用来调用 Fragment 中的方法。
回答by Pradeep
I agree with biegleux... But my suggestion is
我同意biegleux...但我的建议是
public class CategoryFragment extends Fragment{
private static CategoryFragment categoryFrag;
private CategoryFragment(){}
public static CategoryFragment getInstance(){
if(null == categoryFrag){
categoryFrag = new CategoryFragment();
}
return categoryFrag;
}
public void getData(){
/*** Screen Refresh code****/
}
}
I your active when he selects option
当他选择选项时,我是活跃的
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_refresh:
CategoryFragment fragment = CategoryFragment.getInstance();
fragment.getData();
return true;
default:
return super.onOptionsItemSelected(item);
}