片段在Android上的生命周期 [英] The life cycle of Fragment in Android
问题描述
我问一个关于片段的初始化问题后我的code(<一个href=\"http://stackoverflow.com/questions/13580369/delay-initialization-when-using-fragment-in-android\">Delay在Android中使用片段时初始化)。这个问题就解决了。但我的问题仍然存在。当我打电话的onLoad()
来获取变量,它抛出 NullPointerException异常
。我初始化 onCreateView这些变量()
。所以我想在生命周期结束。我loged的信息在A.java这些功能:的onDestroy
, onDestroyView
,的onStop
。在 onDestroyView
和的onStop
被称作而的onDestroy
不是当碎片A被改变到另一个。奇怪的:
有在片段答:我输入一个字符串 ABC
A的EditText。 1,如果生命周期结束,那么当我改变片段从A点到另一个然后改回A,输入应该是空的。但事实并非如此。 2,如果生命周期没有结束,为什么我不能得到变量?
那么,有什么挽救 ABC
地方?那是什么?在哪里?
看来,我不能耽误在片段的初始化。我不能让非静态变量。
code:
/ **超类碎片*的/
公共类BaseFragment扩展片段{
公共无效的onLoad(上下文的背景下){
}
}/ ** * /
公共类AFragment扩展BaseFragment {
TextView的名称;
@覆盖
公共查看onCreateVew(...){
NAME =新的TextView(..);
}
@覆盖
公共无效的onLoad(上下文的背景下){
//此处将抛出NullPointerException
name.setText(======);
}
}
公共类TabsAdapter扩展FragmentPagerAdapter实现TabHost.OnTabChangeListener,ViewPager.OnPageChangeListener {
....
@覆盖
公共片段的getItem(INT位置){
TabInfo信息= mTabs.get(位置);
返回Fragment.instantiate(mContext,info.mClss.getName(),info.mArgs);
} @覆盖
公共无效使用onPageSelected(INT位置){
TabWidget插件= mTabHost.getTabWidget();
INT oldFocusability = widget.getDescendantFocusability();
widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
mTabHost.setCurrentTab(位置); widget.setDescendantFocusability(oldFocusability); BaseFragment F =(BaseFragment)的getItem(位置);
f.onLoad(mContext);
}
}
但我的问题仍然存在。当我打电话的onLoad()来获得
变量,它会引发NullPointerException异常。我初始化这些
在onCreateView变量()。
块引用>问题出在你与那些片段的工作方式。更确切地说,在
使用onPageSelected
方法调用的FragmentPagerAdapter
的getItem()
方法来查找该位置上的片段(或者这是我在想什么,你正在试图做的)。但是,调用此方法简单的实例化一个新片段每次调用的时候,它不会给你的ViewPager
的片断的引用这一立场。此新实例化的片段没有附加到活动
,它是onCreateView
方法还没有被调用,所以它不吨有创建的任何意见。试图使用您的的onLoad
方法来访问这个视图之一是错误的,它会抛出一个NullPointerException异常
。你应该尝试使用我的code从previous的答案,因为这code将尝试找到该ViewPager
实例化,并且它使用的片段通过其页面FragmentManager
。I asked a question about Fragment's initialization and post my code( Delay initialization when using Fragment in Android ). The question is solved. But my problem still exists. When I call
onLoad()
to get the variables, it throwsNullPointerException
. I initialize these variables inonCreateView()
. So I guess the life cycle is end. I loged the info in A.java in these function:onDestroy
,onDestroyView
,onStop
. theonDestroyView
andonStop
is called while theonDestroy
is not when fragment A is changed to another one. Strange:There is a EditText in fragment A. I input a string
abc
. 1、If the life cycle is end, then when I change fragment from A to another then change back to A, the input should be empty. But it's NOT. 2、if the life cycle is not end, why can't I get the variable?So is there anything to save the
abc
somewhere? What's it? Where?It seems that I can't delay the initialization in Fragment. I can't get the non-static variables.
Code: /** super class of Fragment */ public class BaseFragment extends Fragment { public void onLoad(Context context){ } } /** */ public class AFragment extends BaseFragment{ TextView name; @Override public View onCreateVew(...){ name = new TextView(..); } @Override public void onLoad(Context context){ // here will throw NullPointerException name.setText("========="); } } public class TabsAdapter extends FragmentPagerAdapter implements TabHost.OnTabChangeListener,ViewPager.OnPageChangeListener { .... @Override public Fragment getItem(int position) { TabInfo info = mTabs.get(position); return Fragment.instantiate(mContext, info.mClss.getName(), info.mArgs); } @Override public void onPageSelected(int position) { TabWidget widget = mTabHost.getTabWidget(); int oldFocusability = widget.getDescendantFocusability(); widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS); mTabHost.setCurrentTab(position); widget.setDescendantFocusability(oldFocusability); BaseFragment f = (BaseFragment) getItem(position); f.onLoad(mContext); } }
解决方案But my problem still exists. When I call onLoad() to get the variables, it throws NullPointerException. I initialize these variables in onCreateView().
The problem is in the way you work with those fragments. More exactly, in the
onPageSelected
method you call theFragmentPagerAdapter
'sgetItem()
method to find the fragment for that position(or this is what I'm thinking you're trying to do). But calling this method simply instantiate a new fragment each time you call it, it doesn't give you a reference to theViewPager
's fragment for that position. This newly instantiated fragment isn't attached to anActivity
and it'sonCreateView
method hasn't be called so it doesn't have any views created. Attempting to use youronLoad
method to access one of this views is wrong and it will throw thatNullPointerException
. You should try to use my code from the previous answer, as that code will try to find the fragments that theViewPager
instantiated and it's using as its pages through theFragmentManager
.这篇关于片段在Android上的生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!