简体   繁体   中英

Keep Activity on top

I would like to implement a screenLock activity, which triggers whenever the user locks the screen. I did this, but if the user opens the Application History I see two activities of my application: Sensitive-Activity and Lock-Activity.

If the user clicks on the Sensitive-Activity in the stack it comes foreground, without populating the Lock-Activity. By doing this I have some serious security issues here.

Could someone tell me how can I 'pin' Lock-Activity on the top, making it appear even user clicks on other activities?

ps.: noHistroy on Sensitive-Activity is not a solution, since if no lock activated then users can't access to the app once it pushed to the background.

    <activity
        android:name=".Sensitive"
        android:configChanges="keyboardHidden|orientation|screenSize" />

    <activity
        android:name=".Lock"
        android:launchMode="singleInstance"
        android:screenOrientation="portrait" />

In manifest file, remove, launcher tag from sensitive activity. After that only lock activity will show to user. Current you may have set both activities as launcher activity, therefore you are watching two activities.

By loading Sensitive activity from the background(history) run code to show Lock activity. You can register and implement Application lifecycle callbacks

public class ForegroundWatchdog implements Application.ActivityLifecycleCallbacks {
    @Override
    public void onActivityCreated(Activity activity, Bundle bundle) {

    }

    @Override
    public void onActivityStarted(Activity activity) {

    }

    @Override
    public void onActivityResumed(Activity activity) {

    }

    @Override
    public void onActivityPaused(final Activity activity) {

    }

    @Override
    public void onActivityStopped(Activity activity) {

    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {

    }
}

To register callbacks:

public class SomeActivity extends Activity {
    Context appCtx = getApplicationContext();
    if (appCtx instanceof Application) {
        ForegroundWatchdod foregroundWatchdog = new ForegroundWatchdog();
        Application application = (Application)appCtx;
        application.registerActivityLifecycleCallbacks(foregroundWatchdog);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM