0

Scenario - ActivityA is visible. On press of Home button of Android device, ActivityA/App goes in background and in onUserLeaveHint(), intent to open ActivityB is fired. The app minimises instantly but ActivityB opens after 5-6 seconds of delay. After some debugging, intent is fired immediately but onCreate() of ActivityB is called after 5-6 seconds. PS - ActivityB has launch mode - single instance.

Any idea why is this happening?

The code to open the activity is as follows -

    override fun onUserLeaveHint() {
    super.onUserLeaveHint()

    val intent = Intent(this, ActivityB::class.java)
    startActivity(intent)
}

The ActivityB is defined as following in the manifest file -

<activity android:name=".activity.ActivityB"
             android:allowTaskReparenting="true"
             android:autoRemoveFromRecents="true"
             android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
             android:excludeFromRecents="true"
             android:screenOrientation="portrait"
             android:noHistory="true"
             android:launchMode="singleInstance"
             android:taskAffinity=""
             android:supportsPictureInPicture="true"
             android:theme="@style/PipTheme"/>

PipTheme -

<style name="PipTheme" parent="Theme.AppCompat.Light.NoActionBar">
   <item name="android:windowIsTranslucent">true</item>
   <item name="android:windowBackground">@android:color/transparent</item>
   <item name="android:windowContentOverlay">@null</item>
   <item name="android:windowNoTitle">true</item>
   <item name="android:windowIsFloating">false</item>
   <item name="android:windowActionBar">false</item>
   <item name="android:windowFullscreen">true</item>
   <item name="android:backgroundDimEnabled">false</item>

BATMAN
  • 425
  • 4
  • 13

1 Answers1

1

https://issuetracker.google.com/issues/36910222 Rather than a issue, it is most likely a framework feature which prevents app to force itself open on home button press.

There are few workarounds for this:

  1. Using pendingIntent-

    val intent = Intent(context, ActivityB::class.java) val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0) pendingIntent.send()

  2. Using alarm manager to open activtiy

Varun Naik
  • 42
  • 1
  • 3