【问题标题】:Activity opens after delay [duplicate]活动延迟后打开[重复]
【发布时间】:2018-11-29 13:30:52
【问题描述】:

场景 - ActivityA 可见。在按下 Android 设备的 Home 按钮时,ActivityA/App 进入后台并在 onUserLeaveHint() 中,触发打开 ActivityB 的意图。该应用程序会立即最小化,但 ActivityB 会在延迟 5-6 秒后打开。经过一些调试后,intent 会立即触发,但 ActivityB 的 onCreate() 会在 5-6 秒后调用。 PS - ActivityB 有启动模式 - 单实例。

知道为什么会这样吗?

打开activity的代码如下-

    override fun onUserLeaveHint() {
    super.onUserLeaveHint()

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

ActivityB 在 manifest 文件中定义如下 -

<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>

【问题讨论】:

  • 你能分享你的代码吗?
  • @RoyalGriffin,添加代码,请检查。
  • 在 onResume() 方法上传递 ActivityB...
  • 看起来活动在按下 Home 后被故意抑制 5 秒(并且有充分的理由)。请参阅this(在许多类似的帖子中)。

标签: android android-studio android-intent android-activity


【解决方案1】:

https://issuetracker.google.com/issues/36910222 与其说是一个问题,不如说是一个框架功能,它阻止了应用程序在按下主页按钮时强制自己打开。

有几个解决方法:

  1. 使用pendingIntent-

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

  2. 使用报警管理器打开活动

【讨论】:

  • 谢谢。有效。我使用了 PendingIntent 方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多