2

I've been attempting to use the android 6.0 task locking/pinning to lock down my activity. It seems to work unreliably. Sometimes when I start my application it will pin and everything is hidden like it should. Other times it attempts but it doesn't really take. For example I get a toast that says "Screen Pinned" followed immediately by one that says "Screen Unpinned". The end result is the app isn't fully pinned (hiding the home and task switcher icons). But once it is in this cycle, it doesn't seem to work even if I keep trying. This is also an issue on boot, similar to what is reported here:

Android pin activity on boot

Is there any way to tell what is causing android to unpin my activity? It is frustrating because it doesn't give any indication why it doesn't work.

Community
  • 1
  • 1
Will Young
  • 86
  • 6

1 Answers1

0

Our Lenovo tablet wasn't able to pin a task where the Home activity lived. The solution was to have two activities in two different tasks.

The Home Activity

This activity is started on boot as a launcher and it sole responsibility is to open the main activity immediately. Notice that it's transparent and has different task affinity.

<activity
    android:name=".HomeActivity"
    android:clearTaskOnLaunch="true"
    android:configChanges="orientation|screenSize"
    android:launchMode="singleTask"
    android:resumeWhilePausing="true"
    android:stateNotNeeded="true"
    android:taskAffinity="${applicationId}.home"
    android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>

        <category android:name="android.intent.category.HOME"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</activity>

The activity will launch the Main Activity in its onCreate and onNewIntent as well (because it's a singleTask activity). Here's the code in Kotlin:

class HomeActivity : Activity() {
    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        handleIntent(intent)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        handleIntent(intent)
    }

    private fun handleIntent(intent: Intent) {
        val i = packageManager.getLaunchIntentForPackage(packageName)
        startActivity(i)
    }
}

The Main Activity

This is your main activity - main entry point to your app, it can be started from any launcher. It has the default task affinity (which is equal to the application ID).

<activity
    android:name=".webview.activity.RealWebViewActivity"
    android:clearTaskOnLaunch="true"
    android:configChanges="orientation|screenSize"
    android:exported="false"
    android:launchMode="singleTask"
    android:resumeWhilePausing="true"
    android:stateNotNeeded="true">

    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>

        <category android:name="android.intent.category.LAUNCHER"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>

</activity>
Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124