- 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()方法。
- 必须最后调用 commit().
- 如果添加多个fragment到同一个容器, 那么添加的顺序决定了它们在view hierarchy中显示的顺序.
相关推荐
c++ wince sdk AI <br>smartphone版本 和windows版本 <br>目录 Table of content I. Introduction....................................................................................................
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 ...
• 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 ...
其他今年加入到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 ...
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...
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. ...
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 ...
Introduction, Integrated Services model, Coding of multimedia information, Reference Implementation, Traffic control, Packet scheduler, Buffers management, Packets classification, Access control, ...
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 ...
10 Fragment Operations: The End of the Pipeline . .......................................391 11 Advanced Shader Usage . ...........................................................................411 ...
12 Elements of Software Configuration Management. . . . . . . . . . . . . . . 12 Component Identification. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Version ...