Android Question Activity Stack and or Home key Issues

RjCl

Member
Licensed User
Longtime User
Hello,

I'm having problem with controlling the activities.

Via a service, upon receiving a notification of a new email, a transparent 'popup' activity is shown if the 'Main' activity isnt in the foreground, even if Main was never started..... Its working great.

If the Main was run and Back button pressed so its not in foreground and not in background, the popup will again be shown as it should be.......so working great.

These two are done with having a global flag and checked in the service if Main is running,..... this flag is correctly set true/false in the right places, (on startup, pause, back, resume etc, checked with logs)

If (and the problem) Main was run and then HOME key pressed so its not showing but paused in background, upon a new notification BOTH Main and popup activities are incorrectly shown when it should be only popup as the above two scenarios.

Even though the running flag is set to false when Main when Home key is pressed, (checked for UserClosed also) it still shows as well as the popup??? After closing Main with Back key it again works as it should?

I checked for 'IsPaused' but still problem persists..? :confused:
 

RjCl

Member
Licensed User
Longtime User
Thanks,

How do I start popup in new task?

Also, why is it that even though the notification service checks for the flag for Main and its set correctly to false that Android still shows both Main and popup? That 'IF' doesn't even fire cuz running is set to false, it all checks out in the logs, but something is making Main come up if and only if Home was pressed on it. Regardless of Home being pressed the 'IF' statement shouldn't fire, which it doesn't and Android still wants to show Main?
Android should have a way to ask which Activity one wants to call back from the task to the front again, shouldn't be left to Android to guess....
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Thanks,

How do I start popup in new task?
You will have to add flags to your starting intent. I think FLAG_ACTIVITY_NEW_TASK. (If this is what you want).

Also, why is it that even though the notification service checks for the flag for Main and its set correctly to false that Android still shows both Main and popup? That 'IF' doesn't even fire cuz running is set to false, it all checks out in the logs, but something is making Main come up if and only if Home was pressed on it. Regardless of Home being pressed the 'IF' statement shouldn't fire, which it doesn't and Android still wants to show Main?
Android should have a way to ask which Activity one wants to call back from the task to the front again, shouldn't be left to Android to guess....
'Main' will remain on top of your activity stack until you press back. The back button removes it from the stack. When you start your other activity it starts it on the same stack and brings the whole stack forward. (which makes sense).

You might want to read (atleast selected parts) here:
http://developer.android.com/guide/components/tasks-and-back-stack.html
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Does your app support orientation changes? If not then you can call Activity.Finish in Activity_Pause to always remove the activity from the stack.

If your app does support orientation changes then it is a bit more difficult. You need to store the state of the activity in a process global variable. If the activity is in the stack then use CallSubDelayed to call a sub in the activity that calls Activity.Finish.
 
Upvote 0

RjCl

Member
Licensed User
Longtime User
thedesolatesoul,

I was reading last night about tasks and stack, tried the noHistory setting but didn't work. How is the Flag Activity set?
SetActivityAttribute(Main, FLAG_ACTIVITY_NEW_TASK, "true") ?


Erel,
I'd like the state to be stored and even to keep the Main activity in background in pause, as the user going back to Main is much quicker loading that way and opens whatever email was still there. Somehow Viber manages to do this, if press Home key, notification just comes on screen. User can go back to Viber messages and again they are all there, without refresh/reloading/screen flicker, just where the scroll bar is?

I understand about the tasks, (sort of) but why would they both launch? I'm explicitly telling it by the IF statement to launch 'popup' only?
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
If you are not using Erel's method then you can try this *untested* method:
B4X:
SetActivityAttribute(Popup, android:launchMode, "singleInstance")
SetActivityAttribute(Popup, android:taskAffinity, "")

I understand about the tasks, (sort of) but why would they both launch? I'm explicitly telling it by the IF statement to launch 'popup' only?
They dont both launch. They are both part of the same task.
 
Upvote 0

RjCl

Member
Licensed User
Longtime User
If you are not using Erel's method then you can try this *untested* method:
B4X:
SetActivityAttribute(Popup, android:launchMode, "singleInstance")
SetActivityAttribute(Popup, android:taskAffinity, "")


They dont both launch. They are both part of the same task.

Thank you! That did it, its working as it should :)
 
Upvote 0

RjCl

Member
Licensed User
Longtime User
Thanks,

Think should learn more about those attributes. Though is it possible to choose which Activity to pull out from the stack rather than last-in, first-out or something? Block others coming to the foreground and choose the one you would like to. Surely that would make application logic control easier to handle?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
If you have for ex three activities. Main, act2 and act2
And suming you want to go from main to act2 and then to act3. But you know you DONT want to come back to act2 and you want instead coming back to main. you can do it like this

B4X:
'Main act
StartActivity(Act2)

B4X:
'act2
Activity.Finish
StartActivity(Act3)
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Thanks,

Think should learn more about those attributes. Though is it possible to choose which Activity to pull out from the stack rather than last-in, first-out or something? Block others coming to the foreground and choose the one you would like to. Surely that would make application logic control easier to handle?
You can do it with intents and setting their flags. But you will probably make it a lot more complicated than you need to. Usually the application flow is a lot more simpler.
 
Upvote 0
Top