Android Activities
Android Activities
(https://play.google.com/store/apps/details?id=idig.za.net.addiction)
Free Android app covering all aspects of addiction, including prevention and treatment
You are here: Home (/) / Tutorials (/index.php/articles.html) / Articles (/index.php/itemlist/category/8-articles.html) / Android Activities
Tw eet
Check out our google+ page
(https://plus.google.com/+101appsCoZa/?prsrc=3)
Pin it (http://pinterest.com/pin/create/button/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fwww.101apps.co.za%2Findex.php%2Farticles%2Fandroid-
activities.html&media=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fwww.101apps.co.za%2Fimages%2Fandroid%2Farticles%2FActivity%2Factivity_icon.png&description=Android+Activity+icon)
Like Send Share Share
Android Activities
Written by Clive
Activities are application components that we, mostly, see as the screen. It displays images, text, buttons,
etc. that we interact with. Some activities dont display anything.
Apps usually have a number of activities. One is seen as the main activity. This is the rst screen we see
when the app starts.
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 o 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 rst 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 speci c stages.
Create the activity by extending the Activity class. This one does not have a User Interface so you wont see anything on the screen
onCreate you must use this one. Its called when the activity is created. Use it to initialize the important components of your activity. Call setContentView() to
de ne the layout for the activitys User Interface if there is one
onStart the activity is about to show
onResume the user can now see the activitys 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 cant be seen
onDestroy the activity is about to be destroyed
The UI has a number of views that you can show in your activitys 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 elds. 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.
https://www.101apps.co.za/index.php/articles/android-activities.html 3/9
5/4/2017 Android Activities
You can de ne view groups and views in your code but its best to do it in a separate layout le. That way the design of the app is separated from the apps behaviour.
Once you have a layout, you use setContentView() in your activity to tell it to use that layout.
name is the only required attribute in the <activity> element. There are other attributes that you can include.
These lters let the system know how other application components can start your activity.
When you create a new application, the activity thats created for you includes an intent lter. This lter declares that this activity responds to the main action and should
be placed in the launcher category. This is what it looks like:
https://www.101apps.co.za/index.php/articles/android-activities.html 4/9
5/4/2017 Android Activities
This lter ensures that this activity will be the rst to start when the app is launched
The <action> element speci es that this activity is the main entry point for the app
The <category> element speci es that the activity should be listed in the systems app launcher so that the user can launch this activity
If you dont want other apps to be able to use your activities, then dont include any lters.
If you want other apps to start your activities then you must include intent lters. You can also use these lters to start the activity from within your app.
Include an <intent- lter> for each type of intent that you want your activity to respond to. The lter should include an <action> element. The <category> and <data>
elements are optional.
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
https://www.101apps.co.za/index.php/articles/android-activities.html 5/9
5/4/2017 Android Activities
Use an implicit intent if you know what you want done but dont 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 (/articles/passing-data-between-activities.html).
Let them do the work for you: Using other apps 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 apps 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 nds the appropriate activity,
launches it and the work is done for you.
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.
https://www.101apps.co.za/index.php/articles/android-activities.html 6/9
5/4/2017 Android Activities
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 nish(), and the result is returned to your calling activity.
Have a look at this tutorial that covers this in detail, Start an activity for a result (/articles/start-an-activity-for-a-result.html).
Be careful when you use this as it may interfere with the activitys lifecycle and negatively a ect the users experience.
Only use if dont want the user to return to this instance of the activity.
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.
Its important to manage this lifecycle correctly so that the user has a good experience using your app.
Heres a great article covering all you need to know about The Activity Lifecycle (/articles/the-activity-lifecycle.html).
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 (/articles/persisting-the-activity-instance-state.html) and this tutorial, Saving the Activitys Instance State: A
Tutorial (/articles/saving-the-activity-s-instance-state-a-tutorial.html) for more on how to make sure that all your activitys information is saved so that the activity can be
rebuilt.
Related items
Debug Android apps using SharedPreferences (/index.php/item/190-debug-
android-apps-using-sharedpreferences.html)
https://www.101apps.co.za/index.php/articles/android-activities.html 9/9