Learn about the Transaction Fragment Manager

In the last Article you saw FrameLayout which helps to replace a Fragment. Now to do that you will need FragmentTransaction.

You can replace the Fragment at Runtime using FragmentTransaction.

Let’s see how.

Refer to the following code snippet which is taken from the code available here (https://github.com/ankur-srivastava/EmployeeApp/blob/master/app/src/main/java/com/edocent/employeeapp/MainActivity.java)

//Start Fragment Transaction
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.detailFragmentId, employeeDetailFragment);
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();

The first line of code is used to get FragmentTransaction from FragmentManager. Once you have this you can write the code to replace the fragment.

This includes the replace method where you replace the FrameLayout ID with the new Fragment object.

There are other attributes which can be set using the FragmentTransaction object. Like the Transition attribute. For more details please refer to the Android Documentation.

Finally the commit method is used to complete the transaction.

Using this you will be able to replace the Fragment with a new object. This will be useful when the user will click on the back button.

Author: androidsmartly

I am a Tech Enthusiast who likes to work on new Ideas. Sharing knowledge on Android, Marketing, Startups and More.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s