Home > Android > Tutorial – How to start a new Activity

Tutorial – How to start a new Activity

March 31st, 2009

For starting a new Activity we need to use intent, which is an abstract description of an operation to be performed.

http://developer.android.com/reference/android/content/Intent.html

Suppose we are on Activity named ‘CurrentActivity‘ and on certain event (like button click..) we want to start a new Activity named ‘NextActivity’

We can create a new activity as follows.

public class NextActivity extends Activity {

//Your member variable declaration here

// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
//Your code here
}
}

After we have created the new Activity, we have to register it in file ‘AndroidManifest.xml’.
For registering we have to create an entry in ‘AndroidManifest.xml’ as

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

</activity>

Note that here we have not used intent filter , since we are going to use an explicit intent, the syntax of intent filter is

<intent-filter>

<action android:name="<action here>"/>

<category android:name="<category here>"/>

</intent-filter>

Here,
action — The general action to be performed

category — Gives additional information about the action to execute. For example, CATEGORY_LAUNCHER means it should appear in the Launcher as a top-level application, while CATEGORY_ALTERNATIVE means it should be included in a list of alternative actions the user can perform on a piece of data.

Note: you can also use application tab below the ‘AndroidManifest.xml’ file, and in ‘Application Nodes’ section click
‘Add’ button as shown in figure below and select the activity .

Application Node-Add Activity

Application Node-Add Activity

Next you can start this activity on any event as follows

Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
CurrentActivity.this.startActivity(myIntent);

Here, you have to create and intent with CurrentActivity.this as first parameter
and the the next activity as second parameter.

After you have created the intent, you can start the new activity by calling
startActivity, on current Activity, with the created intent as parameter.

Android , ,

  1. June 29th, 2009 at 23:07 | #1

    Thanks man. That is what I needed :)

  2. Nils Thorell
    July 29th, 2009 at 03:33 | #2

    What is the best to pass a parameter to the new Activity?

  3. Virendra Shakya
    July 29th, 2009 at 23:02 | #3

    Bonjour,
    thank you for short but sweet tutorial.
    Merci.

  4. zeeshan
    July 30th, 2009 at 00:18 | #4

    @Nils Thorell
    Hi Nils,
    Suppose you want to pass parameters from an Activity ‘FirstActivity’ to another Activity ‘SecondActivity’.

    We can use bundle to pass parameters from ‘FirstActivity‘ to ‘SecondActivity‘. And then add this bundle to the intent, you are using to initiate activity ‘SecondActivity‘, as:

    Intent intent = new Intent(FirstActivity.this,SecondActivity.class);

    //Next create the bundle and initialize it
    Bundle bundle = new Bundle();

    //Add the parameters to bundle as
    bundle.putString(“NAME”,”my name”);

    bundle.putString(“COMPANY”,”wissen”);

    bundle.putInt(“AGE”,”22″);

    //Add this bundle to the intent
    intent.putExtras(bundle);

    //Start next activity
    FirstActivity.this.startActivity(intent);

    Now in ‘SecondActivity‘ we can retrieve the parameters values as:

    //First Extract the bundle from intent
    Bundle bundle = getIntent().getExtras();

    //Next extract the values using the key as
    String name = bundle.getString(“NAME”);
    String company = bundle.getString(“COMPANY”);
    String age = bundle.getInt(“AGE”);

  5. tinyang
    August 10th, 2009 at 17:18 | #5

    Wonderful article! I have some questions though, it seems like the explicit intent is not specified in the manifest, but in the java code instead? Is that the best way to set up all explicit intents?

    I need to have one activity start another activity, should I do that only with an explicit intent? Because right now I do have an intent filter in my manifest for this application (see below).

    Notice how it has both intent.action.MAIN and intent.action.LAUNCHER. Would CATEGORY_ALTERNATIVE also apply to an app (for example, one choice of many to perform is to launch this activity) or does it just apply to data? Thanks!

  6. tinyang
    August 10th, 2009 at 17:21 | #6

    tinyang :
    Wonderful article! I have some questions though, it seems like the explicit intent is not specified in the manifest, but in the java code instead? Is that the best way to set up all explicit intents?
    I need to have one activity start another activity, should I do that only with an explicit intent? Because right now I do have an intent filter in my manifest for this application (see below).
    Notice how it has both intent.action.MAIN and intent.action.LAUNCHER. Would CATEGORY_ALTERNATIVE also apply to an app (for example, one choice of many to perform is to launch this activity) or does it just apply to data? Thanks!

    Sorry, it cut out my manifest. Let me try again:

    activity android:name=”.MainMenu”
    android:label=”@string/app_name”
    intent-filter
    action android:name=”android.intent.action.MAIN”
    category android:name=”android.intent.category.LAUNCHER”
    /intent-filter
    /activity

  7. zeeshan
    August 11th, 2009 at 00:45 | #7

    @tinyang
    Hi tinyang,

    The activity tag you post contains an action ‘MAIN’ and category ‘LAUNCHER’.

    This intent filter action ‘MAIN’ and category ‘LAUNCHER’, will create and icon of your application in the android device menu. and clicking on it will launch that activity and so is required.

    Also the intent-filter of ‘MAIN’ and ‘LAUNCHER’ should be in one activity only, otherwise then number of times this intent-filter appears in AndroidManifest.xml , that many times the icon will appear on the device menu.

  8. Raghavendra
    October 14th, 2009 at 04:31 | #8

    Hi,
    It is grateful article. It helps me a lot. Thank you very much…….

  9. October 15th, 2009 at 02:29 | #9

    Hi. Great tutorial.

  10. qlimax
    October 21st, 2009 at 17:33 | #10

    @zeeshan
    this stuff helps me a lot!
    Thank you !

  11. David Scholberg
    November 13th, 2009 at 18:42 | #11

    Thank you so much for this! Because of this article, I learned in five minutes what I couldn’t learn in hours of digging through the Android documentation. You rule!

  12. lapsus63
    November 28th, 2009 at 03:55 | #12

    Don’t forget to add
    Looper.prepare();
    before making the new Intent, in the case you are creating the Intent into a Thread.
    Thank you very much for this how-to !

  13. Danilo Cubrovic
    December 27th, 2009 at 08:21 | #13

    Great tutorial.
    Thank you very much.

  14. Gaurav
    December 30th, 2009 at 06:18 | #14

    Very handy n usefull tut :) Thanks a tonne !

  15. January 6th, 2010 at 15:28 | #15

    Thanks for explaining it.

  16. January 11th, 2010 at 04:12 | #16

    THX!!

    Very cool and simple tutorial!!

  17. January 11th, 2010 at 10:40 | #17

    This is a really cool article …but i m facing one problem my application contains many screens which i broke up into activities… now using this approach suppose i create first splash screen which does the initialization tasks.. then i load the next activity…. but now if the user presses the back button the splash screen is appeared again and user can do nothing except quiting it….

    how should i handle this specific thing ….thanx again

  18. MQAndroid
    January 20th, 2010 at 04:36 | #18

    Very simple and usefull article. Thanks

  19. Alan
    January 28th, 2010 at 03:31 | #19

    @Yacir M Turk
    Yacir,
    You need the splash screen to tell the system that it’s finished and to be removed from the activity stack:

    // in your splash screen activity
    Intent myIntent = new Intent(SplashScreen.this, NextActivity.class);
    startActivity(myIntent);
    finish(); // clears the splash screen from the stack

    Check out the Forwarding example in the android sdk samples.

  20. sheik
    February 21st, 2010 at 02:53 | #20

    Hi,
    can anyone help me in this Q’s asked me in a interview ..

    How to start another activity without knowing the next activity class name .. for example . in the activity when we click on the url , it directs to the browser , something like that . hope am clear in asking the Q .

    thanks
    regards,
    Sheik

  21. Miracle
    March 12th, 2010 at 20:39 | #21

    Actually, it’s very good!

  22. raqz
    March 30th, 2010 at 01:08 | #22

    great article…but can you tel me how to return back to the main activity after completing the next activity…
    i am planning to code an application that involves different activites and returning back to main activity after the sub activity are complete..please guide..thanks

  23. raqz
    March 30th, 2010 at 01:13 | #23

    Also..i want to pass objects through the bundle stuff…is that possible…?? pleaset let me know..thanks

  24. mah
    March 31st, 2010 at 15:10 | #24

    raqz, you should be able to simply “finish();” in your next activity when it has completed, and whatever activity was before it should come to the front.

    As to passing objects, you need to make them Parcelable. Then you can use bundle.putParcelable() and bundle.getParcelable()

  25. April 3rd, 2010 at 00:11 | #25

    Tank u so much, it was so useful and just i wanted

  26. WKRIP
    April 5th, 2010 at 03:15 | #26

    Thanks , this was helpful

  27. April 22nd, 2010 at 09:27 | #27

    Hey.. thanks..that got me started with Android Intents :)

  28. Soubhab Pathak
    May 24th, 2010 at 21:44 | #28

    Wonderful tutorials………….It saved my time….Thanks

  29. Sevens
    June 19th, 2010 at 07:17 | #29

    Its very good tutorials and I have got relief from my headache and saved my time .
    And here i’m having some doubts on Android
    I’m new to android .
    what and all my doubts are :
    1.) CAN I Split the my web page with different different parts?
    like javascript frames and frameset.
    2.) if 1 Q is possible then .I have some buttons at my left panel with vertical layout ..and some buttons at top level with Horizontal layout.
    And if i click any buttons from either Horizontal Layout or Vertical layout..then hit ill hit server and get the data,that data would be display center of my web page without reloading ..my top
    horizontal/left vertical layout…
    I hopes my questions are clear and i can get rely soon.

    Thanks,
    Sevens.

  30. mabodev
    July 1st, 2010 at 13:58 | #30

    Thanks a lot for that!! That’s absolutely brilliant!

  31. Ramkumar
    July 20th, 2010 at 08:38 | #31

    Nice one.

  1. February 10th, 2010 at 12:25 | #1
  2. July 22nd, 2010 at 13:47 | #2