Sunday, May 9, 2010

How do I – Android: launch another activity (from my project)?

You use intents to start an activity. Intent describes what exactly you want to do.

I you want to launch an Activity within your project you need to do the following:

1. Lets say you have an Activity “MainActivity.java” with button and “Second.java” – second activity that you want to show when user presses on a button.

2. Define a new intent passing current context (your current activity is an inheritor of a Context) and your target activity’s class name (Second.class in this case).

3. Call startActivity method passing an instance of the Intent created before:

private void onShowViewClick() {
      Intent newActivityIntent = new Intent(this, Second.class);
      this.startActivity(newActivityIntent);
}

Now, when you click on a button – a new activity will be started.

NOTE:
Don’t forget to declare your activity in AndroidManifest.xml.

No comments :

Post a Comment