1

this is my dialog-fragment. below you can see the String selection that keep the result after i choose from list of numbers from the dialog. How can i save this value in other activity?

final   CharSequence[] items= {"1","2","3","4"};

String selection;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){



    AlertDialog.Builder builder= new AlertDialog.Builder(getActivity());
    builder.setTitle("CHOOSE YOUR ITEM TO DELETE").setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which){
                case 0:
                    selection=(String) items[which];
                    break;
                case 1:
                    selection=(String) items[which];
                    break;
                case 2:
                    selection=(String) items[which];
                    break;


            }

        }
    }).setPositiveButton("OK", new DialogInterface.OnClickListener(){
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getActivity(),"Your item to delete is  "+ selection,Toast.LENGTH_SHORT).show();

        }




    });
    return builder.create();

}

}

Yakir Huri
  • 21
  • 2

3 Answers3

0

Define a listener/callback and have the other Activity implement and react to selections in this DialogFragment. StackOverflow contains many examples of the implementation.

liminal
  • 1,144
  • 2
  • 13
  • 24
0

Define a public method in your Activity and and a private variable like below.

private String mString;

public setString(String s)
{
   this.mString = s;
}

And when user clicks button get activity and set the selected string like below:

((YourActivity)getActivity).setString(selectedString);

Than you can pass the string to other activities with Bundle.

If you show this DialogFragment from many activities you can define a Listener and implement on your activity.

public interface SelectionListener{
   void onStringSelected(String s);
}

((SelectionListener)getActivity).onStringSelected(selectedString);
savepopulation
  • 11,736
  • 4
  • 55
  • 80
0

For permanently saving :- For permanently saving your value among activities you can use database say sqlite . For temporary saving :- 1. For these you have an option of global ArrayList and by that you can store . 2.You can use callback as described here and these is almost same problem as you have Callback to a Fragment from a DialogFragment

you can see these example also for more details http://www.kylebeal.com/blog/2011/11/android-datepickerdialog-and-the-dialogfragment/

Community
  • 1
  • 1
Shubham Sharma
  • 2,763
  • 5
  • 31
  • 46