Welcome to part 2 of this review of the Pluralsight course Android for .NET Developers: 2 Building Apps with Android Studio by Jim Wilson
Jim has over 30 years of software engineering experience, with the past 15 years heavily focused on creating mobile device and location-based solutions. He has co-founded multiple software-related startups and has served in a consulting role at several more.
After nearly a decade as a Microsoft Device Application Development MVP, Jim now focuses on developing Android and iOS device applications. Jim’s passion is mentoring software developers. He blogs at hedgehogjim.wordpress.com.
This is the second course in a series of four courses which Jim has created for Pluralsight. I have also reviewed his first course which is Android For .NET Developers 1: Getting Started. If you are completely new to Android you should begin there. But if you have some previous experience with Android Development you can start here.
Adding Activities
Creating and Displaying Activities
In the first module we looked at creating an application with an activity inside of it and we added an options menu.
Most applications have multiple activities, and we can define them inside of our project.
We will enhance the menu to show new activities when the user clicks on a option.
Jim says we can never show an activity directly. Instead, it needs to go through the Android Package Manager.
Although this sounds like a pain, Jim explains that this is a good design.
Android allows us to write programs that use screens from different apps to perform a task, and moving between screens across different apps is as easy as moving between screens in the same app!
This means we need to advertise all the activities in our application, and we do this using the Manifest file.
We must list all of the activities in AndroidManifest.xml even if the activity isn’t used outside of our own application.
The package manager creates a table of entries for all of our activities, and all calls to display an activity pass through this.
The steps are:
- The request to display an Activity is routed to the Package Manager
- The Pacakage Manager locates the information for the Activity
- The Package Manager initiates the display of the Activity
Adding New Activities
Jim creates a new Android project called “ActivityLifecycle” with a blank activity and adds two new activities.
This course was produced using Android Studio v0.1.1 and tons of new features have been added since then.
In this lesson Jim chooses New->Android Component and a dialog box comes up with a dropdown box called Kind, with some options including Activity.
In Android Studio v2.2 you’ll see this:
To follow along with Jim, choose the Empty Activity option.
Jim says we don’t get the code to load the layout and menu resources with this. I have found that v2.2 does automatically create a new layout XML file saving us some work.
We see that it’s dead easy to use the GUI to add text to an activity. We just drag and drop.
Jim also demonstrates the rename refactoring functionality, which is useful.
He manually updates the code adding setContentView. I have found Android Studio now does this for us automatically:
And even v0.1.1 of Android Studio automatically updated the Manifest file for us. This remains the case today.
Displaying Activities
For our main activity Jim says Android Studio generates a stubbed out menu for us and we need to create a full menu. He recommends deleting the default item and recreating it from scratch.
We create items for “Show Activity2”, “Show Activity3” and Exit. Then Jim explains how to write the code to hook these up.
This involves overriding onOptionsItemSelected in a similar, yet simpler, way to how it was done in the previous module.
Then we add menu handlers for each option. We use the Activity’s startActivity method to display another Activity.
Jim also briefly introduces Intents here. They are a way to access Android components, and Jim has produced another course called Android Programming with Intents that explains them in depth.
We write our first intent here:
void onClickMenuShowActivity2(MenuItem item) {
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
}
Jim explains that Activity2.class is similar to typeof(Activity2) in C#.
Demo: Creating and Displaying Activities
We’ve done all the hard work now and can relax and see that when we click “Show Activity 2” it works, as does “Show Activity 3”.
Adding Menus to New Activities
Jim says activities are generally very self contained. He adds a new menu resource “activity3” and adds two menu items to it.
We override onCreateOptionsMenu and onOptionsItemSelected, using the getMenuInflater().inflate method.
We also write a couple of a method onClickMenuShowToast.
Closing Activities
Jim explains why we use the finish method to close our activity in our onClickMenuClose method.
When working in Android, we don’t want to think in terms of process lifetime.
Instead, we think in terms of our components (e.g. Activities). Although the components run within a process, we don’t manage the process lifetime, only the component lifetimes.
Demo: Closing Activities
We run up the code and see activity 3 has action overflow buttons “Show Toast” and “Close”.
When we close the main activity, we are not closing the entire application, only the activity. The process continues to run because it’s managed by the Android platform. Android will later realize it’s no longer in use and clean it up.