5

I am using Activity.startLockTask() and have noticed that if I pin the screen in Activity A, I am unable to transition to Activity B. It seems like I have to startLockTask() and then stopLockTask() and then startLockTask() again on Activity B.

Is there a way a better way of doing handling this so that I can pin the entire app, regardless of what Activity I am on?

This is how I pin the app:

// start lock task mode if it's not already active
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
// ActivityManager.getLockTaskModeState api is not available in pre-M
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
    if (!am.isInLockTaskMode()) {
        startLockTask();
    }
} else {
    if (am.getLockTaskModeState() == ActivityManager.LOCK_TASK_MODE_NONE) {
        startLockTask();
    }
}

This is how I am stop pinning

stopLockTask()
portfoliobuilder
  • 7,556
  • 14
  • 76
  • 136
  • From the first line of the method docs : `Request to put this Activity in a mode where the user is locked to the current task.` <- emphasis on `this Activity`. I am not aware of a way where you can lock an app, and if you could wouldn't that still have the same effect, just a higher scope? – Mark Jul 21 '17 at 23:23
  • And how do you pin an entire app that has multiple Activities? That is the question. The behavior I described matches what the document says, so I get that. What about the question I poised? – portfoliobuilder Jul 21 '17 at 23:28
  • Also, there are only certain devices where this won't work. So it does work on some devices. Pinning on Activity A and transitioning to Activity B. So it is possible. I am asking for the proper implementation or reference to the proper implementation so that it can work consistently across all devices. As you know, managing 15,000+ devices for Android is the tricky part of Android. – portfoliobuilder Jul 21 '17 at 23:30
  • Maybe I misunderstand, my first sentence explained your first paragraph, and my second sentence stated I don't know the answer - if I did I would have `answered` the question, not commented? I was merely giving you the reason why you have to use the current method from reading the docs – Mark Jul 21 '17 at 23:35

1 Answers1

6

This issue is a difficult issue to deal with, but the solution is very simple. For anyone else facing the same problem, all you have to do is change your launchMode to single task. Once I updated my Manifest, I was able to remain pinned while changing Activities seamlessly.

android:launchMode="singleTask"
portfoliobuilder
  • 7,556
  • 14
  • 76
  • 136
  • This does allow all activities to stay pinned, but I'm seeing that pre Android 12, the activity transition is rather jarring. It looks like an app switch when switching activities. – Adam Johns Feb 10 '23 at 17:25