Android Question Forcing Main Activity into Paused Mode

TheWind777

Active Member
Licensed User
Longtime User
I am trying to put my Main Activity into Paused mode.

I run this:

Dim i As Intent
i.Initialize(i.ACTION_MAIN, "")
i.AddCategory("android.intent.category.HOME")
i.Flags = 268435456 ' FLAG_ACTIVITY_NEW_TASK
StartActivity(i)

It acts as if you pressed HOME; but Activity_Paused doesn't trigger on my Activity and it must be in Foreground mode still.

I tried adding i.Action = "android.intent.action.USER_FOREGROUND" before 'StartActivity(i)'... but I guess many tasks can all be in a Foreground state.

Is there a way of getting the Intent of my Activity and setting its action to i.Action = "android.intent.action.USER_BACKGROUND" either with the Reflection library, or somesuch.
 

TheWind777

Active Member
Licensed User
Longtime User
What exactly are you trying to do? Can't you call Activity.Finish?

What I'm doing is very intricate.

When the main activity goes into paused mode, a service module named Overlord continues the task of searching an intricate time queue. It watches and compares DateTime.Now with (coded) ticks in a Global Long Queue. When a tick meets the critera, I make it look like a MsgBox is appearing on the main screen by revealing an invisible activity with my own (non-modal) Message Box. Once they click 'DONE'... I want it to go back into Paused mode and continue searching the Queue until the next occurance.

I just tried:

Dim i As Intent
i.Initialize(i.ACTION_MAIN, "")
i.AddCategory("android.intent.category.HOME")
i.Flags = 268435456 ' FLAG_ACTIVITY_NEW_TASK
StartActivity(i)
Dim ActivityIntent As Intent
ActivityIntent = Activity.GetStartingIntent
ActivityIntent.Action = "android.intent.action.USER_BACKGROUND"

But that didn't work. You'd think it might...

Is there a way of forcing a new Intent to be generated for the Main Activity? Or maybe getting it to rethink the Intent?
 
Upvote 0

TheWind777

Active Member
Licensed User
Longtime User
What exactly are you trying to do? Can't you call Activity.Finish?
Actually, what it's doing is pretty amazing.

If I can succeed at doing that one little thing... then I can give the appearances from a paused Activity, of a MsgBox opening-up, telling you something, then returning to what it was doing after the MsgBox opened. If you could open up a MsgBox (or equivalent) from a Service Module... I wouldn't have any problem.

Is there some other way to open up a tiny window from a Service Module without having to go through all this rigamarole of restarting my App, showing an invisible background, and putting the Activity back into pause mode when I'm done? It would be so much simpler if I could just open a window from a Service Module, they clicked Done, and never go to the App at all.
 
Upvote 0

TheWind777

Active Member
Licensed User
Longtime User
What exactly are you trying to do? Can't you call Activity.Finish?

I suppose I could just use Notifications... or a toast; but the prompt is quite long to read and the whole idea is that they get to click something in response. The click on the DONE button is logged and how many times they respond is remembered.

If I can't get it to go into Paused Mode... that's what I'll have to resort to.
 
Upvote 0

TheWind777

Active Member
Licensed User
Longtime User
What exactly are you trying to do? Can't you call Activity.Finish?

I've seen the following, posted here:

http://stackoverflow.com/questions/1027149/popup-dialog-android-from-background-thread

AlertDialog dialog;
//add this to your code
dialog = builder.create();
Window window = dialog.getWindow(); WindowManager.LayoutParams lp = window.getAttributes();
lp.token = mInputView.getWindowToken();
lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
window.setAttributes(lp);
window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
//end addons
alert.show();

But I don't have the skills, yet, to translate that over into B4A. I'm sure it can be done... but how?

So, my current experiment has to do with seeing if I can force the Main Activity into Paused mode. If so, then I got it.

If it works good enough... I'll probably give them a choice of responding from the Overlord's Service Module via Notifications, Toast or MsgBox from the Overlord.

I'll try-out what it feels like when I use Notifications next. I'm already doing an Hours/Minutes/Seconds countdown via Notifications; so it would be easy to just have the final countdown text also be put there (with vibrations, or flash, to tell them to read it.
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
Hi,
I have tried to understand the problem and I am probably wrong but, as you use a service, why not use activity.finish and store the activity state (or any marker) in Activity_Pause then reuse this marker in Activity_Resume to change the main activity's behaviour ?
 
Upvote 0

TheWind777

Active Member
Licensed User
Longtime User
Hi,
I have tried to understand the problem and I am probably wrong but, as you use a service, why not use activity.finish and store the activity state (or any marker) in Activity_Pause then reuse this marker in Activity_Resume to change the main activity's behaviour ?
Hmmm... Interesting.

You know, the answer to most things is shockingly simple.

Why NOT just do an Activity.Finish.

All I need is the 'com.*****' of my Activity to start it again. After all, that's what I'm doing to replace the App that might currently be running when it comes back from the Service Module.

Brilliant idea; of course its what Erel had originally said. It just hadn't dawned on me like it just did.

Thanks... both Erel and LemonIsDead.

Good idea; bet that's the answer.

Of course I'm not the type to drag my feet. I already am choosing the background color of my Toast.ToastShow4() and have created the 48x48 icon.

I will, most-likely, give them two choices in Settings: to either use MsgBox or Toast (most won't have the slightest idea what that means... so will need to make it understandable. Something like "Show in Message Box or Pop-up"

Am just about to test a nice purple backdrop Toast with dark purple title and off-white text plus icon. Wish me luck.
 
Upvote 0

TheWind777

Active Member
Licensed User
Longtime User
Hi,
I have tried to understand the problem and I am probably wrong but, as you use a service, why not use activity.finish and store the activity state (or any marker) in Activity_Pause then reuse this marker in Activity_Resume to change the main activity's behaviour ?

You ever use the ToastMessageShow library?

It doesn't die, or crash; but it also doesn't show a toast message from the Service module when the Activity is paused.

Thought I had it (by replacing it with a fancy toast message).

A regular toast message is way too small and fast and nobody will ever go to Notifications; they're too lazy.

Guess I'm back to 'how to pause the Main without destroying my Service module. Will try the Activity.Finish method and see what happens.
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
Won't Activity.Finish destroy the Service?
It shouldn't as a service is not an activity. But don't use ExitApplication ;)

You ever use the ToastMessageShow library?
No sorry, I did never use this library. As I understand what you are trying to do, you should call the StartActivity(Main) or a better way create a new activity to display your toast message and call it depending on the Main activity's state from your service.

B4X:
if isPaused(Main) then StartActivity(Display)

and to hide the Display activity

B4X:
if not(isPaused(Display)) then callsubdelayed(Display,"Quit")

And in the Display activity
B4X:
Sub Quit
  Activity.Finish
End Sub


You will have to create subs and call them using callsub or callsubdelayed of course
 
Last edited:
Upvote 0

TheWind777

Active Member
Licensed User
Longtime User
It shouldn't as a service is not an activity. But don't use ExitApplication ;)


No sorry, I did never use this library. As I understand what you are trying to do, you should call the StartActivity(Main) or a better way create a new activity to display your toast message and call it depending on the Main activity's state from your service.

B4X:
if isPaused(Main) then StartActivity(Display)

and to hide the Display activity

B4X:
if not(isPaused(Display)) then callsubdelayed(Display,"Quit")

And in the Display activity
B4X:
Sub Quit
  Activity.Finish
End Sub


You will have to create subs and call them using callsub or callsubdelayed of course


Well, I have Notifications and Toasts working properly now. The reason the Toasts weren't appearing was because it's sometimes hard to update my Service Module code as it is so persistent.

I've been implementing the fancy toast message box... so haven't had a chance to try the Activity.Finish.

I'm also grabbing the Package Name of any running App when my Service Module decides to run the Main module (with dimmed screen and transparent background so it just looks like a MsgBox).

Sub GetPackagename AsString







I save the PackageName, and replace that after my Activity has gone back to Background state (the reason that it all works just fine as long as they have a running app when it all happens).

Then, to start it back up I do:

StartActivityIntent = PM.GetApplicationIntent(Main.ProcessGlobalsActivityToRestore)

Try

If StartActivityIntent.IsInitialized Then

StartActivity(StartActivityIntent)

Else

' Didn't return initialized Intent

IfMain.Debugging1 ThenLog("OVERLORD: The " & Main.ProcessGlobalsActivityToRestore & " Activity Can't be re-Loaded")

'StopOverlordAnErrorOccurred = True

IfMain.Debugging1 ThenLog("OVERLORD: Going Home")

GoHOME("")

EndIf



Catch

ToastMessageShow("OVERLORD: Cannot Start " & Main.ProcessGlobalsActivityToRestore,False)

EndTry

==============

To start-up my Activity, as you said I do:

If IsPaused(Main) Then StartActivity(Main)

===============

Sorry for all the extraneous variable names and such. I have five Debugging flags set-up and they help when I'm debugging... but definitely don't when I'm explaining. ;)
 
Upvote 0

TheWind777

Active Member
Licensed User
Longtime User
It shouldn't as a service is not an activity. But don't use ExitApplication ;)

Oh; I see.

Hmmm. I am the one who does a if UserClosed then StopService(Overlord)

Also, it is true that an Activity is not a Service... but I am intricately utilizing the Global Variables of the Activity. Doesn't finishing the Activity finish the Globals as well?
 
Upvote 0

TheWind777

Active Member
Licensed User
Longtime User
It would seem that I should be able to just redefine the Activity's intent, then resend it to the proper place to have it go to Background state.

I'm currently trying this:

Dim ActivityIntent AsIntent

ActivityIntent = Activity.GetStartingIntent

ActivityIntent.Action = "android.intent.action.USER_BACKGROUND"

ActivityIntent.Initialize("android.intent.action.USER_BACKGROUND",ActivityIntent.ACTION_APPWIDGET_UPDATE)

It has to be very close to something like that.

Nope. Did nothing. It's not giving me any error. It's just not succeeding in sending it to the background


I wonder if I have to set permissions in the manifest in some way?
 
Upvote 0
Top