Showing Custom Dialog in Fragment Activity

4.4k views Asked by At

i am creating a simple application using fragment activity. it is running well. now, everytime i press the back button of my phone with the running application it is always terminating. so i decide to add a custom dialog which will run as verification with "YES" or "NO" button if the user want to close the application.

my code::

MainActivity.java

//NOTE: I want to call the custom dialog at the backpressed event
    @Override
    public void onBackPressed() {
        super.onBackPressed();

        Fragment fragment = null;
        fragment = new ConfirmClose();
    }

close_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="10dp" >

    <TextView
        android:id="@+id/confirmToPrint"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="@string/confirm_to_close" <!-- Do you really want to close? --->
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

ConfirmClose.java

import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class ConfirmClose extends Fragment{
    public ConfirmClose(){
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View promptView = inflater.inflate(R.layout.close_dialog, container);
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity().getApplicationContext());
        alertDialogBuilder.setView(promptView);
        // setup a dialog window
        alertDialogBuilder
                .setCancelable(false)
                .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {;
                            System.exit(0); 
                            //this.finish();
                            }
                        })
                .setNegativeButton("No",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                dialog.cancel();
                            }
                        });
        // create an alert dialog
        AlertDialog alertD = alertDialogBuilder.create();
        alertD.show();
        return promptView;

    }
}

Now, nothings error, nothings problem, i assume that it will work but, when i pressed the back button of my phone with the running application, nothings happen, it is just closing like normal close, the custom dialog is not showing. anyone can help me solve this?

thank you very much!

note: i am using my old android phone to test the application.

4

There are 4 answers

0
Abhishek On
Fragment fragment =  new HomeFragment() ;
        final FragmentManager fragmentManager = getFragmentManager();           
        final Fragment f =fragmentManager.findFragmentById(R.id.content_frame);// your fragment insted of content_frame.......
2
Abhishek On
            final Dialog dialog = new Dialog(MainActivity.this);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.custom_alert_dialogue);
            TextView text = (TextView) dialog.findViewById(R.id.textDialog);
            text.setText("Do You Want To Exit");
            Toast.makeText(MainActivity.this, "Back Click", Toast.LENGTH_SHORT).show();

            Button acceptButton = (Button) dialog.findViewById(R.id.acceptButton);
            Button declineButton = (Button) dialog.findViewById(R.id.declineButton);

            declineButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // Close dialog
                    dialog.dismiss();
                }
            });
            acceptButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // Close dialog
                    fragmentManager.beginTransaction().remove(f).commit(); 
                    finish();
                }
            });
            dialog.show();
0
Surely On
  1. Remove the super.onBackPressed() from the onBackPress function.

  2. Are you trying to implement a popup for the confirmation dialog? Why not just use a alertDialogBuilder directly?

    If you want to customised a pop up fragment, you should use DialogFragment instead of Fragment.

  3. After create the fragment, you also need to show it, otherwise it will not show up.

    I think for your case, you can remove the CloseFragment code, and just build an AlertDiablog in the onBackPressed() code directly.

0
Abhishek On
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textDialog"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_gravity="center"
        android:background="@drawable/square_btnshape"
        android:gravity="center"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:text="adfa"
        android:textColor="#FFF"
        android:textSize="25dp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#FFFFFF" >
    </TextView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2" >

        <Button
            android:id="@+id/acceptButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/square_btnshape"
            android:text=" Yes " />

        <TextView
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="#FFFFFF" >
        </TextView>

        <Button
            android:id="@+id/declineButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/square_btnshape"
            android:text=" No " />
    </LinearLayout>

</LinearLayout>