Why to use more Activities while the fragment is there

Updated 22 December 2016


“Fragments are two or more activities on the screen at the same time”.

In this blog, We have shown the how to use fragments rather than using a lot of activities.

If in your app there is hardly two to three layouts contents than you can easily use activities. But if you have a lot of content in your app to show means 20 to 25 layouts or more than this, than from my opinion you can use Fragments.

We can also see activities being used as a way to add logical flow to an application.


What is fragment?

A Fragment is a piece of an application’s user interface or behavior that can be placed in an Activity. Interaction with fragments is done through FragmentManager, which can be obtained via


The core series of lifecycle methods that are called to bring a fragment up to resumed state (interacting with the user) are:

  1. onAttach(Activity) called once the fragment is associated with its activity.
  2. onCreate(Bundle) called to do the initial creation of the fragment.
  3. onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment.
  4. onActivityCreated(Bundle) tells the fragment that its activity has completed its own. Activity.onCreate()
  5. onViewStateRestored(Bundle) tells the fragment that all of the saved states of its view hierarchy has been restored.
  6. onStart() makes the fragment visible to the user (based on its containing activity being started).
  7. onResume() makes the fragment begin interacting with the user (based on its containing activity being resumed).

As a fragment is no longer being used, it goes through a reverse series of callbacks:

  1. onPause() the fragment is no longer interacting with the user either because its activity is being paused or a fragment operation is modifying it in the activity.
  2. onStop() the fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity.
  3. onDestroyView() allows the fragment to clean up resources associated with its View.
  4. onDestroy() called to do the final cleanup of the fragment’s state.
  5. onDetach() called immediately prior to the fragment no longer being associated with its activity.

In this tut, we have made a simple fragment and how to attach that fragment to an activity.


how to make fragments?

First of all, if you are using android studio for development, then create a blank fragment by following steps:-

  1. Right Click on your project + select New,
  2. Then select Fragment, 
  3. and then select fragment (blank).

Then it gives a black fragment and a layout

Let us the name of the fragment black fragment is DemoFragment.java and fragment_demo.xml


There is a TextView inside FrameLayout. FrameLayout is a layout which is used to hold a single child view.

Why is FrameLayout always USED with Fragments?


We can basically use RelativeLayout or LinearLayout it will still work. But FrameLayout is designed to block out an area on the screen to display a single item.

You can read more about FrameLayout here: About FrameLayout


If you want to access our fragment’s view then

in your onCreateView(),

create an view named fragmentView and assign the Inflated view to fragmentView.

By fragmentView, you can access your fragment’s Views.


how to attach a fragment to an activity

In this example, I have a MainActivity.java. And a button on that. On press of a button, we will open our demoFragment.java.



We can see in above code when attachFragment button is clicked. then DemoFragment adds to the fragment_container. by following code snipped:

And if you want, on the press of the back button the previous container has loaded. Then set

and override onBackPressed()










. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


  • Jennifer
    • Aman Gupta (Moderator)
  • Start a Project

      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home