Ronak Patel Ronak Patel - 6 months ago 90
Java Question

required android.app.Fragment

I am using

android.support.v4.app.Fragment
. But, it's asking me to use
android.app.Fragment
that I can't. How can I resolve below issue:

import android.support.v4.app.Fragment;
public class CameraFragment extends Fragment implements View.OnClickListener, FragmentCompat.OnRequestPermissionsResultCallback {
private void requestCameraPermission() {
--> if (FragmentCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
--> new ConfirmationDialog().show(getChildFragmentManager(), FRAGMENT_DIALOG);
} else {
FragmentCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},
REQUEST_CAMERA_PERMISSION);
}
}
}



ERROR: Found: com.example.app.CameraFragment, required android.app.Fragment


public static class ConfirmationDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final android.app.Fragment parent = getParentFragment();
return new AlertDialog.Builder(getActivity())
.setMessage(R.string.request_permission)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
FragmentCompat.requestPermissions(parent,
new String[]{Manifest.permission.CAMERA},
REQUEST_CAMERA_PERMISSION);
}
})
.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Activity activity = parent.getActivity();
if (activity != null) {
activity.finish();
}
}
})
.create();
}
}

Answer

If you are using the v4 support lib you shouldn't need FragmentCompat:

    if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {

If that method isn't available, you might need to update your build.gradle to a new version of the v4 support lib.