Fragment Back Stack
Fragment Back Stack
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
▪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