Android 6权限=>禁用权限后崩溃,然后返回到应用程序 [英] Android 6 Permissions => Crash when disable permission and go back to the app

查看:298
本文介绍了Android 6权限=>禁用权限后崩溃,然后返回到应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用中使用android 6的权限,但遇到了一个奇怪的事件.也许您可以帮我一下.

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.

如果您以危险"权限启动应用,这些权限将显示在android设备的应用权限"中.完美!

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

但是,如果您将应用程序保留在后台,请转到应用程序权限"菜单,禁用(可以先启用然后禁用它)权限,然后再返回到您的应用程序,Android永远不要转到onStart(片段或活动) ?!再也不会去那里了.

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.

如果您不触摸权限或启用了权限=>,请转到onStart和其他.

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...

您知道禁用权限时Android的运行方式吗?

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

我尝试过 onCreate onStart onResume onRestart onRestoreInstanceState onActionModeStarted onRestoreInstanceState onPostResume onContentChanged

I tried onCreate onStart onResume onRestart onRestoreInstanceState onActionModeStarted onRestoreInstanceState onPostResume onContentChanged

...但是没有办法...:/

... but no way... :/

推荐答案

这是我的解决方案:

我在mainActivity中使用了一些片段.

I use some fragments in my mainActivity.

如您所知,当用户禁用权限=>进行onCreate,...

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

我在mainActivity的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.

赞:

    // 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();
            }
        }
    }

小心!!! :我在使用appcompat和设计库的23.2.0版本时遇到了崩溃.重新创建应用程序时会出现此崩溃!

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 !

检查此链接了解更多信息.

这篇关于Android 6权限=&gt;禁用权限后崩溃,然后返回到应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆