Android App Components — Activities, Fragments and Intents

Avinash Nethala
Android Hunger
Published in
4 min readJan 17, 2017

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

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.

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.

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,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
...
....>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to another activity"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>

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));

package androidhunger.activityfragmentintentdemo;import android.content.Intent;
...
public class MainActivity extends AppCompatActivity {@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
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.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
...>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="This is the Top Fragment"
android:id="@+id/textView"
android:layout_gravity="center" />
</LinearLayout>

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.

package androidhunger.activityfragmentintentdemo;import android.app.Fragment;
....
public class TopFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.top_fragment,container,false);
return view;
}
}

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.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
.....">
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="androidhunger.activityfragmentintentdemo.TopFragment"
android:id="@+id/fragment"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
tools:layout="@layout/top_fragment" />
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="androidhunger.activityfragmentintentdemo.BottomFragment"
android:id="@+id/fragment2"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
tools:layout="@layout/bottom_fragment" />
</RelativeLayout>
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

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.

--

--