Android Question Samsung with V11 killing my App

AHilberink

Active Member
Licensed User
Longtime User
Hi,

According to:
https://dontkillmyapp.com/samsung

It seems Android v11 on Samsung has a lot of changes on keeping my App running. Even foreground service could be killed.

Two questions:
1. If Android is killing my service will Service_Destroy event running?
2. Will this affect StateManager? It looks like the RestoreState is not restoring

I use this in Activity_Create
B4X:
StateManager.RestoreState(Activity, "Hoofdscherm", 0)

And this:
B4X:
Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed Then
        StateManager.ResetState("Hoofdscherm")
        ScreenSaver.ReleasePartialLock
    Else
        StateManager.SaveState(Activity, "Hoofdscherm")
    End If
    StateManager.SaveSettings
End Sub

Kind regards,
André
 

AHilberink

Active Member
Licensed User
Longtime User
2. The state will be saved. You can try it by clicking on the home button and then kill your app while it is the background.

Hi,

This works indeed at my site. Cannot try it at user site at this moment. I will try tomorrow.

If the kill reason is memory, could it be possible Restore is not working?
 
Upvote 0

AHilberink

Active Member
Licensed User
Longtime User
Hi,

This works indeed at my site. Cannot try it at user site at this moment. I will try tomorrow.

I tried this and it is working fine (RestoreState is working) after manually kill the App.

But there is still a problem:
On my Samsung the App is killed after about 1 hour background and RestoreState is not working.
I did the same on my Motorolla Phone and it is working for 2 days without problem.

Both systems are on Android v11 and my App is the same version. On Android v10 there was no problem on both devices. RestoreState is working perfect.

There must something changed with the v11 Release of Samsung.

I know Samsung changed battery management.
I allready had this:
B4X:
Sub CheckDoze
    Dim p As Phone
    If p.SdkVersion>=23 Then
        Dim JavaObject1 As JavaObject
        JavaObject1.InitializeContext
        Dim Ignoring As Boolean=JavaObject1.RunMethod("isIgnoringBatteryOptimizations", Null)
        If Ignoring=False Then
            Dim JavaObject1 As JavaObject
            JavaObject1.InitializeContext
            JavaObject1.RunMethod("ShowPermissionDialog", Null)
        End If
    End If

#If JAVA
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.provider.Settings;
import android.net.Uri;
import anywheresoftware.b4a.BA;

public boolean isIgnoringBatteryOptimizations(){
Context context=this;
String packageName = context.getPackageName();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
return pm.isIgnoringBatteryOptimizations(packageName);
}

public void ShowPermissionDialog(){
    Intent intent = new Intent();
    Context context=this;
    String packageName = context.getPackageName();
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    if (pm.isIgnoringBatteryOptimizations(packageName)){
        BA.LogInfo("isIgnoringBatteryOptimizations TRUE");
        intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
    } else {
        BA.LogInfo("isIgnoringBatteryOptimizations FALSE");
        intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
        intent.setData(Uri.parse("package:" + packageName));
    }
    context.startActivity(intent);
}
#End If   
End Sub

Mannually kill did disappear my App as selectable under the square button
Stopped by Samsung my App is still selectable, but no filled form. Starting empty as first start.

Questions:
1. Could it be dropped permissions after a while, so there is no access to the file system?
2. Could it be new battery management, but why is RestoreState not working on Resume?
3. Where would you start to investigate this because it is bound to Samsung? I try to read logs, but didnot succeed finding something.
4. Would B4XPages prevent/working better?

Kind regards,
André
 
Upvote 0

AHilberink

Active Member
Licensed User
Longtime User
Hi,

It looks like only Activity_Resume is started on selecting the App from the background and not Activity_Create, which has
B4X:
StateManager.RestoreState(Activity, "Hoofdscherm", 0)
in it.

Is it possible to start Resume with the RestoreState command or will this gives unexpected behaviour?

Kind regards,
André
 
Upvote 0

AHilberink

Active Member
Licensed User
Longtime User
Should be OK - try it.

It's a bit strange it needs it as Resume without Create implies that the Activity still exists and should still have its state.

I tried this and it seems to work, but it wasn't. Still get empty forms.

Still have my questions:
1. Could it be dropped permissions after a while, so there is no access to the file system?
2. Could it be new battery management, but why is RestoreState not working on Resume?
3. Where would you start to investigate this because it is bound to Samsung? I try to read logs, but didnot succeed finding something. No infomation by Crashlytics.
4. Would B4XPages prevent/working better?

Killing my App is something that is not possible to prevent, it is just Android's way. Someone told me I had to expect this behaviour. That's why I use Statemanager. But if Statemanager is not working on this kind of Stop, how can I build something to continue after OS has stopped my App?

Can somebody help me PLEASE? It is going urgent now.
Tell me what you need from my App.

Kind regards,
André
 
Upvote 0

jose luis gudino

Active Member
Licensed User
Longtime User
In a pity that every day we have more limitations with the operating system, one misses those days when an application started automatically, or when an application was running in the background. especially applications that require it.

I am currently developing a security application, but with this limitation, I will have to stop the project, the client cannot be asked to check if his application is running, I also have a taxi application, and in this case the client is forced to driver have the application in the foreground all the time, previously the driver had the app in the background and when a service arrived it was activated and ready.

We will have to dedicate ourselves to the development of standard applications, if we go further.

It is my humble opinion.
 
Upvote 0

AHilberink

Active Member
Licensed User
Longtime User

Thanks for this. I understand you are facing the same.

I hoped for a Restore after my App is stopped. This is what Statemanager is used for, as far as I know.

It is a strange situation: My App seems still active (visible in tasks), but completely resetted.
If my App was killed by the OS, would it be still visible to select from tasks?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…