Java Android, static variables are not reset on application close

79 Views Asked by At

I faced with a strange behavior.

My main Activity creates a foreground service and close itself by calling "finishAffinity()".

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String msg = "onCreate, isProgramClosing: " +  Context.isProgramClosing();
        Toast toast = Toast.makeText(this,msg , Toast.LENGTH_SHORT);
        toast.show();

        instance = this;

        mainServiceIntent = new Intent(this, MainService.class);
        mainServiceIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        startService(mainServiceIntent);

        Log.i(getClass().getName(), "Activity finised");
        finishAffinity();
    }

My service change a static bool variable by calling setProgramClosing() , and close itself by calling stopSelf() (jus to reproduce the issue, code is tested)

@Override
public void onCreate() {
    super.onCreate();
    instance = this;

    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,
            0, notificationIntent, 0);

    Notification nt = new NotificationCompat.Builder(this, MainNotificationChannel.CHANNEL_ID)
            .setContentTitle("FarbaRsService")
            .setContentText("")
            .setSmallIcon(R.drawable.ic_android)
            .setContentIntent(pendingIntent)
            .build();

    startForeground(1, nt);

    closeAllResources();
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId){
    return START_NOT_STICKY;
}

public  void closeAllResources(){
    Context.setProgramClosing();
    stopSelf();
}

A class with static variable looks like this.

public class Context { 
   private static boolean isProgramClosing = false;

   public static void setProgramClosing(){
      Context.isProgramClosing = true;
   }

   public static boolean isProgramClosing(){
     return isProgramClosing;
   }
}

When I execute the app 1st time the toast in MainActivity shows false, both Activity and Service suppose to be closed right after it.
When I execute the app 2nd time the toast show true, which means my static variable wasn't reset.

Can someone please explain why is it happening and how to prevent it ?
The only one workaround I see is to manually reset all static variables on Activity creation, but I really don't want to do this.

I know that "Static variables will lose their values only when they are unloaded from JVM during run-time.", but my new instance of app suppose to get new instances of static variables, isn't it ?!

I tried to execute the app on 2 different android devices (android 9 / android 11), got the same result.

1

There are 1 best solutions below

0
CommonsWare On

Can someone please explain why is it happening

Your static variables will live for the entire process duration. Your process will live for a system-defined period of time.

and how to prevent it ?

Stop using static variables.

my new instance of app suppose to get new instances of static variables, isn't it ?

You are creating a new instance of an activity. A new process will be created automatically for you if one is needed, but otherwise Android will reuse an existing running process (a "warm start").