Android Question Strange issue launching second activity from a service

JordiCP

Expert
Licensed User
Longtime User
My program consists of one service and two actvities: "Main" and a "Alert" activity which I use to show alerts when something happens in the background, doesn't matter if the main activity is running or not.


The desired behaviour, which is the one I get with my HTC One M7 (Android 5) is:

(1)The service detects a condition where the "alert" activity has to be launched.
(2)Generates an intent for that activity
(3)the alert activity is launched regardless the "Main" state. (if Main was running, it gets paused)
(4)When I close alert activity, everything is ok (and Main is Resumed if it was previously running)

But with other phones (Galaxy S3 with Android 4.1.X and BQ Aquaris with Android 4.4.2) , I see strange things:

(3) only works when main has been explicitly finished. If it is running or even paused, the second activity will not be launched.

I have a very ugly workaround for this, that is: when Main activity is paused, I always finish it, and when it is running, the alert activity is launched from the same Main (then it works)

Any solution to avoid this workaround?
 

JordiCP

Expert
Licensed User
Longtime User
I can't test now since I will not have access to the "problematic" devices until wednesday.o_O

The called activity is named "Transparent" and as the name says is a transparent activity with a panel in the middle
My code for the intent is (also tried wth several flags combinations):
B4X:
  Dim In As Intent
  In.Initialize("","")'In.ACTION_MAIN,"")
  In.SetComponent("com.twouchapps.map.prueba/.transparent")
  In.flags = Bit.Or(6815872,0x7D3)'0x7D3 =TYPE_SYSTEM_ALERT
  StartActivity(In)    '(In)

@warwound : Thanks for the point. Will report back when I try it. Anyway (not sure, perhaps I'm confused) it seems to me that FLAG_ACTIVITY_CLEAR_TOP will finish any other activity running. What I want is to load this activity on top of main if it is running (so that when I exit this activity Main comes back to top) or on top of whatever else is running at that moment.
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
I need the activity to pop always, also when the phone screen is off. So I started to search for posts and somewhere found the flags

I know I may be doing something wrong, but as with Android 5.02 this worked I thought it would be good for all versions.
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
My tests

Calling StartActvity(SecondActivity) from a Service, directly without flags (Android 5)
  • if Main is running -> Shows SecondActivity in front of it (OK)
  • if Main is paused -> Redraws Main and shows second Activity in front of it (NOT OK)
  • if Main is Finished -> Shows SecondActivity in front of whatever is running (OK)
Calling StartActivity(SecondActivity) from a Service, with an intent with flags (Android 5)
  • if Main is running -> Shows SecondActivity in front of it (OK)
  • if Main is paused -> Shows SecondActivity in front of whatever is running (OK)
  • if Main is Finished -> Shows SecondActivity in front of whatever is running (OK)


Calling StartActivity(SecondActivity) from a service, directly without flags (Android 4.)
  • if Main is running -> Shows SecondActivity in front of it (OK)
  • if Main is paused -> Redraws Main and shows second Activity in front of it (NOT OK)
  • if Main is Finished -> Shows SecondActivity in front of whatever is running (OK)

Calling StartActivity(SecondActivity) from a Service, with an intent with flags (Android4)
  • if Main is running -> SecondActivity never appears (NOT OK)
  • if Main is paused -> Main is Resumed, but NOT SecondActivity (NOT OK)
  • if Main is Finished -> Shows SecondActivity in front of whatever is running (OK)

So, it seems that:

* Calling StartActivity() wth no flags in this case seems to have consistent behaviour in both 4.X and 5 Android versions. The drawback is that when main activity is Paused, in both cases Main Actvity is drawn under the SecondActivity before it is called.
* Best approach seems to detect if Android 5 is running and call StartActivity with an intent. If Android 4, try to finish the Activity if it is Paused.
* Not even sure that these result can be valid for other cases, just a little bit lost


To solve this, I would need to solve some of these points :(
-A magical flag combination which always works
-Knowing how Android treats the stack of activities and priorities when they are launched from a service
-Know wether a given paused activity is just paused or has been killed/finished
-Be able to kill externally an activity (similar to Activity.Finish but from a Service)
 
Upvote 0
Top