简体   繁体   English

活动不从“主页”按钮之后的广播接收器开始,而是从“后退”按钮之后开始

[英]Activity doesn't start from broadcast receiver after home button, and start after back button

I try to start an activity from a broadcast receiver which started from foreground service with this code 我尝试从广播接收器开始一项活动,该接收器使用此代码从前台服务开始

   val intent = Intent("my.intent")
   intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
   context?.startActivity(intent)

Activity started. 活动已开始。 But when I see activity and I press to the home button, next time when receiver tries to start the activity, the activity won't start, and if I press back button when the activity showing, next time activity will start from the receiver without any problems. 但是,当我看到活动并按主页按钮时,下一次接收方尝试启动该活动时,该活动将不会开始,如果在显示活动时按返回按钮,则下一次活动将从接收方开始,而不会任何问题。

I tried to set difference flags to the start intent. 我试图为开始意图设置差异标志。 Set singleTask and singleInstance Also, I tried to override onBackPressed() to finish activity and call finish() from onPause(). 设置singleTask和singleInstance另外,我尝试覆盖onBackPressed()以完成活动并从onPause()调用finish()。 But the behavior wouldn't change. 但是行为不会改变。 I start my foreground service from the main activity, and in this case, I have the same behavior. 我从主要活动中启动前台服务,在这种情况下,我有相同的行为。 If I start my foreground service (simple by button click) and press home to minimize app in this way activity from broadcast wouldn't start too I show toast frow receiver so I'm sure activity must start. 如果我启动前台服务(单击按钮即可)并按主页以这种方式将应用程序减至最少,那么广播活动也不会开始,我会显示土司杯接收器,因此我确定活动必须开始。

My Manifest 我的清单

  <activity
      android:name=".NeededActivity"
      android:screenOrientation="portrait">
      <intent-filter>
          <action android:name="my.intent" />
          <category android:name="android.intent.category.DEFAULT" />
      </intent-filter>
   </activity>

What's the difference and how I can fix it, I need to start Activity every time when I call startActivity in the receiver. 有什么区别以及如何修复它,所以我每次在接收器中调用startActivity时都需要启动Activity。 I've tested my code on Android 10. 我已经在Android 10上测试了我的代码。

You can use the clearTask flag from Intent 您可以使用Intent中的clearTask标志

For more information about intent flags you can refer at the official documentation at Activity Intent 有关意图标志的更多信息,请参见活动意图中的官方文档。

Also, since you are using Kotlin why not try Anko 另外,由于您使用的是Kotlin,为什么不尝试Anko

Use it like this 这样使用

startActivity(intentFor<NeededActivity>().clearTask().newTask())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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