Libin
Libin

Reputation: 17085

Activity noHistory="true" doesn't work when screen locked

I've set noHistory="true" for the Activity on the manifest. The expected result is when the user navigates away from Activity and it's no longer visible on screen the activity will be finished.

This works fine when i navigate to different application by pressing home button and coming back to Activity recreates as expected. But when the Activity is visible , and if the screen locked,and unlocked back resumes the activity . I want the Activity to recreate or just not show up, let the user launch the app again.

  <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".TestActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:noHistory="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Upvotes: 3

Views: 546

Answers (1)

ʍѳђઽ૯ท
ʍѳђઽ૯ท

Reputation: 16976

I want the Activity to recreate or just not show up, let the user launch the app again.

I believe you can handle if the screen is locked, then do your stuffs like killing the app.Seems like when the user is locking the screen, Activity is going in onPause(); method.

Here is the logcat:

02-18 20:27:23.621 22250-22250/com.client.stackoveflow E/LifeCycleEvents: In the onCreate() event
02-18 20:27:23.621 22250-22250/com.client.stackoveflow E/LifeCycleEvents: In the onStart() event
02-18 20:27:23.626 22250-22250/com.client.stackoveflow E/LifeCycleEvents: In the onResume() event
02-18 20:27:27.156 22250-22250/com.client.stackoveflow E/LifeCycleEvents: In the onPause() event
02-18 20:27:27.161 22250-22250/com.client.stackoveflow E/LifeCycleEvents: In the onStop() event
02-18 20:27:36.866 22250-22250/com.client.stackoveflow E/LifeCycleEvents: In the onRestart() event
02-18 20:27:36.866 22250-22250/com.client.stackoveflow E/LifeCycleEvents: In the onStart() event
02-18 20:27:36.881 22250-22250/com.client.stackoveflow E/LifeCycleEvents: In the onResume() event

So, kill the app/or do your stuffs when user locked the screen like this in onPause(); method:

public void onPause() {
        super.onPause();

        Log.e(tag, "In the onPause() event");
        // If the screen is off then the device has been locked
        PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
        boolean isScreenOn = powerManager.isScreenOn(); // deprecated, but you can use isInteractive too

        if (!isScreenOn) {

            finish();  // or do your stuffs
        }

    }

Then, if the user locked the screen, App will be finished and needs to opening it again.

Upvotes: 1

Related Questions