Android Question get the focus back

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
a few years back i asked that question and Erel replied
now i need to refine the question as the entire situation changed

here is the scenario

- my app loads and run
- there's a case where it will need to call another app
- when the second app finish its task it does not close and stays on screen waiting for its normal flow
- i got the result that i want from 2nd app and now i want to take back the focus and continue

how do i get back the focus?

thanks
 

zed

Well-Known Member
Licensed User
You cannot close another application from within your own (an Android restriction).
The best practice is to restart your activity or use "StartActivityForResult" so the system handles the return.
If the other application isn't designed to return a result, you'll simply need to bring your app back to the foreground using "Intent" or "BringToFront".

This should bring your app back to the foreground.
B4A:
Dim in As Intent
in.Initialize("", "")
in.SetComponent("com.votrepackage/.Main")
StartActivity(in)

If the other app supports returning results, use "StartActivityForResult(in)". When the other app finishes, Android will recall your activity with the result, and your app will automatically regain focus.

You can also use "Activity.BringToFront".
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
You cannot close another application from within your own (an Android restriction).
The best practice is to restart your activity or use "StartActivityForResult" so the system handles the return.
If the other application isn't designed to return a result, you'll simply need to bring your app back to the foreground using "Intent" or "BringToFront".

This should bring your app back to the foreground.
B4A:
Dim in As Intent
in.Initialize("", "")
in.SetComponent("com.votrepackage/.Main")
StartActivity(in)

If the other app supports returning results, use "StartActivityForResult(in)". When the other app finishes, Android will recall your activity with the result, and your app will automatically regain focus.

You can also use "Activity.BringToFront".
thanks
i will try that

BTW - i do not wish to close the 2nd application just to get the focus back
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
BTW - i do not wish to close the 2nd application just to get the focus back
I'm not sure it's in your hands. As I understand it, (for normal cases) the OS decides freely when to kill background apps.

I'm curious, what are you trying to achieve when you offload a task to a separate app? What is the task? Is the second app made by you?
 
Upvote 0

avalynn11

New Member
a few years back i asked that question and Erel replied
now i need to refine the question as the entire situation changed

here is the scenario

- my app loads and run
- there's a case where it will need to call another app
- when the second app finish its task it does not close and stays on screen waiting for its normal flow
- i got the result that i want from 2nd app and now i want to take back the focus and continue

how do i get back the focus?

thanks
I’ve run into this same headache before, so I totally get your frustration. When the second app doesn’t close itself, Android won’t automatically return focus to your app — it just stays on top.


If the other app isn’t finishing or calling Finish, the only reliable way is to bring your app back to the front manually. In B4A you can do it by using an intent to reopen your main activity:



Dim i As Intent
i.Initialize(i.ACTION_MAIN, "")
i.SetComponent(Application.PackageName & "/.main")
StartActivity(i)


This basically tells Android, “hey, bring my app forward again.”


It won’t close the second app, but it will put your app back in focus so you can continue your flow.


Hope this helps — I know how annoying this behavior can be!
 
Upvote 0
Top