Android开发之Fragment传递參数的几种方法
Fragment的推出让我们编写和管理用户界面更快捷更方便了。
- public class FramentTestActivity extends ActionBarActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- if (savedInstanceState == null) {
- getSupportFragmentManager().beginTransaction()
- .add(R.id.container, new TestFragment("param")).commit();
- }
- }
- public static class TestFragment extends Fragment {
- private String mArg = "non-param";
- public TestFragment() {
- Log.i("INFO", "TestFragment non-parameter constructor");
- }
- public TestFragment(String arg){
- mArg = arg;
- Log.i("INFO", "TestFragment construct with parameter");
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- 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;
- }
- }
- }
- public class FramentTest2Activity extends ActionBarActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout. activity_main);
- if (savedInstanceState == null) {
- getSupportFragmentManager().beginTransaction()
- .add(R.id. container, TestFragment.newInstance("param")).commit();
- }
- }
- public static class TestFragment extends Fragment {
- private static final String ARG = "arg";
- public TestFragment() {
- Log. i("INFO", "TestFragment non-parameter constructor" );
- }
- public static Fragment newInstance(String arg){
- TestFragment fragment = new TestFragment();
- Bundle bundle = new Bundle();
- bundle.putString( ARG, arg);
- fragment.setArguments(bundle);
- return fragment;
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- 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;
- }
- }
- }
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdHVfYmluZ2Jpbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" style="border:none; max-width:100%">
- protected void onCreate(Bundle savedInstanceState) {
- if (DEBUG_LIFECYCLE ) Slog.v( TAG, "onCreate " + this + ": " + savedInstanceState);
- if (mLastNonConfigurationInstances != null) {
- mAllLoaderManagers = mLastNonConfigurationInstances .loaders ;
- }
- if (mActivityInfo .parentActivityName != null) {
- if (mActionBar == null) {
- mEnableDefaultActionBarUp = true ;
- } else {
- mActionBar .setDefaultDisplayHomeAsUpEnabled( true);
- }
- }
- if (savedInstanceState != null) {
- Parcelable p = savedInstanceState.getParcelable( FRAGMENTS_TAG );
- mFragments .restoreAllState(p, mLastNonConfigurationInstances != null
- ? mLastNonConfigurationInstances .fragments : null);
- }
- mFragments .dispatchCreate();
- getApplication().dispatchActivityCreated( this , savedInstanceState);
- mCalled = true ;
- }
- for (int i=0; i<fms.mActive.length; i++) {
- FragmentState fs = fms.mActive[i];
- if (fs != null) {
- Fragment f = fs.instantiate(mActivity, mParent);
- if (DEBUG) Log.v(TAG, "restoreAllState: active #" + i + ": " + f);
- mActive.add(f);
- // Now that the fragment is instantiated (or came from being
- // retained above), clear mInstance in case we end up re-restoring
- // from this FragmentState again.
- fs.mInstance = null;
- } else {
- mActive.add(null);
- if (mAvailIndices == null) {
- mAvailIndices = new ArrayList<Integer>();
- }
- if (DEBUG) Log.v(TAG, "restoreAllState: avail #" + i);
- mAvailIndices.add(i);
- }
- }
- public Fragment instantiate(Activity activity, Fragment parent) {
- if (mInstance != null) {
- return mInstance ;
- }
- if (mArguments != null) {
- mArguments .setClassLoader(activity.getClassLoader());
- }
- mInstance = Fragment.instantiate(activity, mClassName , mArguments );
- if (mSavedFragmentState != null) {
- mSavedFragmentState .setClassLoader(activity.getClassLoader());
- mInstance .mSavedFragmentState = mSavedFragmentState ;
- }
- mInstance .setIndex(mIndex , parent);
- mInstance .mFromLayout = mFromLayout ;
- mInstance .mRestored = true;
- mInstance .mFragmentId = mFragmentId ;
- mInstance .mContainerId = mContainerId ;
- mInstance .mTag = mTag ;
- mInstance .mRetainInstance = mRetainInstance ;
- mInstance .mDetached = mDetached ;
- mInstance .mFragmentManager = activity.mFragments;
- if (FragmentManagerImpl.DEBUG) Log.v(FragmentManagerImpl.TAG,
- "Instantiated fragment " + mInstance );
- return mInstance ;
- }
- public static Fragment instantiate(Context context, String fname, Bundle args) {
- try {
- Class<?
> clazz = sClassMap .get(fname);
- if (clazz == null) {
- // Class not found in the cache, see if it's real, and try to add it
- clazz = context.getClassLoader().loadClass(fname);
- sClassMap .put(fname, clazz);
- }
- Fragment f = (Fragment)clazz.newInstance();
- if (args != null) {
- args.setClassLoader(f.getClass().getClassLoader());
- f. mArguments = args;
- }
- return f;
- } catch (ClassNotFoundException e) {
- throw new InstantiationException( "Unable to instantiate fragment " + fname
- + ": make sure class name exists, is public, and has an"
- + " empty constructor that is public" , e);
- } catch (java.lang.InstantiationException e) {
- throw new InstantiationException( "Unable to instantiate fragment " + fname
- + ": make sure class name exists, is public, and has an"
- + " empty constructor that is public" , e);
- } catch (IllegalAccessException e) {
- throw new InstantiationException( "Unable to instantiate fragment " + fname
- + ": make sure class name exists, is public, and has an"
- + " empty constructor that is public" , e);
- }
通过上面的分析,我们能够知道Activity又一次创建时,会又一次构建它所管理的Fragment,原先的Fragment的字段值将会所有丢失,可是通过Fragment.setArguments(Bundle bundle)方法设置的bundle会保留下来。所以尽量使用Fragment.setArguments(Bundle bundle)方式来传递參数
Android开发之Fragment传递參数的几种方法的更多相关文章
- android开发之Fragment加载到一个Activity中
Fragments 是android3.0以后添加的.主要是为了方便android平板端的开发.方便适应不同大小的屏幕.此代码是为了最简单的Fragment的使用,往一个Activity中添加Frag ...
- Android开发之Fragment的介绍、使用及生命周期
Fragment官网介绍-http://developer.android.com/guide/components/fragments.html 郭大神的使用实例文章:http://blog.csd ...
- Android开发之DatePickerDialog与TimePickerDialog的功能和使用方法具体解释
DatePickerDialog与TimePickerDialog的功能比較简单,使用方法也非常easy.仅仅要以下两步就可以. Ø 通过newkeyword创建DatePickerDialog.T ...
- 【Jquery】jQuery获取URL參数的两种方法
jQuery获取URL參数的关键是获取到URL,然后对URL进行过滤处理,取出參数. location.href是取得URL.location.search是取得URL"?"之后的 ...
- Android开发之Fragment
一.Fragment生命周期: 二.动态添加Fragment的三步: 1.获得Fragment的管理者FragmentManager FragmentManager fragmentManager = ...
- Android开发之onMeasure(int widthMeasureSpec, int heightMeasureSpec)方法
onMeasure()函数由包含这个View的具体的ViewGroup调用,因此值也是由其ViewGroup中传入的.子类View的这两个参数widthMeasureSpec, heightMeasu ...
- Android开发之ViewPager+ActionBar+Fragment实现响应式可滑动Tab
今天我们要实现的这个效果呢,在Android的应用中十分地常见,我们可以看到下面两张图,无论是系统内置的联系人应用,还是AnyView的阅读器应用,我们总能找到这样的影子,当我们滑动屏幕时,Tab可 ...
- Android开发之TextView高级应用
Android开发之TextView高级应用 我们平时使用TextView往往让它作为一个显示文字的容器,但TextView的功能并不局限于此.以下就和大家分享一下TextView的一些使用技巧. A ...
- android开发之 Wifi的四个类
android开发之 Wifi的四个类 在Android中对Wifi操作,android本身提供了一些实用的包,在android.net.wifi包以下.简介一下: 大致能够分为四个基本的类ScanR ...
随机推荐
- Node.js:Express 框架
Express 是一个简洁而灵活的 node.js Web应用框架, 提供了一系列强大特性帮助你创建各种 Web 应用,和丰富的 HTTP 工具.使用 Express 可以快速地搭建一个完整功能的网站 ...
- RS报表自动适应屏幕分辨率大小
问题:同一个报表,由于用户电脑显示器大小,分辨率大小不同显示的不一样,看起来不完整或者很不协调 原因:设计报表大小属性的时候使用了固定值属性,比如限制为宽:1200px 那么在电脑屏幕小分辨率很小的情 ...
- PHP FTP函数
PHP FTP 函数 PHP FTP 简介 FTP 函数通过文件传输协议 (FTP) 提供对文件服务器的客户端访问. FTP 函数用于打开.登录以及关闭连接,同时用于上传.下载.重命名.删除及获取文件 ...
- 判断IE浏览器版本的精简脚本
IE浏览器不管是什么版本,总是跟Web标准有些不太兼容.对于代码工作者来说,自然是苦不堪言,为了考虑IE的兼容问题,不管是写 CSS 还是 JS,往往都要对 IE 特别对待,这就少不了做些判断.本文不 ...
- Unity3D Android播放视频
http://blog.csdn.net/awnuxcvbn/article/details/17719789 using UnityEngine; using System.Collections; ...
- Effective java读书札记第一条之 考虑用静态工厂方法取代构造器
对于类而言,为了让client获取它自身的一个实例,最经常使用的方法就是提供一个共同拥有的构造器. 另一种放你发,也应该子每一个程序猿的工具箱中占有一席之地.类能够提供一个共同拥有的静态 工厂方法.它 ...
- 算法笔记_136:交替字符串(Java)
目录 1 问题描述 2 解决方案 1 问题描述 输入三个字符串s1.s2和s3,判断第三个字符串s3是否由前两个字符串s1和s2交错而成且不改变s1和s2中各个字符原有的相对顺序. 2 解决方案 ...
- taro 填坑之路(一)taro 项目回顾
(1)像素写法 PX -- 大写,否则会自动成rem (2)拿取列表第一条数据 let { activity:[firstItem] } = this.state; (3)使用props 需要设置默认 ...
- LOL 战斗力查询
LOL(英雄联盟) 战斗力查询 接口:http://lolbox.duowan.com/playerDetail.php?serverName=serverName&playerName=pl ...
- UISearchbar去除背景色的方法,适合iOS5/6/7/8.0beta
通常使用UISearchbar都须要去除其背景色来与自己的界面风格保持协调,可是UISearchbar的设计随着iOS版本号的升级不断地在发生着变化.以下我们通过分析UISearchbar在各个iOS ...