`
wcgdonot
  • 浏览: 89029 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Fragment Management

阅读更多
  • FragmentManager

  使用FragmentManager管理Activity中的fragments,调用Activity中的getFragmentManager()方法。

  因为FragmentManager的API是在Android 3.0,也即API level 11开始引入的,所以兼容之前的版本,需要使用support library中的FragmentActivity,并且使用getSupportFragmentManager()方法。

 

 得到Activity中存在的fragment:使用findFragmentById()或findFragmentByTag()方法。

 

popBackStack():将back stack中最后一次的fragment转换弹出。如果没有可以出栈的东西,返回false。

这个函数是异步的:它将弹出栈的请求加入队列,但是这个动作直到应用回到事件循环才会执行。

 

为back stack加上监听器:addOnBackStackChangedListener()

 

  • Performing Fragment Transactions

  使用Fragment时,可以通过用户交互来执行一些动作,比如增加、移除、替换等。所有这些改变构成一个集合,这个集合被叫做一个transaction。

  可以调用FragmentTransaction中的方法来处理这个transaction,并且可以将transaction存进由activity管理的back stack中,这样用户就可以进行fragment变化的回退操作。

  可以这样得到FragmentTransaction类的实例: 

 

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

  每个transaction是一组同时执行的变化的集合。

  用add(), remove(), replace()方法,把所有需要的变化加进去,然后调用commit()方法,将这些变化应用。

  在commit()方法之前,你可以调用addToBackStack(),把这个transaction加入back stack中去,这个back stack是由activity管理的,当用户按返回键时,就会回到上一个fragment的状态。

  比如下面的代码就是用一个新的fragment取代之前的fragment,并且将前次的状态存储在back stack中。

 

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
 
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
 
// Commit the transaction
transaction.commit();

  在这个例子中,newFragment将取代在R.id.fragment_container容器中的fragment,如果没有,将直接添加新的fragment。

  通过调用addToBackStack(),commit()的一系列转换作为一个transaction被存储在back stack中,用户按Back键可以返回上一个转换前的状态。

  当你移除一个fragment的时候,如果commit()之前没有调用addToBackStack(),那个fragment将会是destroyed;如果调用了addToBackStack(),这个fragment会是stopped,可以通过返回键来恢复。

      如果添加多个变化到事务(例如add()或remove())并调用addToBackStack(), 然后在你调用commit()之前的所有应用的变化会被作为一个单个事务添加到后台堆栈, BACK按键会将它们一起回退.

 

关于commit()方法

  调用commit()方法并不能立即执行transaction中包含的改变动作,commit()方法把transaction加入activity的UI线程队列中。

  但是,如果觉得有必要的话,可以调用executePendingTransactions()方法来立即执行commit()提供的transaction。

  这样做通常是没有必要的,除非这个transaction被其他线程依赖。

  注意:你只能在activity存储它的状态(当用户要离开activity时)之前调用commit(),如果在存储状态之后调用commit(),将会抛出一个异常。

这是因为当activity再次被恢复时commit之后的状态将丢失。如果丢失也没关系,那么使用commitAllowingStateLoss()方法。

 

 

添加变化到 FragmentTransaction的顺序不重要, 除以下例外:
  • 必须最后调用 commit().
  • 如果添加多个fragment到同一个容器, 那么添加的顺序决定了它们在view hierarchy中显示的顺序.
当执行一个移除fragment的事务时, 如果没有调用 addToBackStack(), 那么当事务提交后, 那个fragment会被销毁,并且用户不能导航回到它. 有鉴于此, 当移除一个fragment时,如果调用了 addToBackStack(), 那么fragment会被停止, 如果用户导航回来,它将会被恢复.
提示: 对于每一个fragment事务, 你可以应用一个事务动画, 通过在提交事务之前调用setTransition()实现.
调用 commit() 并不立即执行事务.恰恰相反, 它将事务安排排期, 一旦准备好, 就在activity的UI线程上运行(主线程).如果有必要, 无论如何, 你可以从你的UI线程调用 executePendingTransactions() 来立即执行由commit()提交的事务. 但这么做通常不必要, 除非事务是其他线程中的job的一个从属.
分享到:
评论

相关推荐

    手机游戏开发实例(smartphone联机炸弹人)

    c++ wince sdk AI <br>smartphone版本 和windows版本 <br>目录 Table of content I. Introduction....................................................................................................

    Inside ASP.NET

    Fragment Caching (Partial Page Caching) Request Caching Summary 15. Creating User and Custom Controls for ASP.NET User Controls Introduction to Custom Controls Summary 16. Mobile ...

    OpenGL SuperBible 5th Edition.pdf

    • Fragment operations: controlling the end of the graphics pipeline • Advanced shader usage and geometry management • A fully updated API reference, now based on the official ARB (Core) OpenGL ...

    Cisco_IOS_Router_Exploitation.pdf

    其他今年加入到IOS的服务包括VoIP, SSL VPN, 包过滤Web Service Management Agent(SOAP),XML-PI和H.323 Client Side Vulnerabilities:But up until now, client side vulnerabilities have not played any role ...

    AW.OpenGL.SUPERBIBLE.5th.Edition.Comprehensive.Tutorial.and.Reference.2011.pdf

    10 Fragment Operations: The End of the Pipeline . .......391 11 Advanced Shader Usage . ...........411 12 Advanced Geometry Management . ..........471 PART III Platform-Specific Notes .............539...

    openGL超级宝典(第四版).pdf

    After 12 years of OpenGL driver development and driver team management at DEC, Compaq, and ATI, he is headed for smaller pastures. Benj recently became manager of AMD’s handheld software team. ...

    Introduction.to.Android.Application.Development(4th,2013.12) pdf

    Practicing Good File Management 302 Understanding Android File Permissions 303 Working with Files and Directories 303 Exploring with the Android Application Directories 304 Working with Other ...

    Internetworking IPv6 with Cisco Routers

    Introduction, Integrated Services model, Coding of multimedia information, Reference Implementation, Traffic control, Packet scheduler, Buffers management, Packets classification, Access control, ...

    D3D render pipeline

    use the cursor management provided by the device. Device color palettes and methods for gamma correction are presented. Part II describes the geometry processing portion of the graphics pipeline. The ...

    OpenGL超级宝典(英文第五版)

    10 Fragment Operations: The End of the Pipeline . .......................................391 11 Advanced Shader Usage . ...........................................................................411 ...

    Software Testing and Continuous Quality Improvement

    12 Elements of Software Configuration Management. . . . . . . . . . . . . . . 12 Component Identification. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Version ...

Global site tag (gtag.js) - Google Analytics