tags:

views:

388

answers:

4

Assuming i want to open another activity from my current activity and i want to pass arguments such as in my case difficulty level how do I do it?

newGameButton.setOnClickListener(new OnClickListener() {
        public  void onClick(View view) {

            Intent i = new Intent(countryCityGameMenu.this,GameScreen.class);    
            startActivityForResult(i, GlobalDataStore.STATIC_INTEGER_VALUE);

        }
    });

is there a way to pass those arguments in the calling ?

can someone show an example explaining

what send activity should do what the new born activity should do

+1  A: 

You have to use extras like this:

i.putExtra(varName, value);

ben monro
can you explain more ? i read this one on http://developer.android.com/reference/android/content/Intent.html#putExtras(android.content.Intent)but still an example would b more efficient thanks for everything.
yoavstr
+1  A: 

As ben already mentioned you can add this data to the intent inside an extra bundle.

An extra bundle stores data as a key value pair of native java data types. You can add data to the intent via the putExtra methods.

In the new Activity you can retrieve this data via the getExtra methods of the Intent. For example the getStringExtra method.

To get the intent that started the current activity just use the getIntent() method from activity.

Janusz
A: 

Not a big fan of this approach... but unfortunately it sends to be the only way to do it in android...

ben monro
send activity side : Intent i = new Intent(countryCityGameMenu.this,GameScreen.class); i.putExtra("title","my name is slim shady")startActivityForResult(i, GlobalDataStore.STATIC_INTEGER_VALUE); invoked side : Intent i = this.getIntent(); String mystr= i.getStringExtra("title"); seems to b odd solution does somebody knows how to solve it differently?
yoavstr
A: 

fine.. U can also uee StartActivity(intent); Thats enough to pass

Lakshmanan