URL : http://abhiandroid.com/ui/fragment-lifecycle-example-android-studio.html
Fragment means isolated or incomplete part of something.
Android introduced fragments in Android 3.0 (API level 11), primarily to support more dynamic and flexible UI designs on large screens
Some of feature are -
- Modularity: Dividing complex activity code across fragments for better organization and maintenance.
- Reusability: Placing behavior or UI parts into fragments that can be shared across multiple activities.
- Adaptability: Representing sections of a UI as different fragments and utilizing different layouts depending on screen orientation and size.
Method of Fragment
1. onAttach()
2. onCreate()
3. onCreateView()
4. onViewCreated()
5. onActivityCreated()
6. onStart()
7. onResume()
8. onPause()
9. onStop()
10. onDestroyView()
11. onDestroy()
12. onDetach()
-> LifeCycle method of Fragment
Case 1 : When Activity created and fragment attach.
1. First Activity onCreate() event
2. first Fragment onAttach() event
3. First Activity onAttachFragment() event
4. first Fragment onCreate() event
5. first Fragment onCreateView() event
6. first Fragment onViewCreated() event
7. first Fragment onActivityCreated() event
8. first Fragment onStart() event
9. First Activity onStart() event
10. First Activity onPostCreate() event
11. First Activity onResume() event
12. first Fragment onResume() event
13. First Activity onPostResume() event
Case 2 : When one fragment replace another fragment.
1. First Fragment onPause() event
2. First Fragment onStop() event
3. First Fragment onDestroyView() event
4. First Fragment onDestroy() event
5. First Fragment onDetach() event
6. Second Fragment onAttach() event
7. First Activity onAttachFragment() event
8. Second Fragment onCreate() event
9. Second Fragment onCreateView() event
10. Second Fragment onViewCreated() event
11. Second Fragment onActivityCreated() event
12. Second Fragment onStart() event
13. Second Fragment onResume() event
Case 3 : Remove fragment.
Fragment onPause() event
Fragment onStop() event
Fragment onDestroyView() event
Fragment onDestroy() event
Fragment onDetach() event
-> Fragment Transaction
STATIC
Case 1 : Add static fragment
<fragment
android:name="slider.app.com.appsfortest.FirstFragment"
android:id="@+id/idFirstFragment"
android:layout_width="match_parent"
android:layout_height="300dp"/>
DYNAMIC
Case 1 : Adding Fragment
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.idContainer, firstFragment);
fragmentTransaction.commit();
ShortCut
getSupportFragmentManager().beginTransaction().add(R.id.idContainer, firstFragment).commit();
Case 2 : Replacing Fragment
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.idContainer, secondFragment);
fragmentTransaction.commit();
ShortCut
getSupportFragmentManager().beginTransaction().replace(R.id.idContainer, secondFragment).commit();
Case 3 : Remove Fragment
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(secondFragment);
fragmentTransaction.commit();
ShortCut
getSupportFragmentManager().beginTransaction().remove(secondFragment).commit();
-> Fragment to AddToBackStack() method
Definition : Re-attach a fragment after it had previously been detached from the UI with detach(Fragment). This causes its view hierarchy to be re-created, attached to the UI, and displayed.
Parameter - An optional name for this back stack state, or null.
Case 1 : null parameter
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.idContainer, new FirstFragment());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Case 2 : With parameter and tag
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.idContainer, new FirstFragment(), "FirstFragment");
fragmentTransaction.addToBackStack("TAG");
fragmentTransaction.commit();
-> Find Fragment By Tag
Fragment fragment = fragmentManager.findFragmentByTag("firstFragment");
-> PopStack/Remove Fragment By Tag
fragmentManager.popBackStack("firstFragment",FragmentManager.POP_BACK_STACK_INCLUSIVE);
-> Hide fragment
Hides an existing fragment. This is only relevant for fragments whose views have been added to a container, as this will cause the view to be hidden.
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.hide(secondFragment);
fragmentTransaction.commit();
-> Show fragment
Shows a previously hidden fragment. This is only relevant for fragments whose views have been added to a container, as this will cause the view to be shown.
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.show(secondFragment);
fragmentTransaction.commit();
-> isAddToBackStackAllowed
Returns true if this FragmentTransaction is allowed to be added to the back stack. If this method would return false, addToBackStack(String) will throw IllegalStateException.
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.idContainer, new FirstFragment());
fragmentTransaction.addToBackStack(null);
Log.d(tag, "isAddToBackStackAllowed : "+fragmentTransaction.isAddToBackStackAllowed());
fragmentTransaction.commit();
-> isEmpty
true if this transaction contains no operations, false otherwise.
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.idContainer, new FirstFragment());
fragmentTransaction.addToBackStack(null);
Log.d(tag, "isAddToBackStackAllowed : "+fragmentTransaction.isEmpty());
fragmentTransaction.commit();
-> setReorderingAllowed(boolean).
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.idContainer, new FirstFragment());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.setReorderingAllowed(true);
fragmentTransaction.commit();
22
No comments:
Post a Comment