标签:
顾名思义,Fragment持久化是为了在窗口状态发生变化时,保存当前Fragment状态以免数据丢失。
关键点是使用Fragment.setArguments和Fragment.getArguments方法
Fragment类
package mobile.android.fragment.persistence; import android.app.Activity; import android.app.Fragment; import android.os.Bundle; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MyFragment extends Fragment { //name字段需要在onSaveInstanceState方法中保存 public String name = "name:default"; @Override public void onActivityCreated(Bundle savedInstanceState) { if (savedInstanceState != null) { //恢复name字段值 String s = savedInstanceState.getString("name"); if (s != null) name = s; } super.onActivityCreated(savedInstanceState); } @Override public void onSaveInstanceState(Bundle outState) { //保存name字段值 outState.putString("name", name); super.onSaveInstanceState(outState); } //onInflate方法会在MyFragment与窗口类绑定之前调用,所以可以在该方法中 //调用setArguments方法设置Bundle对象,并设置name参数的默认值 @Override public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState) { if (getArguments() == null) { Bundle bundle = new Bundle(); //设置name参数的默认值 bundle.putString("name", "arg:default"); //设置bundle对象 setArguments(bundle); } super.onInflate(activity, attrs, savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.my_fragment, container, false); return view; } }
1,重写方法onSaveInstanceState()方法,
outState.putString
2,在onInflate方法里面,调用setArguments();
3,在onActivityCreated方法里面恢复字段
package mobile.android.fragment.persistence; import android.app.Activity; import android.app.FragmentManager; import android.os.Bundle; import android.view.View; import android.widget.Toast; public class FragmentPersistenceActivity extends Activity { //需要在onStatanceState方法中保存fragment字段的值 private MyFragment fragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragment_persistence); FragmentManager fragmentManager = getFragmentManager(); if (savedInstanceState != null) //恢复fragment字段的值 fragment = (MyFragment) fragmentManager.getFragment( savedInstanceState, "fragment"); //如果状态未保存,创建新的MyFragment对象 if (fragment == null) { fragment = (MyFragment) getFragmentManager().findFragmentById( R.id.fragment_persistence); } } public void onClick_SetFragmentField(View view) { if (fragment != null) { fragment.name = "name:新的字段值"; fragment.getArguments().putString("name", "arg:新设置的值"); Toast.makeText(this, "成功为name字段赋值", Toast.LENGTH_LONG).show(); } } public void onClick_GetFragmentFieldValue(View view) { if (fragment != null) { Toast.makeText(this, fragment.name, Toast.LENGTH_LONG).show(); Toast.makeText(this, fragment.getArguments().getString("name"), Toast.LENGTH_LONG).show(); } } @Override protected void onSaveInstanceState(Bundle outState) { if (fragment != null) { //保存fragment字段的值,key是fragment getFragmentManager().putFragment(outState, "fragment", fragment); } super.onSaveInstanceState(outState); } }
在onCreate方法里面判断
savedInstanceState,如果不为空,就把Fragment提取出来,否则创建一个新的
在onSaveInstanceState方法中保存Fragment
标签:
原文地址:http://my.oschina.net/carbenson/blog/504460