Reputation: 23
How to pass value back to activity from custom selection of Dialog class?...I am having an application on clicking an image a dialog class will be appeared and i will select one value..i need to update those value in the back activity..can any one suggest me?
Upvotes: 0
Views: 2102
Reputation: 41076
If your dialog is an activity with theme declared as Dialog, you should check how to use startActivityForResult() in activities;
by which you can transfer data from activity 2 to activity 1.
Upvotes: 0
Reputation: 5747
If you're using a class which extends Dialog, you can add an interface for the action you want to perform when the dialog button is clicked (like setting your value in the calling activity) and then set a callback in your constructor. Something like this:
public class CustomDialog extends Dialog {
// this is your interface for what you want to do on the calling activity
public interface ICustomDialogEventListener {
public void customDialogEvent(int valueYouWantToSendBackToTheActivity);
}
private ICustomDialogEventListener onCustomDialogEventListener;
// In the constructor, you set the callback
public CustomDialog(Context context,
ICustomDialogEventListener onCustomDialogEventListener) {
super(context);
this.onCustomDialogEventListener = onCustomDialogEventListener;
}
// And in onCreate, you set up the click event from your dialog to call the callback
@Override
public void onCreate(Bundle savedInstanceState)
{
Button btnOk = (Button) findViewById(R.id.customDialogButton);
btnOk.setOnClickListener( new Button.OnClickListener()
{
public void onClick(View v) {
onCustomDialogEventListener.customDialogEvent(valueYouWantToSendBackToTheActivity);
dismiss();
}
});
}
}
When you want to use your dialog, you construct it with the callback that sets the value in your calling activity:
final CustomDialog dialog = new CustomDialog(this, new ICustomDialogEventListener() {
public void customDialogEvent(int value) {
// Do something with the value here, e.g. set a variable in the calling activity
}
});
Upvotes: 3