0

As soon as my app opens up for the first time, a dialog fragment should come up (in the MainActivity), explaining why some permissions are needed, and there is an Accept button at the end. I have set an onClick listener to this button, where I want to show the default popup to ask for the permissions. To do that, I need the ActivityCompat.requestPermission() function, which requires the activity where it should be opened in. I have tried several things, like MainActivity(), or this@MainActivity or this@WelcomeDialogFragment etc, but none of these worked.

Is there a way to do that? (Right after the button is pressed, the dialog is closed.) This is my WelcomeDialogFragment class:

class WelcomeDialogFragment : DialogFragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val rootView : View = inflater.inflate(R.layout.welcome_popup, container, false)

        rootView.accept_btn.setOnClickListener {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                ActivityCompat.requestPermissions(
                    this@MainActivity, arrayOf(
                        Manifest.permission.ACCESS_BACKGROUND_LOCATION
                    ), PackageManager.PERMISSION_GRANTED)
            }
            ActivityCompat.requestPermissions(
                this@MainActivity, arrayOf(
                    Manifest.permission.SEND_SMS, Manifest.permission.READ_CONTACTS
                ), PackageManager.PERMISSION_GRANTED)
            dismiss()
        }

        return rootView
    }
}

Thank you.

Enrico Cortinovis
  • 811
  • 3
  • 8
  • 31
  • You should be able to call `getActivity()` or `requireActivity()` from within a fragment ([docs](https://developer.android.com/reference/androidx/fragment/app/Fragment.html#getActivity())) – Tyler V Sep 28 '21 at 16:52
  • @TylerV Thanks for the answer. In the docs it says that `getActivity()` returns an object of type **FragmentActivity**, and the IDE says that an object of type **Activity** is required. I don't understand how I should use it. – Enrico Cortinovis Sep 28 '21 at 16:56
  • Instead of having the dialog fragment request for permissions, I would have the Activity get "notified" that a permissions request is needed and execute the actual request from the Activity. See: https://stackoverflow.com/questions/10905312/receive-result-from-dialogfragment – gioravered Sep 28 '21 at 17:15
  • @gioravered Thank you. I was thinking about that as well, and was searching how to implement `onDismiss` but I was only finding answers in Java. I checked the [documentation](https://developer.android.com/reference/android/app/DialogFragment) as well but it does not make an example and it isn't explained much in detail. Do you know how that could work? Thank you. – Enrico Cortinovis Sep 28 '21 at 17:20
  • Did you try using `FragmentActivity`? It inherits from `Activity`, so you should be able to use it. But as others noted, using a callback and having the activity do it directly would be better. – Tyler V Sep 28 '21 at 17:20
  • @TylerV I did, but I do not understand where and how I should use it. Could you kindly explain that? – Enrico Cortinovis Sep 28 '21 at 17:22
  • `ActivityCompat.requestPermissions(requireActivity(), stuff)` – Tyler V Sep 28 '21 at 17:22

1 Answers1

1

You can use getActivity() or requireActivity(), as in:

ActivityCompat.requestPermissions(requireActivity(), list_of_permissions)
Tyler V
  • 9,694
  • 3
  • 26
  • 52