1

How to introduce DialogFragment in this or how we can prevent Dialog dismissal when using function to display dialog.

public void alertShow(String s){
    //Game Ended...!!!
    android.support.v7.app.AlertDialog.Builder alertDialog = new android.support.v7.app.AlertDialog.Builder(this);

    // Setting Dialog Title
    if(s.equals("You") || (s.contains("P")))
        alertDialog.setTitle("Congratulations...");
    else
        alertDialog.setTitle("Game's Up...");

    // Setting Dialog Message
    if(s.equals("You") || s.contains("P"))
        alertDialog.setMessage(s+" Win The Game...");
    else
        alertDialog.setMessage("You Lose... I Win...");

    final ImageView img = new ImageView(this);
    if(s.equals("You") || (s.contains("P")))
        img.setImageResource(R.drawable.firecrackers);
    else
        img.setImageResource(R.drawable.mmm);

    alertDialog.setView(img);

    // Setting Dialog Cancellation
    alertDialog.setCancelable(false);

    // Setting Positive "Yes" Button

    SessionClass sessionClass = (SessionClass)getApplicationContext();
    final String str = sessionClass.getPlayer();
    alertDialog.setPositiveButton("Try Again", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Intent i;
            if(null != str && str.contains("2")){
                i = new Intent(MainActivity2.this, Game1_1.class);
                startActivity(i);
            }
            finish();
        }
    });

    // Setting Negative "NO" Button
    alertDialog.setNegativeButton("Main Menu", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Intent intent = new Intent(MainActivity2.this, MainActivity2.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |  Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY);
            startActivity(intent);
            finish();
        }
    });

    // Showing Alert Message
    alertDialog.show();
}

I am a newbie in Android so I don't know how to change this to a dialog which doesn't disappear when screen orientation changes.

Sourav Roy
  • 323
  • 4
  • 12
  • Have a look art this tutorial: http://www.androidbegin.com/tutorial/android-dialogfragment-tutorial/ – Mark Mar 25 '16 at 21:50
  • I've already extended my parent class on the activity then how can I extend FragmentActivity. And isn't there any other way to do this without creating .xml AlertDialogs? – Sourav Roy Mar 25 '16 at 22:00

2 Answers2

1

You just need to create a DialogFragment class, create an AlertDialog, and do everything you're doing now on your AlertDialog. You can send the String in the constructor. Then, you can call back into the Activity for the click events.

Something like this for the DialogFragment (this goes in it's own Java file):

import android.support.v7.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;

public class GameDialogFragment extends DialogFragment {

    String s;

    public GameDialogFragment(String str) {
        this.s = str;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        String title = "";
        String message = "";

        if(s.equals("You") || (s.contains("P")))
            title = "Congratulations...";
        else
            title = "Game's Up...";

        if(s.equals("You") || s.contains("P"))
            message = s+" Win The Game...";
        else
            message = "You Lose... I Win...";

        final ImageView img = new ImageView(getActivity());
        if(s.equals("You") || (s.contains("P")))
            img.setImageResource(R.drawable.firecrackers);
        else
            img.setImageResource(R.drawable.mmm);

        AlertDialog.Builder alertBuilder = new AlertDialog.Builder(getActivity());

        alertBuilder.setView(img);


        AlertDialog dialog = builder.setTitle(title)
                .setMessage(message)
                .setPositiveButton("Try Again", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ((MainActivity2) getActivity()).clickedYes();
                    }
                })
                .setNegativeButton("Main Menu", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ((MainActivity2) getActivity()).clickedNo();
                    }
                })
                .create();

          return dialog;
    }
}

Then, in the Activity, show the DialogFragment in your alertShow() method, and define the click handlers:

public void alertShow(String s){
   new GameDialogFragment(s).show(getFragmentManager(), "");
}

public void clickedYes() {
    SessionClass sessionClass = (SessionClass)getApplicationContext();
    String str = sessionClass.getPlayer();
    Intent i;
    if(null != str && str.contains("2")){
      i = new Intent(MainActivity2.this, Game1_1.class);
      startActivity(i);
    }
}

public void clickedNo() {
    Intent intent = new Intent(MainActivity2.this, MainActivity2.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |  Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY);
    startActivity(intent);
}
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
0

Use a DialogFragment or set a flag and re-trigger the alert dialog after rotation.

This has been answered here.

Community
  • 1
  • 1
Matthew Shearer
  • 2,715
  • 3
  • 23
  • 32
  • I've already extended my parent class on the activity then how can I extend FragmentActivity. And isn't there any other way to do this without creating .xml AlertDialogs? – Sourav Roy Mar 25 '16 at 22:05