1

Fragment に向きの変更時にインスタンスを保持させることができません。

活動クラス

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 変数のみを再利用しているので、アダプターを再作成できます。そしてUIを変更。

4

2 に答える 2

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);
    }
}
于 2013-08-26T17:59:06.757 に答える