Sending Data To An Intent Depending On A View (a Radio Button)
i have an activity showing a dialog which contains radioButtons, and to buttons (Ok,cancel) can i launch an activity from this dialog ? how to send the id of the radioButton sele
Solution 1:
in the onClick()
method of the OK Button, you can add this code to start another Activity and pass the id of the checked radio button :
Intent intent = newIntent(FirstActivity.this, SecondActitvity.class);
intent.putextras("extra_selected_radio_button", checkedRadioButton.getId());
startActivity(intent);
and in the onCreate()
method of the SecondActivity
, you can retrieve the id of the selected radio button like this :
Intentintent= getIntent();
intradioButtonId= intent.getIntExtra("extra_selected_radio_button", -1); // -1 is the default value
PS : Check this tutorial to learn more about sending data between activities in Android.
Solution 2:
Yes you can:
- On the OK button listener just do
startActivity(intent)
- Before the previous method call you have to put an extra in the intent, like so
intent.putExtra("radioButtonId", radioButtonValue)
Solution 3:
To start an activity from your dialog you'll have to add this into your button's onClick() code:
Intent intentLoad = newIntent(getBaseContext(), YourNewActivity.class);
startActivityForResult();
If you want to send data to your activity, use: putExtra
Like this:
Intent intentLoad = newIntent(getBaseContext(), YourNewActivity.class);
intent.putExtra("radioID", id-of-your-radioButton);
startActivityForResult();
You can catch your id in the activity with:
int myRadioId = extras.getInt("radioID");
Post a Comment for "Sending Data To An Intent Depending On A View (a Radio Button)"