如何使用带片段的onSaveInstanceState和onRestoreInstanceState方法?

时间:2013-10-29 12:03:13

标签: java android listview android-fragments onrestoreinstancestate

我有两个Fragments。其中一个有一个EditText和一个Button。另一个只有一个ListView。如果模拟器的方向发生任何变化,如何保存ListView中的数据?

我的问题是,当我旋转模拟器的屏幕时,我不知道如何保存和恢复ListView。我应该在哪里放置onSaveInstanceStateonRestoreInstanceState方法以及如何使用它们?

MainActivity.java

    public class MainActivity extends Activity implements AddToDoFragment.OnToDoAddedListener {

private ArrayList<String> todoItems;
private ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FragmentManager fm = getFragmentManager();

    ToDoListFragment listToDo = new ToDoListFragment();
    listToDo = (ToDoListFragment) fm.findFragmentById(R.id.list_view_fragment);
    todoItems = new ArrayList<String>();
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems);
    listToDo.setListAdapter(adapter);
}

public void OnToDoAdded(String newToDo) {
    todoItems.add(newToDo);
    adapter.notifyDataSetChanged();

}

AddToDoFragment.java

public class AddToDoFragment extends Fragment {

public interface OnToDoAddedListener {

    public void OnToDoAdded (String newToDo);

}

private OnToDoAddedListener onToDoAddedListener;

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

    final EditText edittext = (EditText) view.findViewById(R.id.editText);
    final Button button = (Button) view.findViewById(R.id.addButton);
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            String text = edittext.getText().toString();
            onToDoAddedListener.OnToDoAdded(text);
            edittext.setText("");
        }
    });

    return view;

}

 @Override
    public void onAttach(Activity activity) {
      super.onAttach(activity);
      if (activity instanceof OnToDoAddedListener) {
        onToDoAddedListener = (OnToDoAddedListener) activity;
      } else {
        throw new ClassCastException(activity.toString()
            + " must implemenet AddToDoFragment.OnToDoAddedListener");
      }
    }
   }

ToDoListFragment.java

public class ToDoListFragment extends ListFragment {
}

1 个答案:

答案 0 :(得分:3)

您需要覆盖活动的onSaveInstanceState方法,以便了解何时需要保存状态。然后,您还需要更新onCreate方法,以检查savedInstanceState是否为null。如果是null,那么活动尚未启动。这是您的MainActivity课程的示例,您可以从那里开始:

<强> MainActivity.java

public class MainActivity extends Activity implements AddToDoFragment.OnToDoAddedListener {

private ArrayList<String> todoItems;
private ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if(savedInstanceState == null) {
        todoItems = new ArrayList<String>();
    } else {
        todoItems = savedInstanceState.getStringArrayList("todoItemTag");//the tag must match what the variable was saved with
    }

    FragmentManager fm = getFragmentManager();

    ToDoListFragment listToDo = new ToDoListFragment();
    listToDo = (ToDoListFragment) fm.findFragmentById(R.id.list_view_fragment);
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems);
    listToDo.setListAdapter(adapter);
}

public void OnToDoAdded(String newToDo) {
    todoItems.add(newToDo);
    adapter.notifyDataSetChanged();

}

//Saving the instance by overriding this function
@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    outState.putStringArrayList("todoItemTag", todoItems);//it would be advised to make the tags a static final String
}

希望这有帮助!

PS:我不认为onRestoreInstanceState方法确实是必要的。我想我之前从未使用过它。我相信您应该能够使用null方法中的onCreate检查提供相同的功能。