0% found this document useful (0 votes)
2 views16 pages

Fragment Back Stack

The document explains the use of FragmentManager and Fragment transactions in Android, detailing how to manage multiple fragments and their interactions. It covers the importance of back stack behavior, saving fragment state, and persistent fragments during configuration changes. Additionally, it discusses communication between fragments using ViewModel and interfaces, as well as methods for starting activities and handling results from them.

Uploaded by

jasinjoji6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views16 pages

Fragment Back Stack

The document explains the use of FragmentManager and Fragment transactions in Android, detailing how to manage multiple fragments and their interactions. It covers the importance of back stack behavior, saving fragment state, and persistent fragments during configuration changes. Additionally, it discusses communication between fragments using ViewModel and interfaces, as well as methods for starting activities and handling results from them.

Uploaded by

jasinjoji6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Android Fragment Manager and

Fragment Transactions
▪A FragmentManager manages Fragments in Android,
specifically it handles transactions between fragments.
▪When there are several fragments and we want to manipulate
several of them at the same time under one single operation, that
kind of interaction can be supported with the fragment
transaction in android
▪It includes add,remove,replace fragments,commit the transaction
Fragment Transactions and the
Fragment Back Stack
▪By default fragments have no awareness of back button
▪In android, whenever we navigate from one activity to another, the previous
activity is not completely destroyed ,it is simply added to a stack from where
we can access it again

Fragment Fragment
Activity 1 A B
expectation
Activity 2

User presses back button

• Fragments do not added to the back stack by default


Fragment Back stack behaviour
Call addToBackStack() before calling commit on a transaction
1.FragmentManager fragmentManager = getFragmentManager();
2.FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
3.fragmentTransaction.replace(R.id.fragment_container, mFeedFragment);
4.fragmentTransaction.addToBackStack(null);
5.fragmentTransaction.commit
Fragment Back stack behaviour
Code explained
1.Return the FragmentManager for interacting with fragments associated with this
activity.
2.Start a series of edit operations on the Fragments associated with this
FragmentManager.
3.Replaces the current fragment with the mFeedFragment on the layout with the id:
R.id.fragment_container
4.Add this transaction to the back stack. This means that the transaction will be
remembered after it is committed, and will reverse its operation when later popped
off the stack. The parameter name Is an optional name for this back stack state, or
null.
Programatically simulate back operation by calling popBackStack()
Saving Fragment State
▪Fragments have a onSaveInstanceState() method which is called when their
state needs to be saved
▪It comes under the class Fragment.SavedState
▪ We can pass this method a fragment, and it returns an object representing
the state of that fragment.
▪We can then use that object when initializing a fragment, using Fragment’s
setInitialSavedState( ) method
Persistent Fragments
▪When an Activity starts a fragment and soon after the user rotates the screen, causing
the Activity to be destroyed and recreated.
▪When configurations changed during run time (such as screen orientation, keyboard
availability, and language), Android usually destroys application’s existing Activity or
Fragment and recreate it.
▪Android does this so that application can reload its resources based on the new
configuration
▪The restart behavior helps application to adapt new configurations by automatically
reloading the application with alternative resources that match the new device
configuration.
▪To handle these configuration changes, Android provides callbacks to save your
application state before destroying either Activity or Fragment. In the same it also
provides to restore the application state when it is recreating them.
▪However, there is a trick that can make a Fragment much more persistent than its
normally is and some use this to avoid all of the work we have just described.
▪The RetainInstance property can be used to ask the system not to destroy the
Fragment, even when the Activity is destroyed for a configuration change.
▪ Instead all that happens is that the Fragment is detached from the Activity and
left in memory.
▪ When the Activity is recreated the Fragment is reAttached.
▪This means that the entire state of the Fragment is preserved, only the onAttach
and onDetach events occur and the onCreateView is called to restore the Activity's
View state.
Communications with Fragments

▪The communication between fragments should not be done directly. There are two
ways of doing so.
▪ The Fragment library provides two options for communication: a shared
ViewModel and the Fragment Result API
View Model
▪ViewModel is a class that is used to store and manage UI-related data.
▪By Using ViewModel application will have some consistent data even if there is a
change in UI of the application.
▪One common scenario of data sharing can be one Fragment taking information for
the user and the other Fragment displaying the entered information.
▪Here ViewModel Class can be used as a communicator between these Fragments.
▪ The first fragment i.e. the fragment taking the information from the user will store
data into the ViewModel and the second fragment i.e. the fragment showing the
information of the user will collect the data from the ViewModel.
Interface
▪Basically we will be having one Activity and in that activity, we will be having two
Fragments. Our aim is to send the data from one Fragment to the other with the help
of Interface.
▪ Make an Interface in your FragmentA
▪ Implement the Interface of the FragmentA in your Activity
▪ Call the Interface method from your Activity
▪ In your Activity, call your FragmentB to do the required changes
startActivity()
▪Launch a new activity from fragment
▪The startActivity(Intent) method is used to start a new activity, which will be
placed at the top of the activity stack. It takes a single argument, an Intent, which
describes the activity to be executed.
▪Sometimes you want to get a result back from an activity when it ends.
▪ For example, you may start an activity that lets the user pick a person in a list of
contacts; when it ends, it returns the person that was selected.
▪To do this, call the startActivityForResult(Intent, int) version with a second
integer parameter identifying the call.
▪ The result will come back through onActivityResult(int,int,intent) method.
startActivity()
private static final int REQUEST_PICK_IMAGE = 1;
Intent pickImageIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.
Media. EXTERNAL_CONTENT_URI);
startActivityForResult(pickImageIntent, REQUEST_PICK_IMAGE);
…..
protected void onActivityResult(int requestcode, int resultcode,Intent data)
{
If(requestCode== REQUEST_PICK_IMAGE){
If(resultCode==RESULT_OK){
//perform the actions
}}
setTargetFragment()
▪public void setTargetFragment (Fragment fragment, int requestCode)
▪Optional target for this fragment.
▪setTargetFragment(target) lets the “called” fragment know where to send the result

You might also like