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 ...
随机推荐
-
239. Sliding Window Maximum *HARD* -- 滑动窗口的最大值
Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...
-
usb库文件usb_desc.c分析
参考<圈圈教你玩USB> usb协议中使用的是小端结构,所以实际数据在传输时是低字节在先的. 设备描述符的实现: 已知每个设备都必须有且仅有一个设备描述符,它的结构在USB协议中有详细的定 ...
-
Linux 线程 条件变量
一:条件变量 直接上最基本的两个函数,先抓主要矛盾: //等待条件 int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex ...
-
系统架构师JD
#################################################################################################### ...
-
JavaScript 中的数字和日期类型
本章节介绍如何掌握Javascript里的数字和日期类型 数字EDIT 在 JavaScript 里面,数字都是双精度浮点类型的 double-precision 64-bit binary form ...
-
oracle 11g impdp时 报ORA-12899(转)
源库ZHS16BGK,汉字在数据库存放的时候占用两个字节 目标库UTF8,汉字在数据库里存放的时候占用三个字节 由于字符集不同,导致现在数据库impdp的时候有些表的字段长度不够,出现ORA-1289 ...
-
git使用手册整理
-------------------20181217------------------- git使用:在gitbash 下初始化用户: $ git config --global user.nam ...
-
springboot邮件发送与接收读取
发送邮件 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...
-
NOI2018退役记
NOI2018退役记 终于我也退役了-- Day0 高中毕业前最后一次坐飞机了--在机场干什么呢?当然是打元气打元气打元气.下飞机干什么呢?当然是打元气打元气打元气. 有接机服务,大巴上有个导游,又向 ...
-
pivot 与 unpivot函数
pivot 与 unpivot函数 pivot 与 unpivot 函数是SQL05新提供的2个函数 灰常灰常的实用 ----------------------------------------- ...