SendToBackground

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi
I have done an app that starts a service and has some screens for managing working parameters and other situations. I write this to explain that the service is not the only working entity. I need to hide the activity, i.e. sending it to background, keeping it alive. If I press the "back" button on bottom right, the app ends. If I trap the KeyPress event, returning "true", the app doesn't end but stays on foreground. If I use Activity.SendToBackground, inside the KeyPress event, and return "true", nothing happens and app stays on foreground, not allowing to manage anything else. What am I missing?
Thanks in advance
 

IanMc

Well-Known Member
Licensed User
Longtime User
This works for me:
B4X:
Sub ShowHomeScreen 'programmatically press the Home button on the Android device
   Dim i As Intent    
   i.Initialize(i.ACTION_MAIN, "")    
   i.AddCategory("android.intent.category.HOME")    
   i.Flags = 268435456    
   StartActivity(i)
End Sub

However it is the same as pressing the home key, if you run it twice its like pressing the home key twice.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi, thanks for answer, that is completely out of my skills.. I am forced to ask you how to use your function. As a matter of fact, are you suggesting to insert a call to this function inside the Activity_KeyPress function ? What I need is to hide/unhide the activity screen, allowing the user to make other operations, just like a Windows ShowWindow function. Using:

Sub Activity_KeyPress(KeyCode As Int) As Boolean
If KeyCode = KeyCodes.KEYCODE_BACK Then
Return True
Else
Return False
End If
End Sub

simply prevents the app to be closed. I would like that a code like:

Sub Activity_KeyPress(KeyCode As Int) As Boolean
If KeyCode = KeyCodes.KEYCODE_BACK Then
Activity.SendToBackground '<<<<<<<<<<<<
Return True
Else
Return False
End If
End Sub

will hide the app without closing it. Instead, nothing happens, the application screen stays on foreground.
Thanks
 
Upvote 0

IanMc

Well-Known Member
Licensed User
Longtime User
Hi again, well its not 'my' function :) I think Erel wrote it.

Why not write yourself a quick test application in B4a, that's what I do to test out functions, make one activity and put a button on it, generate its 'click' function then in the code for the button's click function put a call to this function which you can insert further down in your code.

That's what I do to test out pieces of code, you can knock up a new B4a test project in about 2 seconds flat to test it :)

I've done this first one for ya...

and P.S. this ShowHomeScreen function can be called from within a service :)
 

Attachments

  • ShowHomeScreen.zip
    7 KB · Views: 201
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Yes

Hi
sorry, but don't misunderstand. I had no doubt that that code works. Only I was wondering myself how to use it. Your answer came before i do the most obvious test, more or less as you suggested. Now, i put a call to that function inside the KeyPress, instead of the activity.SendToBackground and things go well. Thanks for your extremely useful suggestion. Perfect. (Being a practical guy, I don't go on asking myself why the SendToBackground is doing nothing... but it is my lack of knowledge of Android, I guess..One day I will understand this.)
Thanks a lot again.
 
Upvote 0
Top