Android Fragment and androidfragment

Source: Internet
Author: User

Android Fragment and androidfragment
Lifecycle:

OnAttach, onCreate, onCreateView, onActivityCreated, onStart, onResume,

OnPause, onStop, onDestroyView, onDestroyView, onDetach

Three types:

DialogFragment

Dialog Box-based Fragments. You can add a fragments dialog box to the fragments back stack of activity management, allowing users to return to the dialog

ListFragment

Similar to ListActivity, and provides functions such as onListItemCLick and setListAdapter. Internal maintenance of a ListView

Use layout android. R. layout. list_content

PreferenceFragments

Similar to PreferenceActivity. You can create a PAD-like setting interface.


Use Fragment1. xml

<Fragment class = "com. stone. fragment. fragmentDemoActivity $ TitlesFragment "// use android: name or android: id =" @ + id/titles "android: layout_width =" 0px "android: layout_height =" match_parent "android: layout_weight = "1"/>

The Fragment derived class implements onCreateView () {inflater. inflate (... adds a layout to fragment) and returns View },

2. Add Fragment using code

FragmentManagerfragmentManager = getFragmentManager (); // 3.0 or more used in the Activity

FragmentManagerfragmentManager = getSupportFragmentManager (); // use the V4 package and FragmentActivity

FragmentTransactionfragmentTransaction = fragmentManager. beginTransaction ();

Fragment01dynamicFragment = new Fragment01 ();

FragmentTransaction. add (R. id. ll_frag_two, dynamicFragment); // There is layout --- add fragment to the layout-ll_frag_two

FragmentTransaction. add (R. id. ll_frag_three, new Fragment01 (), "kkk"); // "kkk" ----- tag

FragmentTransaction. add (dynamicFragment, "without_layout"); // No layout

FragmentTransaction. commit (); // submit. begintransaction is required for each transaction.

FragmentManager. findFragmentById ();

FragmentManager. findFragmentByTag ("tag"); // the unbounded surface can be retrieved as a unique tag.

FragmentManager. popBackStack (); // fragment pops up from the back stack of the activity (this can simulate the action triggered by the back key ). Return void

FragmentManager. addOnBackStackChangedListerner (); // register a listener to monitor changes in the back Stack

FragmentManager.exe cutePendingTransactions (); // execute the transaction immediately

FragmentTransaction. setTransition (transit); // sets the jump Animation

FragmentTransaction. addToBackStack ("options name"); // Add the previous operation to the back stack. Press the back key to bring up fragment and restore it to the previous status.

3. Transaction attention

Perform operations by adding (), remove (), replace (), and so on.

Warning: You can submit transactions only when the activity is saved, such as running, onPause () and onStop () methods. Otherwise, an exception occurs. This is because fragment

Will be lost. Use commitAllowingStateLoss () If you want to commit a transaction that may be lost ().

4. parameter transfer

Interaction with Activity. In the implementation class, getActivity (); // obtain the activity

Parameter passing between fragment: fragment. setArguments (bundle );

5. Activity and Fragment sharing events

Define the callback interface in Fragment and input it in Activity.

Stack rollback description

Example of fragmentTransaction. addToBackStack and fragmentManager. popBackStack:

Example 1

Ft. add (new asdf (), "frag1 ");

Ft. addToBackStack ("back1 ");

Ft. commit ();

Ft = fm. beginTransaction ();

Ft. add (new asdff (), "frag2 ");

Ft. commit (); // then press back to bring up the back1-frag1 in the backStack, press back again, pop up frag2

Example 2

Ft. add (new asdf (), "frag1 ");

Ft. addToBackStack ("back1 ");

Ft. commit ();

Ft = fm. beginTransaction ();

Ft. add (new asdff (), "frag2 ");

Ft. addToBackStack ("back2 ");

Ft. commit (); // press back to display back2 and then press back.

Example 3

Ft. add (new asdf (), "frag1 ");

Ft. addToBackStack ("back1 ");

Ft. commit ();

Fm. popBackStack (); // The back1 Stack has been popped up here.

Ft = fm. beginTransaction ();

Ft. add (new asdff (), "frag2 ");

Ft. addToBackStack ("back2 ");

Ft. commit (); // press back. Only back2 is displayed.




Android fragment problems

1. Do not write the Fragement directly in the XML file. This will cause the Fragment object to fail to be destroyed and cause memory overflow. In the Code dynamic new and add Fragement is the correct way.

2. Use the drawer method to put two LinearLayout lines in the XML file, and add the Fragment to the two LinearLayout lines in the code.
To move Fragment, you can move LinearLayout. You can modify the margin value to achieve gradient movement.
Remember to detach the Fragment screen when it is removed.

How does Android add listeners to controls in Fragment?

Same settings as activity
Package com. paiao. 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. View. OnClickListener;
Import android. view. ViewGroup;
Import android. widget. Button;

Public class BtFragment extends Fragment implements OnClickListener {
Private Button login;
Private Button reg;

@ Override
Public void onAttach (Activity activity ){
// TODO Auto-generated method stub
Super. onAttach (activity );
Log. I ("I", "Fragment executes onAttach ");

}
@ Override
Public void onCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );
Log. I ("I", "Fragment executes onCreate ");
}
@ Override
Public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState ){
// TODO Auto-generated method stub
Log. I ("I", "Fragment executes onCreateView ");
View view = inflater. inflate (R. layout. main1, null, false );
Login = (Button) view. findViewById (R. id. login );
Reg = (Button) view. findViewById (R. id. reg );
Login. setOnClickListener (this );
Reg. setOnClickListener (this );
Return view;
}

@ Override
Public void onActivityCreated (Bundle savedInst ...... remaining full text>

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.