Android Fragments

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!


Introduction about Fragments and their intercommunication mechanism


  • void onActivityCreated(Bundle savedInstanceState) // Called when the fragment's activity has been created and this fragment's view hierarchy instantiated.

  • void onActivityResult(int requestCode, int resultCode, Intent data) // Receive the result from a previous call to startActivityForResult(Intent, int).

  • void onAttach(Activity activity) // This method was deprecated in API level 23. Use onAttach(Context) instead.

  • void onAttach(Context context) // Called when a fragment is first attached to its context.

  • void onAttachFragment(Fragment childFragment) // Called when a fragment is attached as a child of this fragment.

  • void onConfigurationChanged(Configuration newConfig) // Called by the system when the device configuration changes while your component is running.

  • void onCreate(Bundle savedInstanceState) // Called to do initial creation of a fragment.

  • View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) // Called to have the fragment instantiate its user interface view.

  • void onDestroy() // Called when the fragment is no longer in use.

  • void onDestroyView() // Called when the view previously created by onCreateView(LayoutInflater, ViewGroup, Bundle) has been detached from the fragment.

  • void onDetach() // Called when the fragment is no longer attached to its activity.

  • void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState) // This method was deprecated in API level 23. Use onInflate(Context, AttributeSet, Bundle) instead.

  • void onInflate(Context context, AttributeSet attrs, Bundle savedInstanceState) // Called when a fragment is being created as part of a view layout inflation, typically from setting the content view of an activity.

  • void onPause() // Called when the Fragment is no longer resumed.

  • void onResume() // Called when the fragment is visible to the user and actively running.

  • void onSaveInstanceState(Bundle outState) // Called to ask the fragment to save its current dynamic state, so it can later be reconstructed in a new instance of its process is restarted.

  • void onStart() // Called when the Fragment is visible to the user.

  • void onStop() // Called when the Fragment is no longer started.

  • void onViewStateRestored(Bundle savedInstanceState) // Called when all saved state has been restored into the view hierarchy of the fragment.


A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).


Every fragment must have an empty constructor, so it can be instantiated when restoring its activity's state. It is strongly recommended that subclasses do not have other constructors with parameters, since these constructors will not be called when the fragment is re-instantiated; instead, arguments can be supplied by the caller with setArguments(Bundle) and later retrieved by the Fragment with getArguments().

Got any Android Question?