Start an Activity with a parameter

I’m very new on Android development.

I want to create and start an activity to show information about a game. I show that information I need a gameId.

  • Genymotion Error: “Unable to load VirtualBox Engine” on Yosemite. VirtualBox installed
  • What are the differences between R.styleable, and R.attr?
  • append to file android
  • How do you format date and time in Android?
  • LocationListener of NETWORK_PROVIDER is enabled but , onLocationChanged is never called
  • Android: Fragments backStack
  • How can I pass this game ID to the activity? The game ID is absolutely necessary so I don’t want to create or start the activity if it doesn’t have the ID.

    It’s like the activity has got only one constructor with one parameter.

    How can I do that?


  • mapFragment.getMapAsync(this) - NullPointerException
  • Gradle 0.9.+/0.9.2 error: Unable to load class ''
  • How to enable Heap updates on my android client
  • What does synchronized()/wait()/notifyAll() do in Java?
  • Using ECC Curve25519 to encrypt/decrypt data in Java
  • 4 Solutions collect form web for “Start an Activity with a parameter”

    Put an int which is your id into the new Intent.

    Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
    Bundle b = new Bundle();
    b.putInt("key", 1); //Your id
    intent.putExtras(b); //Put your id to your next Intent

    Then grab the id in your new Activity:

    Bundle b = getIntent().getExtras();
    int value = -1; // or other values
    if(b != null)
        value = b.getInt("key");

    Just add extra data to the Intent you use to call your activity.

    In the caller activity :

    Intent i = new Intent(this, TheNextActivity.class);
    i.putExtra("id", id);

    Inside the onCreate() of the activity you call :

    Bundle b = getIntent().getExtras();
    int id = b.getInt("id");

    Edit : Oops, Charlie Sheen was quicker.

    I like to do it with a static method in the second activity:

    private static final String EXTRA_GAME_ID = "your.package.gameId";
    public static void start(Context context, String gameId) {
        Intent intent = new Intent(context, SecondActivity.class);
        intent.putExtra(EXTRA_GAME_ID, gameId);
    protected void onCreate(Bundle savedInstanceState) {
        Intent intent = this.getIntent();
        String gameId = intent.getStringExtra(EXTRA_GAME_ID);

    Then from your first activity (and for anywhere else), you just do:

    SecondActivity.start(this, "");

    The existing answers (pass the data in the Intent passed to startActivity()) show the normal way to solve this problem. There is another solution that can be used in the odd case where you’re creating an Activity that will be started by another app (for example, one of the edit activities in a Tasker plugin) and therefore do not control the Intent which launches the Activity.

    You can create a base-class Activity that has a constructor with a parameter, then a derived class that has a default constructor which calls the base-class constructor with a value, as so:

    class BaseActivity extends Activity
        public BaseActivity(String param)
            // Do something with param
    class DerivedActivity extends BaseActivity
        public DerivedActivity()

    If you need to generate the parameter to pass to the base-class constructor, simply replace the hard-coded value with a function call that returns the correct value to pass.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.