Android Hundred Day Program: Fragment dynamic management and lifetime

Source: Internet
Author: User

Previously wrote the program used by fragment, fragment can be static or dynamically loaded into memory, this chapter further see how to dynamically change fragment and see what functions fragment life.


This chapter uses the response menu click events to take turns loading different fragment, displaying different interfaces, with the following effects:

The beginning is not loaded fragmen as blank:


Click on the menu next FRAGMENT VIEW to enter the next interface, loading two:


Continue to click Show Fragment 1:


Continue clicking, show Fragment2:


Then there is the loop:



This cycle shows different images.

The first key code to achieve this effect is to enter the following code in the menu response function:

@Overridepublic boolean onoptionsitemselected (MenuItem item) {int id = item.getitemid (); switch (ID) {Case r.id.next_ Fragment_view_menu_item: {Fragmentmanager Fragmanager = Getfragmentmanager (); Fragmenttransaction Fragtrans = Fragmanager.begintransaction (); if (0 = = turnnum) {Fragment1 frag1 = new Fragment1 (); Fragment2 frag2 = new Fragment2 () Fragtrans.replace (R.ID.FRAME_LAYOUT1, FRAG1); Fragtrans.replace (R.id.frame_layout2 , Frag2);} else if (1 = = Turnnum) {Fragment1 frag1 = new Fragment1 (); Fragment Frag2 = Fragmanager.findfragmentbyid (R.ID.FRAME_LAYOUT2); Fragtrans.replace (R.ID.FRAME_LAYOUT1, FRAG1); if ( Frag2 = null) fragtrans.remove (FRAG2);} else if (2 = = turnnum) {Fragment Frag1 = Fragmanager.findfragmentbyid (R.ID.FRAME_LAYOUT1); Fragment2 frag2 = new Fragment2 (); if (frag1! = null) fragtrans.remove (FRAG1); Fragtrans.replace (R.id.frame_layout2, FRAG2);} Turnnum++;if (Turnnum > 2) turnnum = 0;fragtrans.addtobackstack (null); Fragtrans.commit (); return true;}} Return Super.onoptionsitemselectEd (item);} 



1 Fragmentmanager is a class that manages all Fragment, so look for existing Fragment usage functions: Fragment frag2 = fragmanager.findfragmentbyi ( R.ID.FRAMELAYOUT2); Note to determine whether to retrieve fragment, if not then frag2 = = null.

2 fragmenttransaction Management Fragment change transactions, all actions, such as remove in the example, replace, must be between BeginTransaction () and commit () will not take effect, Repeated calls to commit without calling BeginTransaction program crashes.

3 Addtobackstack (NULL), is to manually put fragment on the stack, so you can use the Back button to return to the previous layer of fragment arrangement. The activity is automatically placed in the stack.

4 Simple logic: Turnnum is a global variable that uses different fragment depending on the value of this turnnum.

The XML for the two main interfaces is as follows:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    tools:context= "bill.su.fragment.MainActivity"    android:orientation= "horizontal"    android:id= "@+id/ Linear_fragment "    android:baselinealigned=" false ">    <framelayout        android:id=" @+id/frame_ LAYOUT1 "        android:layout_weight=" 1 "        android:layout_width=" 0sp "        android:layout_height=" Match_ Parent "/>    <framelayout        android:id=" @+id/frame_layout2 "        android:layout_weight=" 1 "        Android:layout_width= "0SP"        android:layout_height= "Match_parent"/></linearlayout>

Two framelayout to fit fragment


Three-menu Res/menu/main.xml code:

<menu xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http://schemas.android.com /tools "    tools:context=" bill.su.fragment.MainActivity ">    <item         android:id=" @+id/next_fragment_ View_menu_item "        android:showasaction=" always "        android:title=" @string/next_fragment_view_menu_item "/ ></menu>

Four test fragment lifetime of all code:

Package Bill.su.fragment;import Android.app.activity;import Android.app.fragment;import android.os.Bundle;import Android.util.log;import Android.view.layoutinflater;import Android.view.view;import Android.view.ViewGroup;public Class Fragment1 extends Fragment {private final String TAG = "FRAGMENTTAG1"; @Overridepublic View Oncreateview (layoutinfla Ter Inflater, ViewGroup container,bundle savedinstancestate) {log.d (TAG, "Oncreateview called"); return Inflater.inflate (R.layout.fragment1, container, false);} @Overridepublic void Onattach (activity activity) {Super.onattach (activity); LOG.D (TAG, "Onattach called");} @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); LOG.D (TAG, "onCreate called");} @Overridepublic void onactivitycreated (Bundle savedinstancestate) {super.onactivitycreated (savedinstancestate); LOG.D (TAG, "onactivitycreted called");} @Overridepublic void OnStart () {Super.onstart (); LOG.D (TAG, "OnStart called");} @Overridepublic void Onresume () {super.Onresume (); LOG.D (TAG, "onresume called");} @Overridepublic void OnPause () {super.onpause (); LOG.D (TAG, "onPause called");} @Overridepublic void OnStop () {super.onstop (); LOG.D (TAG, "onStop called");} @Overridepublic void Ondestroyview () {Super.ondestroyview (); LOG.D (TAG, "Ondestroyview called");} @Overridepublic void OnDestroy () {Super.ondestroy (); LOG.D (TAG, "OnDestroy called");} @Overridepublic void Ondetach () {Super.ondetach (); LOG.D (TAG, "Ondetach called");}}

The main and activity differences are:

Onattached (): Fragment and activity are connected by calling

Oncreateview (): Fragment View creation

Onactivitycreated (): Called when the activity's OnCreate () call

Ondestroyview (): Called when Fragment's view is deleted

Ondetach (): fragment removed from the activity is the call




Android Hundred Day Program: Fragment dynamic management and lifetime

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.