大佬教程收集整理的这篇文章主要介绍了android – Fragment在旋转设备两次时失去状态,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
>创建活动时,它会显示Fragment1.
>当用户按下Fragment1上的按钮时,它会显示Fragment2,添加到BACkstack.
碎片很简单. Fragment1包含checkBox和EditText. Fragment2包含简单的TextView.我也在片段的onCreate(…)方法中调用setRetainInstance@R_801_6334@.
问题:如果显示Fragment2并且设备旋转两次,Fragment1将失去其状态.但是,如果设备只旋转一次,一切都按预期工作.
重现步骤:
>启动应用程序.
>选中checkBox并在EditText中键入一些文本
>按Button启动Fragment2
>旋转设备
>再次旋转设备
>按设备上的后退按钮(或模拟器上的ESC)返回Fragment1
Fragment1失去了状态(复选框未选中,EditText为空).
但是,如果在步骤#4之后返回Fragment1 – Fragment1将按预期保持其状态.
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://@R_197_10906@mas.android.com/apk/res/android" android:id="@+id/placeholder" android:layout_width="match_parent" android:layout_height="match_parent" />
fragment1.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://@R_197_10906@mas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <checkBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="checkBox" /> <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" > </EditText> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start Fragment2" /> </LinearLayout>
fragment2.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://@R_197_10906@mas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Fragment #2" /> </LinearLayout>
FragmentTestActivity.java:
package fragmenttest.example.com; import android.app.Activity; import android.app.FragmentManager; import android.os.bundle; public class FragmentTestActivity extends Activity implements FragmentListener { @Override public void onCreate(Bundle savedInstanceStatE) { super.onCreate(savedInstanceStatE); setContentView(R.layout.main); FragmentManager fm = getFragmentManager(); if (fm.findFragmentByTag(Fragment1.TAG) == null) { fm.begintransaction().replace(R.id.placeholder,new Fragment1(),Fragment1.TAG).commit(); } } @Override public void onButtonClick() { FragmentManager fm = getFragmentManager(); if (fm.findFragmentByTag(Fragment2.TAG) == null) { fm.begintransaction().replace(R.id.placeholder,new Fragment2(),Fragment2.TAG).addToBACkStack(Fragment2.TAG).commit(); } } }
Fragment1.java:
package fragmenttest.example.com; import android.app.Activity; import android.app.Fragment; import android.os.bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.onClickListener; import android.view.ViewGroup; import android.widget.button; public class Fragment1 extends Fragment { public static final String TAG = Fragment1.class.getName(); private FragmentListener mListener; @Override public void onCreate(Bundle savedInstanceStatE) { super.onCreate(savedInstanceStatE); setRetainInstance@R_801_6334@; } @Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceStatE) { View root = inflater.inflate(R.layout.fragment1,container,falsE); Button button = (Button) root.findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mlistener.onButtonClick(); } }); return root; } @Override public void onAttach(Activity activity) { super.onAttach(activity); mListener = (FragmentListener) activity; } }
Fragment2.java:
package fragmenttest.example.com; import android.app.Fragment; import android.os.bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment2 extends Fragment { public static final String TAG = Fragment2.class.getName(); @Override public void onCreate(Bundle savedInstanceStatE) { super.onCreate(savedInstanceStatE); setRetainInstance@R_801_6334@; } @Override public View onCreateView(LayoutInflater inflater,Bundle savedInstanceStatE) { View root = inflater.inflate(R.layout.fragment2,falsE); return root; } }
FragmentListener.java:
package fragmenttest.example.com; public interface FragmentListener { public void onButtonClick(); }
以上是大佬教程为你收集整理的android – Fragment在旋转设备两次时失去状态全部内容,希望文章能够帮你解决android – Fragment在旋转设备两次时失去状态所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。