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

查看:26
本文介绍了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.

如果您使用危险"权限启动应用,这些权限会出现在您的安卓设备的应用权限"中.完美!

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 ?

我试过了在创建开始时在简历上重启恢复实例状态onActionModeStarted恢复实例状态简历上onContentChanged

I tried onCreate onStart onResume onRestart onRestoreInstanceState onActionModeStarted onRestoreInstanceState onPostResume onContentChanged

...但没办法...:/

... but no way... :/

推荐答案

正如已经指出的,正确的 android 正在重新启动您的应用程序.这与当您的应用程序处于后台并且系统在使用更多内存时终止您的应用程序时的行为相同.当您返回到您的应用程序时会发生什么是重新创建最后一个活动包括片段.

As already pointed out correct android is relaunching your app. This is the same behavior as when your app is in background and the system kills your application as more memory is used. What happens when you go back to your application is that the last activity inclusiv fragment(s) is recreated.

通常使用启动屏幕(启动画面)来初始化应用程序.一旦应用程序初始化(例如服务、视图模型准备就绪),启动活动就会切换到主活动.

Usually a start up screen (splash screen) is used to initialize the application. Once the application is initialize (for example the services, viewmodel are ready) the startup-activity is switched to the main activity.

在重新创建应用程序(例如撤销权限)时,许多应用程序都会发生常见的崩溃,因为应用程序未初始化并且使用的服务或视图模型为空.我认为没有办法避免在应用重启后重新创建上一个活动.

A common crash occurs in many applications when the app is recreated(for example revoke permissions) as the app is not initialized and the services or viewmodels are used which are null. I don't think there is a way to avoid a recreation of the last activity after a app restart.

您可以做的是检查应用程序是否已初始化,否则切换到启动活动并初始化应用程序.请注意,您必须在 Activity 和片段中处理单元化的应用程序.

What you could do is to check if the application is initialized and otherwise switch to the startup activity and initialize the app. Be aware that you have to handle unitialized applications in the activity and also the fragments.

Xamarin 示例代码:

Xamarin example code:

if (!((MyApplication)ApplicationContext).IsInitialized)
{
    Intent intent = new Intent(Application.Context,typeof(StartupActivity));
    intent.SetFlags(ActivityFlags.NewTask);
    StartActivity(intent);
    Finish();
}

一旦调用 base.onCreate,就会创建片段,因此即使 acs-team 提供的解决方法"也无法避免重新创建最后一个片段.

As soon as base.onCreate is called the fragments are created so even the provided "workaround" from acs-team does not avoid the recreation of the last fragment.

使用上面提供的示例代码,生命周期将是在 Activity 有片段的情况下:

With the above provided sample code the lifecycle will be in case the activity had a fragment:

  • 撤销权限应用被杀死
  • 重启应用
  • 应用程序创建
  • LastActivity.OnCreate
  • LastFragment.OnAttach
  • LastFragment.OnCreate
  • LastFragment.OnCreateView
  • LastFragment.OnViewCreated
  • LastFragment.OnDestroy
  • LastFragment.OnDettach
  • LastActivity.OnDestroy
  • StartupActivity.OnCreate

顺便说一句,您还可以通过 adb shell 测试应用程序重启:

By the way you can also test an app restart over the adb shell:

打开您的应用,然后转到安卓主屏幕,以使您的应用在后台

Open your app then go to the android home screen in order that your app is in the background

adb shell "ps | grep <com.yourpackage>" // get the app process id
adb shell run-as <com.yourpackage> kill <app-process-id>    // kill the app

在图标或最近的任务上再次启动您的应用

start the your app again over the icon or the recent tasks

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

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