Last post, we have seen how to build a simple application just by selecting the default values during application building wizard process. We did not focus on what alternates were available. Starting this post, we will be taking a look at each and every possible alternates that can be used, and we will also try to understand where it can be used. In this post, I will be taking a look at the different activity types available for selection.

Any User Interface (UI) in Android is called as an Activity, While a developer can create an activity by typing in the code, most prefer to go to wizard to build an Activity. There are different types of ready-made activity templates available and should be used as required. The available types of activity are as listed.

Blank Activity Blank Activity with Fragment Empty Activity Full Screen Activity Login Activity Master / Detail Flow Navigation Drawer Activity Settings Activity Tabbed Activity

We will take each activity and try to understand how and where to use it. We will split the discussion in two parts, and cover the standard activities first and then take up the advanced activities in the next post.

1. Blank Activity

This is the most commonly used Activity type and the one used by beginners who do not know any else.

What do I get from Wizard?

We get one XML file, which defines the default layout with Relative Layout and one default TextView component that displays, “Hello World”. We get one .java file, which extends the default Android activity class and provides the over-ridden onCreate() method. This method provides the basic code for instantiating the layout and displaying the default text. We get one string.xml file, which contains the key-value pair of Strings used for i18n.

What do I need to do to make it execute?

Nothing, you just need to run the code, and it will display “Hello World” on the emulator (We have not talked about how to create an emulator, but that will come in a later post).

What will be required to create an app?

The wizard gives us just the basic code, it is nowhere near working application. There are two things that we will have to do.

Go to the Design screen and drag-drop the components that you think is going to be required for building the requirements of the user. Get a reference to the components in the Java code using the auto generated R.java (more on this later).

Where to use it?

You can use this type of activity for any kind of application, as this will be a base activity. You can even generate the same features listed in other advanced activity types by extending this basic activity.

My Recommendation

When you are not sure what type of execution your activity is going to be involved with this would be the best option available, as it can run on any Android device regardless of the version installed. For standard activities that are supposed to accept user data and send it for processing, this activity would be a best fit.

2. Blank Activity with fragment

This activity is similar to the activity discussed earlier, the only difference is that now you are equipped to work with fragments (as introduced in version 3.x). For applications targeting devices sporting versions higher than 4.x, this activity should be the default activity.

What do I get from the wizard?

We get two XML files for layout. First XML file is for the inner fragment, which contains the default “Hello World” message (as described earlier). Second XML file is the encompassing activity level XML file. If we need to make any changes to the layout, we have to make changes to the fragment file, as the fragment is going to be the default view container. We get one .java file, which overrides the ActionBarActivity instead of Activity, as was the case earlier. With version 4.x and above, the menu which used to be available at the bottom of the UI screen on demand, can also be made available at the top of the app screen, to make available there, we use ActionBar Activity. The Java class also provides readymade code to integrate the fragment implementation so that it can integrate with the activity class lifecycle methods.

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_mainfragment);

if (savedInstanceState == null) {

getSupportFragmentManager().beginTransaction()

.add(R.id.container, new PlaceholderFragment()).commit();

}

}

It also embeds one more class within the code to manage the fragment related activities.

public static class PlaceholderFragment extends Fragment {



public PlaceholderFragment() {

}



@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_main, container,

false);

return rootView;

}

}





The PlaceHolderFragment class instantiates the fragment and makes it available for display.

3. We also get the strings.xml file containing the string values for i18n.

What do I need to do to make it execute?

As in previous case, we will have to do nothing, as the wizard has provided us with everything that we need to execute. If you are uncomfortable with fragments and wish to make use of this same .java file to implement the activity, you can do so by implementing a short cut.

Change the extension of the code from ActionBarActivity to Activity, which will lead to errors being displayed in the code. Simply comment out the code that tries to instantiate the fragment, and the code will run as good old activity class.

What will be required to create an app?

The wizard gives you the basic layout of the activity, you will have to populate the fragment with the additional components as the requirements dictate, and then the components can be instantiated in the .java file and start using it.

Where to use it?

When we are trying to build an application for devices sporting API level 11 and above, it is recommended that we should be using fragments, as it is highly possibly that the application may also be utilised on a tablet. Further, for building any application for a tablet, you should use an activity with a fragment.

My Recommendation:

Personally, I feel this activity type should be used as a basic building block for any activity (model devices), as it provides default implementation for fragments and we can simply customise it build our own application.

Closing Remark:

I had thought, I would explain all the activity types in a single post, but looking at the size it does not seem to be possible, hence I will break it up into multiple parts and try to post them every alternate day from today.

Next post: Empty Activity, Full Screen Activity and Login Activity.