Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Permission result callback in DialogFragment?

Here is my Code sample.

public class DialogPermission extends DialogFragment {

    @Nullable
    @Override
    public View getView() {
        View root = LayoutInflater.from(getActivity()).inflate(R.layout.frg_dialog_permission, null, false);
        Button btnRead = (Button) root.findViewById(R.id.btn_read_contact_permission);
        btnRead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED){
                    if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),Manifest.permission.READ_CONTACTS)){
                    }else{
                      ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.READ_CONTACTS},101);}
                }
            }
        });
        return root;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        Log.e("DialogPermission","Ho! Ho! Ho!");  // Log not printed
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

}

I don't want callback in Activity. I hope everything is explained clearly.

like image 452
Nilesh Senta Avatar asked Dec 22 '15 07:12

Nilesh Senta


People also ask

How do I know if DialogFragment is showing?

Showing the DialogFragment Instead, use the show() method to display your dialog. You can pass a reference to a FragmentManager and a String to use as a FragmentTransaction tag.

What is the difference between dialog and DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.

What is DialogFragment used for?

DialogFragment is a utility class which extends the Fragment class. It is a part of the v4 support library and is used to display an overlay modal window within an activity that floats on top of the rest of the content. Essentially a DialogFragment displays a Dialog but inside a Fragment.

Is dialog fragment deprecated?

Stay organized with collections Save and categorize content based on your preferences. This class was deprecated in API level 28.


1 Answers

To get Request permission result in same dialog use requestPermissions method of DialogFragment class.

public class DialogPermission extends DialogFragment {

    @Nullable
    @Override
    public View getView() {
        View root = LayoutInflater.from(getActivity()).inflate(R.layout.frg_dialog_permission, null, false);
        Button btnRead = (Button) root.findViewById(R.id.btn_read_contact_permission);
        btnRead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED){
                    if (shouldShowRequestPermissionRationale(Manifest.permission.READ_CONTACTS)){
                    } else{
                      requestPermissions(new String[]{Manifest.permission.READ_CONTACTS},101);
                    }
                }
            }
        });
        return root;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        Log.e("DialogPermission","Ho! Ho! Ho!");  // Log printed
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

}
like image 80
Dhaval Patel Avatar answered Sep 21 '22 06:09

Dhaval Patel