berimbolo
berimbolo

Reputation: 3849

Android: equivelent of IOS' "applicationDidEnterBackground()"

I am working on an app and in the IOS version whenever an external event happens such as the home button or a call comes in the app shows its password entry screen as it shows sensitive data.

I am trying to replicate this in android, I am having trouble because the lifecycle methods can be called and it does not necessarily mean that it was an external event like another app taking focus.

Is there a standard way to detect if onPause() was called because a external event triggered it?

Edit: I have a partially working solution:

@Override
public void onTrimMemory(int level) {
    super.onTrimMemory(level);

    Intent login = new Intent(this, AppEntryPoint.class);
    login.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(login);
}

The issue with this is that if the home button is pressed, this method is called but as the app loses focus it is then brought back into focus by the login intent being fired.

The same applies to the open apps button, pressing it my app goes to the login screen which obscures the view of the page but then it has focus again immediately.

I want the intent to fire but for it not to bring the app back into focus.

Upvotes: 2

Views: 1288

Answers (2)

csenga
csenga

Reputation: 4114

You can use TRIM_MEMORY_UI_HIDDEN callback for this.

Level for onTrimMemory(int): the process had been showing a user interface, and is no longer doing so. Large allocations with the UI should be released at this point to allow memory to be better managed.

Example for it:

Create a class which extends from Application, register it in the Manifest and override it's onTrimMemory.

manifest:

 <application
        android:name=".AppContext" // the declared class name
        android:allowBackup="true"
        android:icon="@mipmap/app_icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        tools:replace="icon"
        >

Aplication class:

public class AppContext extends Application{

....

@Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);

        if(level >= TRIM_MEMORY_UI_HIDDEN)
        {
          //do your job here
        }
    }

Upvotes: 2

aviZatM
aviZatM

Reputation: 11

I can't share all of my code, but the basic gist of it is to:

Set static integers to track the app's state

For example:

private static int resumed = 0;
private static int paused = 0;
private static int started = 0;
private static int stopped = 0;
private static boolean appInBackground = true;

Update these integers accordingly

For example:

@Override
public void on ActivityResumed(Activity activity) {
    ++resumed;
}

@Override
public void onActivityPaused(Activity activity) {
    ++paused;
}

@Override
public void onActivityStopped(Activity activity) {
    ++stopped;
    if(stopped == started) {
        appInBackground = true;
        // other stuff here
    }
}

User that logic to determine what state the app is in

For example:

public static boolean isApplicationInForeground() {
    return resumed > paused;
}

All of this is in a AppLifecycleHandler file. I hope this helps, despite my inability to share all the details of my code!

Upvotes: 1

Related Questions