-1

I am new to Android and am having difficult times handling a very simple task. I understand that in Android, a DialogFragment is non-blocking programmatically. I have my DialogFragment with positive and negative buttons on it and I am calling it from activity that is set as listener for this DialogFragment. That is all fine.

The problem I am having is that in C#, I would use a MessageBox which allows me to check what user has choosen (Yes or No). So, I could write something like this

public void DoSomething(){
  if (boolA == false){
    if (MessageBox.Show(....) == mbYes){
      DoTask();
    }
  }
  else {
    DoTask();
  }
}

In C#, the execution would stop at MessageBox.Show(). But in Android, that is not the case. Also, in C#, I know how to get result of the MessageBox.Show (Yes or No) but I dont know how do I do that in Android.

So, I am trying to use DialogFragment to make decision what to do next. How do I do that? Please provide example as I am novice to Android.

Much appreciated

pixel
  • 9,653
  • 16
  • 82
  • 149
  • You need to pass a reference to something that you want to operate on, and then just call some method on that reference, like a callback. You need to provide more detail for a more comprehensive answer. Some code you have written for example. – ci_ Jul 21 '15 at 15:49
  • I think this link can help. http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/pop-ups/ – krystian71115 Jul 21 '15 at 15:49
  • You can't get the result synchronously. When you block main thread you will get Application not responding message. See that link what have i posted earlier. I hope it can help you. – krystian71115 Jul 21 '15 at 15:53
  • Thanks krystian71115. I am not using C#. I am developing in Java for Android. I am refering to C# just to explain my point. Much appreciated. – pixel Jul 21 '15 at 15:59
  • 1
    See this tutorial.http://www.androidbegin.com/tutorial/android-dialogfragment-tutorial/ – krystian71115 Jul 21 '15 at 16:07

1 Answers1

0

You can build a simple AlertDialog

AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
    builder.setMessage("Are you sure you want to exit?")
            .setCancelable(false)
            .setTitle("My Title")
            .setIcon(R.drawable.icon)
            .setPositiveButton("YES", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                     //DoSomething
                }
            })
            .setNegativeButton("No",new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                     //DO something
                }
            });
    AlertDialog alertDialog=builder.create();
    alertDialog.show();
Sanjeev
  • 4,255
  • 3
  • 28
  • 37
  • 2
    That's not really answering the question, on how to use a `DialogFragment`. – ci_ Jul 21 '15 at 15:49
  • Yes but it can be use in place of it it can do the same thing that DialogFragment can do , – Sanjeev Jul 21 '15 at 15:51
  • 3
    Not exactly the same. Pop up your `AlertDialog`, rotate the phone, dialog gone. – ci_ Jul 21 '15 at 15:53
  • Ok I didn't know that ,Thanks for this,Can you tell me difference between AlertDialog and DialogFragment – Sanjeev Jul 21 '15 at 15:56
  • Have a look here: http://stackoverflow.com/questions/7977392/android-dialogfragment-vs-dialog – ci_ Jul 21 '15 at 15:56
  • Sorry but I am not looking into solution how to create and show AlertDialog or DialogFragment. That I know. My question is asking how to use DialogFragment to make decision in execution flow. – pixel Jul 21 '15 at 16:00
  • @dbnex14 When yes or no is clicked onClick(DialogInterface dialog, int which) is called and in that methods you can do something. – krystian71115 Jul 21 '15 at 16:05
  • MyBad maybe this can help [link](http://stackoverflow.com/questions/12622742/get-value-from-dialogfragment) – Sanjeev Jul 21 '15 at 16:05