Android App Components – Activities, Fragments and Intents

Hello everyone, Welcome back to Android Hunger. Here I am going to tell you about some of the main Android App Components like Activities, Fragments, and Intents.

Activities :

  • Activity is one of the most important component for any android app.
  • Activities are the User Interface (UI) screens which user see.
  • It is similar to the main() function in different programming languages.
  • Its is the main entry point for user interaction.
  • You can have multiple activities in your app.
  • All your activities must be declared in the manifest file, with their attributes.

Every activity has different functions throughout its life, onCreate(), onStart(), onResume(), onPause(), onStop(), onRestart(), onDestroy().

Below is the image from android’s documentation, which clearly shows the lifecycle of an android activity.

Android app components : activity, Fragment, Intent - androidhunger.com

Android app components: Activity, Fragment, Intent – androidhunger.com

Project on Github

Fragments :

In most of the applications these days, fragments are largely used.

As there are a lot of android devices with different resolutions, its a bit tough to handle all of those, that’s where fragments come handy. We can combine 2 or more fragments and show them in an activity.

A Fragment is a component that is used by an activity.

Even though it is used by an activity, it has its own lifecycle.

Android app components : activity, Fragment, Intent - androidhunger.com

Android app components: activity, Fragment, Intent – androidhunger.com

There are also some different fragments which you can extend: DialogFragment, ListFragment, PreferenceFragment.

Project on Github

Intents :

Intent is one of the most important and most used app component of an android application.

Using Intents, you call to other app components or to other activity or also call other applications on your phone.

Intents are two types:

Explicit Intents  where you call another activity or something with a class name. For instance, you can call another activity when some action happened in one activity. So you here explicitly specifies which activity to call.

Implicit Intents where we do not specify a class name but specify some sort of action, which can be handled by some other inbuilt apps or some other apps. For instance, you may want to open a camera, showing a map, sending emails etc. Here you don’t directly call camera app or map app, you will just specify the action.

Project on Github

Example

Below is a simple application which has one button in one activity and when clicked to goes to other activity using intent and in the other activity, two fragments are shown.

activity_main.xml

Here we have only one button,

 

MainActivity.java

In MainActivity I just initialize the button and set a click listener to it to call SecondActivity, this moving from one activity to another activity here is done using an Intent.

startActivity(new Intent(MainActivity.this, SecondActivity.class));

 

top_fragment.xml and bottom_fragment.xml

I want to show two fragments in the second activity, so I need to create two layouts for the fragments, pretty simple layouts with a text view in each layout.

 

TopFragment.java and BottomFragment.java

For the fragments, they need a class, so I created a class which extends Fragment class and simply set its content to the layout files I just created above.

 

activity_second.xml

In the second activity I just wanted to show two fragments which I just created above, so in its layout file, I will add the two fragments.

Screenshots

Android app components : activity, Fragment, Intent - androidhunger.com

Android app components: activity, Fragment, Intent – androidhunger.com

Android app components : activity, Fragment, Intent - androidhunger.com

Android app components: activity, Fragment, Intent – androidhunger.com

Project on Github

However you can understand about these more deeply when you actually implement them in your app, in my next posts I will tell about creating apps and we will create many android applications, so stay tuned and happy learning.

Feel free to look at my previous post on different android UI layouts.

Leave a Reply

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