片段在Android上的生命周期生命周期、片段、Android

2023-09-07 04:33:33 作者:心如刀割。

我问一个关于片段的初始化问题后我的code(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信息= mTab​​s.get(位置);        返回Fragment.instantiate(mContext,info.mClss.getName(),info.mArgs);    }    @覆盖    公共无效使用onPageSelected(INT位置){        TabWidget插件= mTab​​Host.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

Android Studio插件大全

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 throws NullPointerException. I initialize these variables in onCreateView(). So I guess the life cycle is end. I loged the info in A.java in these function: onDestroy, onDestroyView, onStop. the onDestroyView and onStop is called while the onDestroy 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 the FragmentPagerAdapter's getItem() 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 the ViewPager's fragment for that position. This newly instantiated fragment isn't attached to an Activity and it's onCreateView method hasn't be called so it doesn't have any views created. Attempting to use your onLoad method to access one of this views is wrong and it will throw that NullPointerException. You should try to use my code from the previous answer, as that code will try to find the fragments that the ViewPager instantiated and it's using as its pages through the FragmentManager.

 
精彩推荐
图片推荐