1

I've been playing arround with using the camera flashlight from many interfaces, Activities, Widgets and Notifications, and to coordinate all interfaces I'm using a static initialization block in a random class to instance all needed components, widgets and notifications work fine, but when launcher apps are cleared from recent apps it seems that my random class gets unloaded for some reason I dont understand.

I dont want this class to get unloaded, am I doing something wrong?

Here is my manifest: (I've changed some stuff when I started testing what was going on)

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

This is my random 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();
    }
}

The activity class does nothing weird on onCreate and onDelete events

Whenever the activity is cleared from recent apps, the next thing starting prints

"... D/Flashlight: initialization"

and then probably crashes because there is a thread in the background switching the flash on and off, and gets created twice.

Juan
  • 124
  • 2
  • 8

1 Answers1

0

but when launcher apps are cleared from recent apps it seems that my random class gets unloaded for some reason I dont understand

Your process is terminated. This can happen at any time when your app is not in the foreground.

I dont want this class to get unloaded

You do not have much of a choice. Your app's process will not run forever. You need to be able to gracefully handle this scenario.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • What if launch those components in a separate process to that of the activity? then would they be unloaded? – Juan Dec 04 '17 at 23:10
  • @Juan: Again, any process can be terminated when it does not contain part of the foreground user interface (foreground activity, foreground service). – CommonsWare Dec 04 '17 at 23:12