Stacking them up: The Back Stack
Activities can start other activities.
When a new activity starts, the current one stops. The new one is placed on top of it in the back stack.
Activities continuously move on and off the Back Stack as they are created and destroyed
Press the back button and the top activity is destroyed, showing the next activity in the stack.
The activity comes alive when it is first created and dies when it is destroyed. During this time it executes a couple of lifecycle methods. You can use these methods to run your code at these specific stages.
Giving birth: Creating the activity
To create your activity, subclass the Activity class, like this:
Create the activity by extending the Activity class. This one does not have a User Interface so you won’t see anything on the screen
Then implement the lifecycle methods that you want to use:
- onCreate – you must use this one. It’s called when the activity is created. Use it to initialize the important components of your activity. Call setContentView() to define the layout for the activity’s User Interface if there is one
- onStart – the activity is about to show
- onResume – the user can now see the activity’s User Interface
- onPause – called when the user is about to leave the activity and another one is about to show. This is where you can save important information that you want to keep
- onStop – the activity has stopped and can’t be seen
- onDestroy – the activity is about to be destroyed
What you see: Implementing the User Interface
The User Interface is what the user uses to interact with your app.
The UI has a number of views that you can show in your activity’s screen. Each view takes up some space on the screen.
Android has a number of ready-made views or Widgets that you can use, such as buttons and text fields. Some allow the user to interact with them.
Layouts are also views that enable you to group other, child views together. You can also create your own widgets and layouts to use in your apps.
You can define view groups and views in your code but it’s best to do it in a separate layout file. That way the design of the app is separated from the app’s behaviour.
Once you have a layout, you use setContentView() in your activity to tell it to use that layout.
Use setContentView() to specify the layout to be used by the activity
Declare your intentions: The manifest
You must declare the activity in the AndroidManifest.xml file. If you don’t then you’ll get an error when you run your app.
You must declare all your activities in the AndroidManifest.xml file
name is the only required attribute in the
So what can your activity do? Intent Filters
You can also include intent filters in your
These filters let the system know how other application components can start your activity.
When you create a new application, the activity that’s created for you includes an intent filter. This filter declares that this activity responds to the main action and should be placed in the launcher category. This is what it looks like:
This filter ensures that this activity will be the first to start when the app is launched
Note the following:
- The
element specifies that this activity is the main entry point for the app - The
element specifies that the activity should be listed in the system’s app launcher so that the user can launch this activity
Only one activity in the app should have these elements.
If you don’t want other apps to be able to use your activities, then don’t include any filters.
If you want other apps to start your activities then you must include intent filters. You can also use these filters to start the activity from within your app.
Include an
Get going: Starting an activity
To start an activity, call startActivity(), passing an intent that describes the activity that you want to start.
There are two types of intents:
- Explicit intents – specifies the activity by its class name
Use an explicit intent if you know the class name of the activity that you want to start
- Implicit intents – describes the action that you want performed and the system will then select the appropriate activity for you. The activity could even be in another app
Use an implicit intent if you know what you want done but don’t necessarily know the class name of the activity that you want started
You can also include data in the intent. Have a look at the article, Passing data between activities.
Let them do the work for you: Using other app’s activities to do your work
Sometimes you may want to do something in your app that can be done by another app. So instead of writing your own code, you can start the other app’s activity to do the work for you.
Use the power of an email app to send your email from your activity
Simply create an intent describing the action that you want done. Include any extra data and call startActivity, passing the intent. The system finds the appropriate activity, launches it and the work is done for you.
It’s the result that counts: Start an activity for a result
You can start another activity and get it to return data to your activity.
Simply create an intent specifying the activity that you want to start and then call startActivityForResult, passing the intent and a unique request code as parameters:
Starting another activity to do work for you and return the result
The newly started activity does the work and returns the result.
In the started activity, create an intent, put the data that you want to return in the intent. Call setResult, passing a result ok constant and the intent as parameters. Then call finish(), and the result is returned to your calling activity.
Returning the result
The result is received and processed in the onActivityResult() method:
Receiving and processing the result
Have a look at this tutorial that covers this in detail, Start an activity for a result.
It’s all over: Shutting down an activity
Shut down an activity by calling finish().
Call finish() to close down the activity
Be careful when you use this as it may interfere with the activity’s lifecycle and negatively affect the user’s experience.
Only use if don’t want the user to return to this instance of the activity.
Round and round we go: The activity’s lifecycle
As mentioned previously, the activity moves through a lifecycle, from the time that it is created until the time that it is destroyed.
During this time there are a number of lifecycle methods that we can use to perform tasks at that stage in the lifecycle.
Bear in mind that the activity will probably move through this lifecycle many times when the app is used.
It’s important to manage this lifecycle correctly so that the user has a good experience using your app.
Here’s a great article covering all you need to know about The Activity Lifecycle.
Saving activity state
Each time your device changes orientation, the activity is destroyed and then recreated.
An activity should be able to be destroyed and recreated without the user being aware that this has happened.
When an activity is destroyed, most of its information is automatically saved and then used to rebuild it. Some information is not saved. You need to take care of this.
Have a look at this article, Persisting the Activity Instance State and this tutorial, Saving the Activity’s Instance State: A Tutorial for more on how to make sure that all your activity’s information is saved so that the activity can be rebuilt.
I hope that you have found this tutorial helpful.