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 ...
随机推荐
- 转:mac下安装Sublime Text
转:http://blog.sina.com.cn/s/blog_559d66460101cab0.html 正版的买个license其实并不贵,定价为70美元.如果不买license,也可acces ...
- angular中定义全局变量及全局变量的使用
一个例子,定义了两个变量,并且把变量显示出来: <!DOCTYPE html> <html ng-app="myApp"> <head> < ...
- UNIX网络编程读书笔记:recvmsg和sendmsg函数
这两个函数是最通用的I/O函数.实际上我们可以把所有read.readv.recv和recvfrom调用替换成recvmsg调用.类似地,各种输出函数调用也可以替换成sendmsg调用. #inclu ...
- mysql表utf-8 字符串入库编码异常
分析:http://www.myexception.cn/mysql/639943.html 解决方法:http://blog.sina.com.cn/s/blog_3f78232201011o26. ...
- 使用CopyTable工具方法在线备份HBase表
CopyTable is a simple Apache HBase utility that, unsurprisingly, can be used for copying individual ...
- Python——Shell编程关于Sha-Bang(#!)
Q. #!的名字为什么叫Sha-Bang? A. Sha-Bang是Sharp和Bang的组合词.Sharp for #, Bang for ! 类似的情况是,C#通常被称为C Sharp Q. Sh ...
- 安卓手机使用Fiddler抓获HTTPS报文方法
Configure Fiddler Click Tools > Fiddler Options > Connections. Ensure that the checkbox by All ...
- 〖Android〗(CM10.2)Android4.2/4.3中的boot.img打包方法(合适三星exynos4 CPU)
#!/bin/bash - #=============================================================================== # # F ...
- 文字选中的js实现
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 通用的sql语句
1.插入: INSERT INTO 表名称 VALUES (值1, 值2,....) 我们也可以指定所要插入数据的列: INSERT INTO table_name (列1, 列2,...) VALU ...