Please enable JavaScript.
Coggle requires JavaScript to display documents.
Fragment Part 2 (Removing Fragments (FragmentTransaction…
Fragment Part 2
-
-
-
-
- android:visibility=”gone”
-
fragmentTransaction.replace(R.id.details_fragment, new DetailFragment(selected_index));
-
fragmentTransaction.add(R.id.ui_container, new MyListFragment());
-
-
-
- Best Practice -->
- Create layout.
- the layout use a Container View
- Add the fragments to this container.
-
-
- Fragment Transaction
- used to add, remove, replace fragments within an activity.
- Using it, we make the layout DYNAMIC -- > Change based on user interaction && app state.
- Each set of changes to the activity - not fragment- is called Transaction.
- Transaction can be Saved to allow navigate Back among fragments when clicking the Back Button.
- Fragment Transaction Steps
- Acquire an instance of the Fragment Manager
FragmentManager fg = getFragmentManager();
FragmentTransaction ft = fg.beginTransaction();
- Create new Fragment and Transaction
MyFragmnet f = new MyFragment();
ft.replace(R.id.fragmentName, f );
- Save to backStack and Commit
ft.addToBackStack(null);
ft.commit()
- Activity Stack
- Allow users to navigate back to previous screens using back button
- addToBackStack()
لما استخدمها بمقطع الفديو و عمل تراجع .. قفلت الفراقمنت بس مش الاكتفتي كمان، لكن لما ما استخدمها و عمل تراجع .. كلهم قفلوا سوا
- popBackStack()
- simulate the Back
- findFragmentById() VS findFragmentByTag
- MyFragment myFragment = (MyFragment)fragmentManager.findFragmentById(R.id.MyFragment);
- MyFragment myFragment = (MyFragment)fragmentManager.findFragmentByTag(MY_FRAGMENT_TAG);