java-如何将onSaveInstanceState和onRestoreInstanceState方法与片段一起使用?
作者:互联网
我有两个片段.其中之一具有一个EditText和一个Button.另一个只有一个ListView.如果模拟器的方向发生任何变化,如何保存ListView中的数据?
我的问题是,当我旋转模拟器的屏幕时,我不知道如何保存和还原ListView.我应该在哪里放置onSaveInstanceState和onRestoreInstanceState方法以及如何使用它们?
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 {
}
解决方法:
您将要覆盖活动的onSaveInstanceState方法,以便知道何时需要保存状态.然后,您还需要更新onCreate方法,以检查saveInstanceState是否为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方法不是真正必要的.我想我以前从未使用过它.我相信您应该能够通过onCreate方法中的null检查提供相同的功能.
标签:android-fragments,listview,onrestoreinstancestate,java,android 来源: https://codeday.me/bug/20191122/2060828.html