首页 文章

为什么片段的setRetainInstance(true)方法不起作用?

提问于
浏览
1

我无法让我的片段在方向更改上保留其实例 .

活动类

public class MyActivity extends Activity
{
   private MyFragment fragment;

   public void onCreate(Bundle savedInstanceState)
   {
       if(savedInstanceState == null)
       {
           fragment = new MyFragment();
       }

       //null pointer exception on this line of code. fragment not being retained. 
       getFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).commit();
   } 
}

片段类

public class MyFragment extends Fragment
{ 
    private View view;
    private CustomListViewAdapter adapter;
    public ArrayList<HashMap<String, String>> arrHashMap;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        view = inflater.inflate(R.layout.fragment_screen, container, false);

        if(arrHashMap != null)
        {
            ListView lv = (ListView) view.findViewById(R.id.fragment_lv);
            adapter = new CustomListViewAdapter( (MyActivity)getActivity() , arrHashMap);
            lv.setAdapter(adapter);
            lv.setOnItemClickListener((MyActivity)getActivity());
        }
        else
        {
            /* some code to create arrHashMap variable

            */
            ListView lv = (ListView) view.findViewById(R.id.fragment_lv);
            adapter = new CustomListViewAdapter( (MyActivity)getActivity() , arrHashMap);
            lv.setAdapter(adapter);
            lv.setOnItemClickListener((MyActivity)getActivity());
        }

        return(view);
    }

    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
        setRetainInstance(true);
    }
}

尽管在onActivityCreated中设置了setRetainInstance(true),但MyFragment在方向更改时保持为null . 创建MyActivity时,也始终重新创建MyFragment . 另外,我知道setRetainInstance(true)不能用于UI片段,但是,我没有使用保存的适配器或视图成员变量,我只是在方向更改时重用保留的arrHashMap变量,所以我可以重新创建适配器并更改用户界面 .

1 回答

  • 0

    更新:我决定不使用setRetainInstance(true),我使用ObjectInputStream和ObjectOutputStream类解决了问题,并将arrHashMap对象保存到MyActivity的onSaveInstanceState(Bundle outState)方法中的文件中,并从中检索了arrHashMap对象 . 文件在MyActivity的onRestoreInstanceState(Bundle savedInstanceState)方法中 . 然后我继续使用重新获得的arrHashMap对象设置适配器 .

    作为补充说明,我将MyFragment的arrHashMap实例变量更改为静态变量,以便可以从MyActivity访问它 .

    保存代码:

    @Override
    public void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);
    
        try
        {
            File f = new File( this.getDir("myDir", Context.MODE_PRIVATE), "arrHashMap");
            ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(f));
            os.writeObject(MyFragment.arrHashMap);
            os.flush();
            os.close();
        }
        catch(IOException e)
        {
            return;
        }
    }
    

    恢复代码:

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState)
    {
        super.onRestoreInstanceState(savedInstanceState);        
    
        ArrayList<HashMap<String,String>> arrHashMap;
        try
        {
            File f = new File( this.getDir("myDir", Context.MODE_PRIVATE), "arrHashMap");
            ObjectInputStream is = new ObjectInputStream(new FileInputStream(f));
            arrHashMap =  ((ArrayList<HashMap<String, String>>) is.readObject() );
            is.close();
        }
        catch (Exception e)
        {
            arrHashMap = null;
        }
        if(arrHashMap != null)
        {
            ListView lv = (ListView)findViewById(R.id.fragment_lv);
            CustomListViewAdapter adapter = new CustomListViewAdapter(this, arrHashMap);
            lv.setAdapter(adapter);
            lv.setOnItemClickListener(this);
        }
    }
    

相关问题