Android Wing of mucchin | how to pass a value to the Activity in Android, to launch



How to pass a value to the Activity in Android, to launch


In the Android app, how you want to launch another Activity from the Activity, the information and pass values?

Earlier, we show you how to launch another activity in the Android app.
How to start another activity in the Android app
You might not only be started from the side only, to boot, you want to pass some information, that you want to take over.
Here we will introduce how to do it.


() Method can be used putExtra of Intent class.
Activity in the case to launch the same app, called xxxx, for example, that, and pass word search, and search or search or flags or will be as follows.


i = new Intent (getApplicationContext (), xxxx.class);
i.putExtra ("KEYWORD", keywordStr);
i.putExtra ("AND", andRadio.isChecked ());
startActivity (i);


The first argument of putExtra is the name of the key.
The second argument is the value you want to pass.
even if to start at () startActivity, in the same manner if you can use to start at () startActivityForResult.


Activity in the side is called, will receive in the following manner.


Intent i = getIntent ();
String keyword = i.getStringExtra ("KEYWORD");
boolean isAnd = i.getBooleanExtra ("AND", true);


First, you get an instance type in Intent () getIntent.
This is the implication getIntent () is called, was used to obtain the Intent to start their own.

Side to be set, I set the value in () putExtra uniform, on the receiving end, the method is divided depending on the type of the value are stored.




Type String: getStringExtra ()

When the corresponding key if it does not exist, the return value is null.

type boolean: getBooleanExtra ()

When the corresponding key if it does not exist, the return value is set by the second argument.
In the above example, it is true.

type int: getIntExtra ()

When the corresponding key if it does not exist, the return value is set by the second argument.


After introducing a three well personally I use now, there are many more.
Since that can not be introduced, please refer to the API reference.
Incidentally, you can pass arrays.


Activity also has been started in time from () startActivityForResult, Activity Back to the original boot, you can pass the value in the same way as this.
Do the following.


i = new Intent ();
i.putExtra ("ID", id);
i.putExtra ("NAME", name);
setResult (RESULT_OK, i);
finish ();


This is the end of this time.

Add this entry to Hatena bookmark

1 comment

  1. You can retrieve [...]. () description, has written to the following articles putExtra. Activity in this way to pass a value to Android, which is greater than or equal to start. Categories: Activity-related (activity), Intent (Intel [...]

Comment from here




You can use the following tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> < del datetime = ""> <em> <i> <q cite=""> <strike> <strong>

Next »

«Previous