Question

i have an activity showing a dialog which contains radioButtons, and to buttons (Ok,cancel)

  1. can i launch an activity from this dialog ?
  2. how to send the id of the radioButton selected to the second activity ?
Was it helpful?

Solution

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 = new Intent(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 :

Intent intent = getIntent();
int radioButtonId = 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.

OTHER TIPS

Yes you can:

  1. On the OK button listener just do startActivity(intent)
  2. Before the previous method call you have to put an extra in the intent, like so intent.putExtra("radioButtonId", radioButtonValue)

To start an activity from your dialog you'll have to add this into your button's onClick() code:

Intent intentLoad = new Intent(getBaseContext(), YourNewActivity.class);
startActivityForResult();

If you want to send data to your activity, use: putExtra

Like this:

Intent intentLoad = new Intent(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");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top