working with android intents how to pass arguments

2019-06-02 07:37发布

问题:

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:

You have to use extras like this:

i.putExtra(varName, value);



回答2:

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.



回答3:

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



回答4:

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