Fragment栈回退和传参方式
2016-03-01 10:39
302 查看
1.返回栈
当你需要点击返回键是返回上一个Fragment而不是直接退出Activity时,需要把之前的fragment加入返回栈。ft.addToBackStack(null),然后重写退出键的方法。(
public boolean onKeyDown(int keyCode, KeyEvent event)
)
具体的解释点击打开链接
2.AVD横竖屏切换
我们都知道让AVD在横竖屏切换的时候,当前Activity中的fragment都会通过Fragment.instantiate重新生成,该方法将使用默认的构造函数来生成相应的Fragment。因此,使用public PopFragment(String titleString) {
this.titleString = titleString;
}该方法来传参,然后new一个对象是不安全的。
可以通过getArguments获得之前设置的数据,其原理是在FragmentActivity切换时会调用onRetainNonConfigurationInstance方法将FragmentManager中管理的所有Fragment其状态数据(其中就包括了这个设置的Bundle)保存在一个FragmentActivity.NonConfigurationInstances对象实例中,这样在新的FragmentActivity启动时在onCreate方法中可以使用Activity.getLastNonConfigurationInstance()方法来获取这个对象,然后通过FragmentManager.restoreAllState方法还原所有Fragment及其状态,需要注意的是在这种情况下可能会出现之前的Fragment没有detach而处于活动状态导致该Fragment的视图生成,可能会造成两个Fragment视图重叠的情况,在官方的Support.v4的例子FragmentTabs.TabManager.addTab里有一段检查Fragment是否detach的代码正是用于解决这个问题。
具体代码我把它贴出来
PopActivity.javapublic class PopActivity extends Activity{
private Button one, two;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pop);
one = (Button)findViewById(R.id.button1);
two = (Button)findViewById(R.id.button2);
one.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
PopFragment p1 = PopFragment.getInstance("one");
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content, p1);
ft.addToBackStack(null);////////////
ft.commit();
}
});
two.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
PopFragment p1 = PopFragment.getInstance("two");
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content, p1);
ft.addToBackStack(null);///////////////////////////
ft.commit();
}
});
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {//实现回退
if (keyCode == KeyEvent.KEYCODE_BACK) {
if(getFragmentManager().getBackStackEntryCount() == 0){
finish();
}else{
getFragmentManager().popBackStack();// 返回上一个fragment
}
}
return super.onKeyDown(keyCode, event);
}
}对应的pop布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two" />
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>PopFragment.java
public class PopFragment extends Fragment {
public static PopFragment getInstance(String title){//安全的传参方式
PopFragment popFragment = new PopFragment();
Bundle bundle = new Bundle();
bundle.putString("title", title);
popFragment.setArguments(bundle);
return popFragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.pop_test, container, false);
TextView tView = (TextView)view.findViewById(R.id.textView1);
tView.setText(getArguments().getString("title"));
return view;
}
}对应的pop_test布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories