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 ...
随机推荐
- SQL insert失败时也会造成自增长字段加1
CREATE TABLE #test(id INT IDENTITY(1,1), NAME varchar(30)) INSERT #test(name)SELECT '41545' SELECT ...
- 高性能WEB开发:深入理解页面呈现、重绘、回流
在讨论页面重绘.回流之前.需要对页面的呈现流程有些了解,页面是怎么把html结合css等显示到浏览器上的,下面的流程图显示了浏览器对页面的呈现的处理流程.可能不同的浏览器略微会有些不同.但基本上都是类 ...
- 转: Mac 使用ADT的问题
http://blog.csdn.net/wwj_748/article/details/44806253
- 【Android】读取sdcard上的图片
Android读取sdcard上的图片是很easy的事情,以下用一个样例来说明这个问题. 首先,在sdcard上有一张已经准备好的img25.jpg 以下,须要做的是把这张图片读取到app中显示. 做 ...
- 解决tmux在PuTTY下工作异常的问题
ubuntu 默认系统配置文件位置/usr/share/byobu/profiles/tmux 来自 PC通过PuTTY连接到VPS,在使用VPS上安装的tmux时遇到了一些小问题.主要是因为PuTT ...
- VS下控制台执行保持(不要一闪而过)
曾经上课的时候是用VC++6.0来学习编程的,编完打印出来的东西就直接显示在控制台上.而在Visual Studio下会出现控制台一闪而过的情况.这个问题事实上是非常好解决的.方法有多种.以下列举两种 ...
- 娓娓道来c指针 (4)解析c的声明语句
(4)解析c的声明语句 在继续探索c指针之前.有必要来解析下c语言中复杂的声明语法. 仅仅须要记住两则:一个原则,一个规则. 原则:先看标示符. 规则:运算符优先级是规则. 举例说明 1.最简单的 i ...
- Docker配置本地镜像与容器的存储位置
默认情况下Docker的存放位置为:/var/lib/docker 可以通过下面命令查看具体位置: sudo docker info | grep "Docker Root Dir" ...
- Oracle GoldenGate (ogg) 11.2.1.0.20 是最后一个支持oracle db 10g的 ogg版本号
參考原文: Oracle GoldenGate 11.2.1.0.22 Patch Set Availability (Doc ID 1669160.1) 该文章不做翻译,只摘录当中有价值的信息,例如 ...
- CMD查看进程ID并查杀进程
开始-运行,输入CMD打开命令行界面,输入命令netstat -ano 结束该进程C:\>taskkill /f /t /im Wiz.exe 根据进程ID杀 >taskkill /F / ...