我一直在使用来自许多界面、 Activity 、小部件和通知的相机手电筒,并且为了协调所有界面,我在随机类中使用静态初始化 block 来实例化所有需要的组件,小部件和通知工作正常,但是当从最近的应用程序中清除启动器应用程序时,似乎我的随机类由于某种我不明白的原因而被卸载。
我不想卸载这个类,我做错了什么吗?
这是我的 list : (当我开始测试正在发生的事情时,我改变了一些东西)
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<!--
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera.flash" android:required="false" />
-->
<application
android:allowBackup="true"
android:icon="@mipmap/icon"
android:label="@string/app_name"
android:roundIcon="@mipmap/icon_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".activity.FlashlightActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".widget.FlashlightWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/flashlight_widget_info" />
</receiver>
<receiver android:name=".widget.WidgetReceiver" />
</application>
这是我的随机类(class):
public class Flashlight {
public static final Flasher FLASHER;
public static final FlashlightActivityList ACTIVITY_LIST;
public static final FlashlightWidgetList WIDGET_LIST;
public static final UI UI;
static {
Log.d("Flashlight", "initialization");
FLASHER = new Flasher();
UI = new UI();
ACTIVITY_LIST = UI.getActivityList();
WIDGET_LIST = UI.getWidgetList();
}
}
Activity 类在 onCreate 和 onDelete 事件上没有任何奇怪的行为
每当从最近的应用程序中清除 Activity 时,就会打印下一个开始的内容
"... D/Flashlight: initialization"
然后可能会崩溃,因为后台有一个线程打开和关闭闪光灯,并且被创建了两次。
最佳答案
but when launcher apps are cleared from recent apps it seems that my random class gets unloaded for some reason I dont understand
您的进程已终止。 This can happen at any time when your app is not in the foreground 。
I dont want this class to get unloaded
你没有太多选择。您的应用程序的进程不会永远运行。您需要能够优雅地处理这种情况。
关于java - 从历史记录中清除 Activity 会卸载应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47643318/