Android activity and fragment

Android activity and fragment
December 2, 2019 1 Comment Android Development,Development Pushpendra Kumar

Hi Guys, Welcome here! If you are looking for the deep difference in between android activity and fragment. Then you on the correct page. So without waisting time let’s see the detailed difference both android activity and android fragment.

Difference in Android activity and fragment

An Activity is an application component that provides a screen, with which users can interact in order to do something.

Whereas a Fragment represents a behavior or a portion of user interface in an Activity.

A Fragment is a section of an Activity, which has:

  • Its own lifecycle
  • Receives its own input events
  • We can be added or removed fragment while Activity is running.

Activities are one of the fundamental building blocks of apps on the Android platform. They serve as the entry point for a user’s interaction with an app and are also central to how a user navigates within an app (as with the Back button) or between apps (as with the Recents button).

Skillfully managing activities allows you to ensure that, for example:

  • Orientation changes take place smoothly without disrupting the user experience.
  • Its own lifecycle, This worked with fragment lifecycle.
  • When we open another activity from existing activity then existing activity will remain same with the data. In conclusion activity does not lost it’s data while transition come into picture.

Some common difference in Android activity and fragment

  • Activity is an application component which give user interface where user can interect. Fragment is a part of an activity,which contibute its own UI to that activity.
  • without using fragment in Activity we can’t create multi-pane UI.but using multiple fragments in single activity we can create multi-pane UI.
  • An activity may contain 0 or multiple number of fragments. A fragment can be reused in multiple activities, so its act like a reusable component in activities.
  • For Activity we must need to mention in Manifest but for fragment its not required.

Example of Android activity and fragment

  • Create new android project as I have already described in last tutorial. And also in last video.
  • After that create copy and paste the existing code into your MainActivity.java file
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d("MainActivity", "ONCREATE");
        getSupportFragmentManager().beginTransaction().add(R.id.container, new BlankFragment()).commit();
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d("MainActivity", "ON START");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("MainActivity", "ON RESUME");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("MainActivity", "ON RESTART");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("MainActivity", "ON Pause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("MainActivity", "ON Stop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("MainActivity", "ON DESTROY");
    }
}

After writing the above code then just start your android application. Once you start your application then your activity will execute the function as given into the below image.

Activity lifecycle
Basic Life cycle of an activity

After that we will create an fragment to see the lifecycle of android fragment. So let’s begin with it.

  • Create new fragment inside your project. As I have already described in given video.
  • After that, First edit your activity_main.xml, as I did.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

That’s cool, Now replace your onCreate() method in MainActivity.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d("MainActivity", "ONCREATE");
        getSupportFragmentManager().beginTransaction().add(R.id.container, new your_fragment_name()).commit();
    }

Create now after that, Create a hierarchy of lifecycle of Android fragment into the your_fragment_name.java

public class your_fragment_name extends Fragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("BLANK_FRAGMENT", "ON CREATE");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.d("BLANK_FRAGMENT", "ON CREATE VIEW");
        return inflater.inflate(R.layout.fragment_blank, container, false);
    }

    @Override
    public void onAttach(Context context) {
        Log.d("BLANK_FRAGMENT", "ON ATTACH");
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        Log.d("BLANK_FRAGMENT", "ON DETACH");
        super.onDetach();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        Log.d("BLANK_FRAGMENT", "ON ACTIVITY CREATED");
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onStart() {
        Log.d("BLANK_FRAGMENT", "ON START");
        super.onStart();
    }

    @Override
    public void onResume() {
        Log.d("BLANK_FRAGMENT", "ON Resume");
        super.onResume();
    }

    @Override
    public void onPause() {
        Log.d("BLANK_FRAGMENT", "ON Pause");
        super.onPause();
    }

    @Override
    public void onStop() {
        Log.d("BLANK_FRAGMENT", "ON STOP");
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        Log.d("BLANK_FRAGMENT", "ON DESTROY VIEW");
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        Log.d("BLANK_FRAGMENT", "ON DESTROY");
        super.onDestroy();
    }
}

That’s great, Now your fragment will behave as per given image. And the hierarchy also will work in same manner.

fragment lifecycle
Complete lifecycle of android fragment

Conclusion of Difference in Android activity and fragment

In Conclusion, I hope it’s very clear to you that what is the difference in android activity and android fragment. And I am sure that you also understand the lifecycle of an activity with the help of given example, But If still did not understand then please go through with the video. So guys once again thank you so much fo visiting on this tutorial have a great day and great time 😎😎😎

Tags
About The Author
Pushpendra Kumar I am passionate about mobile application development and professional developer at Colour Moon Technologies Pvt Ltd (www.thecolourmoon.com). This website I have made so that I can meet with new challenges and can share here.
Leave Comment
  1. 1

    society and media

    Hello, just wanted to mention, I loved this post.
    It was practical. Keep on posting!

    Reply

Leave a reply

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