Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
699 views
in Technique[技术] by (71.8m points)

methods - Android 6 Permissions => Crash when disable permission and go back to the app

I would like to use permission for android 6 in my app but i saw a strange event. Perhaps you can help me about that.

If you start your app with "Dangerous" permissions, these permissions appears in the "App permissions" of your android device. PERFECT!

BUT if you keep your app in background, go to the "App permissions" menu, disable (you can enable and then disable it) a permission and go back to your app, android never go to onStart (Fragment or activity) ?! And never go there again.

If you don't touch to the permission or if you enable a permission => it go to onStart and others.

That's problematic, for exemple, if your app use listeners, you can't restart it and you can have a crash...

Do you know the method where Android go when you disable a permission ?

I tried onCreate onStart onResume onRestart onRestoreInstanceState onActionModeStarted onRestoreInstanceState onPostResume onContentChanged

... but no way... :/

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is my solution:

I use some fragments in my mainActivity.

As you know, the activity is recreated when user disable a permission => go through onCreate, ...

What i do in the onCreate(...) of my mainActivity, i remove all loaded fragments and put my app in the same state as a first run.

Like this:

    // Clean fragments (only if the app is recreated (When user disable permission))
    FragmentManager fragmentManager = getSupportFragmentManager();
    if (fragmentManager.getBackStackEntryCount() > 0) {
        fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
    }

    // Remove previous fragments (case of the app was restarted after changed permission on android 6 and higher)
    List<Fragment> fragmentList = fragmentManager.getFragments();
    if (fragmentList != null) {
        for (Fragment fragment : fragmentList) {
            if (fragment != null) {
                fragmentManager.beginTransaction().remove(fragment).commit();
            }
        }
    }

Be careful !!! : I had a crash with the version 23.2.0 of appcompat and design libraries. This crash appear when the app is recreated !

Check this link for more info.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...