Skip to content Skip to sidebar Skip to footer

Android Java.lang.illegalstateexception In Onrequestpermissionsresult()

I am working with sd card and so trying to get permission in runtime. Here is the code: public class MainActivity extends AppCompatActivity implements FileListFragment.OnFragmentIn

Solution 1:

You should add commitAllowingStateLoss()

 FileListFragment fileListFragment = FileListFragment.newInstance(0);  // **Error line**
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, fileListFragment)
                .commitAllowingStateLoss();

Please check this link

Solution 2:

This Error is because the Activity State is Not Saved and you are adding Fragment before it.

Use This Code :

FileListFragment fileListFragment = FileListFragment.newInstance(0); 
    getSupportFragmentManager().beginTransaction()
            .add(R.id.fragment_container, fileListFragment)
            .commitAllowingStateLoss();

And Override Your onSaveInstanceState() method and remove the super() from it.

Solution 3:

using commitAllowingStateLoss() is more like a hacky fix.

This problem is very similar to this one and is well explained here.

Instead it's better to set a boolean flag in onRequestPermissionsResult and use this in onPostResume (for Activitys) or onResume (for Fragments) to commit the transaction.

Post a Comment for "Android Java.lang.illegalstateexception In Onrequestpermissionsresult()"