Ashuthosh Patoa
Ashuthosh Patoa

Reputation: 314

Start a fragment from upon getting permission

I want to start a fragment from a activity only if the user grants storage permission. My main activity layout looks like this

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/root_container"
android:fitsSystemWindows="true"
android:background="@drawable/splash_gradient">
</FrameLayout>

Now this layout gets displayed when the main activity starts. And i set the code to get the storage permission. And want to replace this layout with my fragment layout but only after storage permission is granted currently its not working my fragment is loading up everything is working fine except that my fragments starts up before the permission is granted. Is there any way to set a callback method on the permissions function and start the fragment only if the permission is granted?

My onCreate method looks like

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.root_container);
    boolean b = getPermission();
    if(b ==true){
        HomeFragment fragment = new HomeFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.root_container, fragment).commit();
    }
    else getPermission();
}

My get Permissions method looks like this

    public boolean getPermission() {
    boolean f = false;
    //Check for SDK Version if greater than 23 then seek user permission
    if(Build.VERSION.SDK_INT>=23) {
        //Check whether permission is already granted
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
           // Ask for user permission
                ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 404);
            if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
                    !=PackageManager.PERMISSION_DENIED)
                f = true;
        }
        else f = true;
    }
    return f;
}

The getPermission returns true when i grant the permission but when the app is first run it starts the fragment before even getting the result of getPermission() method and when i run the app for the second time it displays the fragment layout.

Upvotes: 1

Views: 1287

Answers (2)

Amg91
Amg91

Reputation: 164

what you need to do is: when permission is granted, open.

@Override
public void onRequestPermissionsResult(int requestCode,
    String permissions[], int[] grantResults) {
switch (requestCode) {
    case READ_EXTERNAL_STORAGE: {
        // If request is cancelled, the result arrays are empty.
        if (grantResults.length > 0
            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // permission was granted
            HomeFragment fragment = new HomeFragment();
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.root_container, fragment).commit();
        } else {

            // permission denied, boo! Disable the
            // functionality that depends on this permission.

        }
        return;
    }
}

Follow the example on https://developer.android.com/training/permissions/requesting where the check permission was granted or not.

Upvotes: 0

shmakova
shmakova

Reputation: 6426

You should use onRequestPermissionsResult to handle result:

private static final int REQUEST_CODE = 404;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    // Your code

    if (savedInstanceState == null) {
        tryToOpenHomeFragment();
    }
}

private void tryToOpenHomeFragment() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
        showHomeFragment();
    } else {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == REQUEST_CODE) {
        if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            showHomeFragment();
        } else {
            // Permission was not granted
        }
    }
}

private void showHomeFragment() {
    HomeFragment fragment = new HomeFragment();
    getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.root_container, fragment)
            .commit();
}

Upvotes: 2

Related Questions