源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

浅谈Android app开发中Fragment的Transaction操作

  • 时间:2021-12-06 16:24 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:浅谈Android app开发中Fragment的Transaction操作
在Android中,对Fragment的操作都是通过FragmentTransaction来执行。而从Fragment的结果来看,FragmentTransaction中对Fragment的操作大致可以分为两类: 显示:add() replace() show() attach() 隐藏:remove() hide() detach() 对于每一组方法,虽然最后产生的效果类似,但方法背后带来的副作用以及对Fragment的生命周期的影响都不尽相同。 [b]add() vs. replace() [/b]只有在Fragment数量大于等于2的时候,调用add()还是replace()的区别才能体现出来。当通过add()连续两次添加Fragment的时候,每个Fragment生命周期中的onAttach()-onResume()都会被各调用一次,而且两个Fragment的View会被同时attach到containerView。 [img]http://files.jb51.net/file_images/article/201602/2016225165625309.png?2016125165632[/img] 同样,退出Activty时,每个Fragment生命周期中的onPause()-onDetach()也会被各调用一次。 [img]http://files.jb51.net/file_images/article/201602/2016225165647521.png?2016125165653[/img] 但当使用replace()来添加Fragment的时候,第二次添加会导致第一个Fragment被销毁,即执行第二个Fragment的onAttach()方法之前会先执行第一个Fragment的onPause()-onDetach()方法,同时containerView会detach第一个Fragment的View。 [img]http://files.jb51.net/file_images/article/201602/2016225165705852.png?2016125165713[/img] [b]show() & hide() vs. attach() & detach() [/b]调用show() & hide()方法时,Fragment的生命周期方法并不会被执行,仅仅是Fragment的View被显示或者​隐藏。而且,尽管Fragment的View被隐藏,但它在父布局中并未被detach,仍然是作为containerView的childView存在着。相比较下,attach() & detach()做的就更彻底一些。一旦一个Fragment被detach(),它的onPause()-onDestroyView()周期都会被执行。 [img]http://files.jb51.net/file_images/article/201602/2016225165731825.png?2016125165739[/img] 同时Fragment的View也会被detach。在重新调用attach()后,onCreateView()-onResume()周期也会被再次执行。 [img]http://files.jb51.net/file_images/article/201602/2016225165749522.png?2016125165758[/img] [b]remove() [/b]其实看完上面的分析,remove()方法基本也就明白了。相对应add()方法执行onAttach()-onResume()的生命周期,remove()就是完成剩下的onPause()-onDetach()周期。 [img]http://files.jb51.net/file_images/article/201602/2016225165808846.png?2016125165815[/img] [b]错误[/b] 曾经在FragmentTransaction编写时遇到过以下错误:
[u]复制代码[/u] 代码如下:
The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, MyFragment)
Fragment newfragment =new MyFragment(); fragmentTransaction.replace(R.layout.activity_main,newfragment ).commit(); 提示错误:The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, MyFragment) 原因找了好久!一直以为子类对象不能赋值给父类引用。这不科学啊! 代码时这样的:
package com.example.testforfragment;

import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.support.v4.app.Fragment.*;
import android.view.Menu;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  FragmentManager fragmentManager = getFragmentManager();
  FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  Fragment newfragment =new MyFragment();
  fragmentTransaction.replace(R.layout.activity_main,newfragment ).commit();
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

}

所以关键就在于:
Fragment newfragment =new MyFragment();
fragmentTransaction.replace(R.layout.activity_main,newfragment ).commit();
修改后:
package com.example.testforfragment;

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;

public class MainActivity extends FragmentActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  FragmentManager fragmentManager = getSupportFragmentManager();
  FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  Fragment newfragment =new MyFragment();
  fragmentTransaction.replace(R.layout.activity_main,newfragment ).commit();
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

}

  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部