ATTENTION ATTENTION
gear.huuah.com has launched. Visit the shop at http://gear.huuah.com/. Lots of Photo Gear at the moment
ATTENTION ATTENTION
Itsplanned.com is just launched! - Task and project management made easy. Try it for free.

Create your own lists of things to do - arrange the order to do them - move them around - group them

No limitations - all free project management - try the free demo before signing up - demo: itsplanned.com
The basics of Android intents and activities
30.05.2010

Intents can among many things be used to start op a new user interface in your Android App. If you are using a MVC technique, you would like the view part to the separated into classes for itself. To do this, there is three basic steps to make:

- Create a new class for the activity
- Add the class to the Android Manifest
- Start the activity

Create a class for the user interface

Lets call this file UI:

import android.app.Activity;

public class UI extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // type your magic code here
    }

}

That it basically it. A class extending the Activity – the onCreate is called when the activity is started.
NB: the super.onCreate is very important – without it the code will fail to run!

Editing the AndroidManifest.xml

The register the intent in the application, we have to edit the AndroidManifest.xml file like this:

...
<application...>
        ...
        <activity android:name=".UI" android:label="@string/app_name">
        </activity>
        ...
</application>

This enables us to access the UI intent created earlier.

Starting the activity

All we have to do now, is to start the new intent. This can be done with the startActivity()-function like this:

...
startActivity(new Intent(this, UI.class));
...

I hope this small guide is helpful to you. If you would like an example with more details, please let me know

One Response to “The basics of Android intents and activities”

  1. Activity from Intent Says:

    How do you get an Activity from an Intent? This seems untintuitve as a newbie to Android development. I launch and Intent with an Activity as follow:

    Intent mapIntent = new Intent();
    mapIntent.setClass(this, MathleteMapActivity.class);

    // How do I get a reference to the MathleteMapActivity ‘Activity’ from the Intent ‘mapIntent’?

    Chris

Leave a Reply