Reputation: 1
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(). 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. I've tested my code on Android 10.
Upvotes: 0
Views: 165
Reputation: 10528
You can use the clearTask
flag from Intent
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
Use it like this
startActivity(intentFor<NeededActivity>().clearTask().newTask())
Upvotes: 0