Fragments Tutorial. Part 3: The parent activity

  • Written by  Clive

The parent's in control

fragments tut part3 icon

We only have one activity in our app. It hosts the fragments.

If you haven't already done so, have a look at :

We’re using the support library so make sure that you import the correct class:

The fragment's parent activity

Import the support classes

Create the activity by extending the FragmentActivity class:

Create the fragment by extending the FragmentActivity class

Extend the FragmentActivity class


We implement onCreate and initialize the activity:

Initialise the parent activity in onCreate

Initialize the activity in onCreate

Note the following:

  • setContentView – we set the layout to be used for the activity’s User Interface. The system will choose the correct layout at runtime depending on the size and orientation of the device. If it’s the dual pane layout then nothing else happens in onCreate. The layout will inflate and display the two fragments it contains
  • fragment_container – this is the xml layout file. Bear in mind that there are two versions of this file. One for a single pane layout and one for a dual pane layout
  • we use an if statement to decide what to do depending on which layout is being used. If the layout contains a view with an ID of container then we are using the single pane layout file
  • savedInstanceState – we use an if statement to check if the activity is being recreated from a saved state (after an orientation change for example). If it is then we don’t have to do anything because the system will take care of the restoration. The return causes an immediate exit from the onCreate method
  • onCreate is called once when the activity is first created. If this is the first time that we are creating the activity then we need to create and display a fragment in the placeholder
  • firstFragment – we create an instance of the ItemFragment
  • getSupportFragmentmanager – gets a Fragment Manager so that we can interact with the fragment associated with this activity
  • beginTransaction – starts an edit operation on a fragment
  • add – the transaction that we want to perform is to add a new fragment. We pass two parameters:
    • the container that we want to put the fragment into
    • our new item fragment that we want to add to the container
  • commit – schedules the commit. The transaction will be executed when the thread is ready

This interface method receives the position in the list of the selected item from the list fragment. We need to now display the corresponding image in the image fragment.

Implement the interface method in the parent activity

Display the selected item’s image in the image fragment

Note the following:

  • we use getSupportFragmentManager to find the image fragment. This is the fragment defined in the dual pane layout file. We’ll use it later to update the displayed image, if we’re currently using the dual pane. We first have to confirm this
  • we use an if statement to check if the fragment placeholder exists. If it does then it means that we are currently using the single pane layout.
  • If we’re using the single pane layout, then it is currently displaying the list fragment. We need to swop it with the image fragment so that we can display the correct image. We create a new image fragment, include a bundle that contains the selected item’s position and commit the transaction so that the new image is displayed
  • newFragment – we create a new image fragment
  • args – we create a Bundle object
  • putInt – we put the position value into the bundle
  • setArguments – we include the bundle in the fragment. The values it contains will be used to construct the new fragment
  • beginTransaction – we get the fragment manager and start the transaction
  • replace – we want to replace the current list fragment with our new image fragment
  • addToBackStack – we want the fragment added to the backstack so that the user will be able to reverse the transaction and display the previous fragment, the list fragment
  • commit – finally we call commit to finalise the transaction
  • if we’re using the dual pane layout, then we simply call updateImage() and pass it position as the parameter. The image fragment will then display the new image

Want to convert your Activities to Fragments? Check out this tutorial, Converting Android Activities to Fragments

I hope that you have found this tutorial helpful.

Please consider subscribing to our notification email. We’ll send you one email on Friday with links to our latest tutorials. That way you won’t miss out. If we didn’t publish any then we won’t send any email. No spam. 

This tutorial was created using Android Studio. You can download the project files here Download icon

Are you using Eclipse or another IDE? Here's how you can use this project's Android Studio files.