Fragment中传递参数推荐用Fragment.setArguments(Bundle bundle),而不用构造方法。
来源:互联网 发布:两期二叉树模型 知乎 编辑:程序博客网 时间:2024/05/21 10:54
Fragment在Android3.0开始提供,并且在兼容包中也提供了Fragment特性的支持。Fragment的推出让我们编写和管理用户界面更快捷更方便了。
但当我们实例化自定义Fragment时,为什么官方推荐Fragment.setArguments(Bundle bundle)这种方式来传递参数,而不推荐通过构造方法直接来传递参数呢?为了弄清这个问题,我们可以做一个测试,分别测试下这两种方式的不同
activity_main:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.fragmenttest.MainActivity"></RelativeLayout>
fragment_main:
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" /></FrameLayout>
首先,我们来测试下通过构造方法传递参数的情况
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState == null){ getSupportFragmentManager().beginTransaction().add(R.id.activity_main,new TestFragment("param")).commit(); } } public static class TestFragment extends Fragment { private String mArg = "non-param"; public TestFragment(){ } public TestFragment(String arg){ mArg = arg; } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main,container,false); TextView tv = (TextView) rootView.findViewById(R.id.tv); tv.setText(mArg); return rootView; } }}竖屏效果:
横屏效果:
发生了什么问题呢?我们传递的参数哪去了?为什么会显示默认值?不急着讨论这个问题,接下来我们来看看Fragment.setArguments(Bundle bundle)这种方式的运行情况:
使用Fragment.setArguments(Bundle bundle)来传参:
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState == null){ getSupportFragmentManager().beginTransaction().add(R.id.activity_main,TestFragment.instance("param")).commit(); } } public static class TestFragment extends Fragment { private static final String Arg = "arg"; public TestFragment(){ } public static TestFragment instance(String arg){ TestFragment fragment = new TestFragment(); Bundle bundle = new Bundle(); bundle.putString(Arg,arg); fragment.setArguments(bundle); return fragment; } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main,container,false); TextView tv = (TextView) rootView.findViewById(R.id.tv); tv.setText(getArguments().getString(Arg)); return rootView; } }}
横屏:
看到了吧,我们传递的参数在横竖屏切换的情况下完好保存了下来,正确的显示给用户
那么这到底是怎么回事呢,我们知道设备横竖屏切换的话,当前展示给用户的Activity默认情况下会重新创建并展现给用户,那依附于Activity的Fragment会进行如何处理呢?
通过Android源代码可以看到,最终会通过反射无参构造实例化一个新的Fragment,并且给mArgments初始化为原先的值,而原来的Fragment实例的数据都丢失了,并重新进行了初始化。
通过上面的分析,我们可以知道Activity重新创建时,会重新构建它所管理的Fragment,原先的Fragment的字段值将会全部丢失,但是通过Fragment.setArguments(Bundle bundle)方法设置的bundle会保留下来。所以尽量使用Fragment.setArguments(Bundle bundle)方式来传递参数
0 0
- Fragment中传递参数推荐用Fragment.setArguments(Bundle bundle),而不用构造方法。
- 为什么要用Fragment.setArguments(Bundle bundle)来传递参数
- 为什么要用Fragment.setArguments(Bundle bundle)来传递参数
- 为什么要用Fragment.setArguments(Bundle bundle)来传递参数
- 为什么要用Fragment.setArguments(Bundle bundle)来传递参数
- Android Fragment传递参数Fragment.setArguments(Bundle bundle)
- Android Fragment传递参数Fragment.setArguments(Bundle bundle)
- Android解惑 - 为什么要用Fragment.setArguments(Bundle bundle)来传递参数
- Android解惑 - 为什么要用Fragment.setArguments(Bundle bundle)来传递参数
- Android解惑 - 为什么要用Fragment.setArguments(Bundle bundle)来传递参数
- Android解惑 - 为什么要用Fragment.setArguments(Bundle bundle)来传递参数
- Android解惑 - 为什么要用Fragment.setArguments(Bundle bundle)来传递参数
- Android解惑 - 为什么要用Fragment.setArguments(Bundle bundle)来传递参数
- Android解惑 - 为什么要用Fragment.setArguments(Bundle bundle)来传递参数
- Android解惑 - 为什么要用Fragment.setArguments(Bundle bundle)来传递参数
- Android解惑 - 为什么要用Fragment.setArguments(Bundle bundle)来传递参数
- Android解惑 - 为什么要用Fragment.setArguments(Bundle bundle)来传递参数
- Android解惑 - 为什么要用Fragment.setArguments(Bundle bundle)来传递参数
- 尊重个人劳动成果,如果可以请点赞,顶一下,谢谢!
- 2017上半年课程设计递推斐波那契
- java中的值传递和引用传递
- 炒蘑菇
- 百度 echarts 使用总结
- Fragment中传递参数推荐用Fragment.setArguments(Bundle bundle),而不用构造方法。
- VBA入门--变量的基础内容和知识点
- HashMap解决hash冲突的方法
- Android Xml的解析
- 自旋锁、互斥锁和信号量的区别
- Golang RPC性能测试
- 静态初始板块,构造方法和初始板块它们之间的执行优先级
- 模拟数字式时钟
- Linux(CentOS)下,下载安装Nginx并配置