Android bring App back to foreground - android

I am devloping an app that has to work in the background and come back up when I get an event from some server.
For that I have a Service that runs in the background and gets messages. What I tought I could do was just start the Activity. Like this:
Intent ROA = new Intent(MainActivity.getInstance(), RouteOverviewActivity.class);
MainActivity.getInstance().startActivity(ROA);
The problem is this. The code gets executed but the app is not pushed in the foreground. When I reopen the app by hand, it opens at the activity I started in the background.
So everything works exept that the app is pushed to the foreground.
Thanks for your help.

You should probably use your service as the context to start the activity, i.e from inside your service code:
Intent ROA = new Intent(this, RouteOverviewActivity.class);
startActivity(ROA);

Related

How do I return to previous app after a service generated activity is 'finished'

My app has a service that runs in the background. It wakes up on intent from the user which is typically generated from another application (such as sharing to my app from the web browser). After the service is done performing a task I want it to open an Activity that displays some info about the task that was completed. When the user is done looking at this info, I want them to be able to hit 'Back' or 'Close' and return to what they were doing previously (not within my application).
I've only managed to get the activity to close leaving my App's main activity front and center on the screen. This only happens if the app is still running. If it's been killed then it correctly returns to the web browser where the user intent was generated from.
How can I make the 'Back' button close the activity and return to the previous app from which this entire workflow was started?
EDIT:
This is the code I am executing from within the android service:
Intent intent = new Intent(this, TestActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
When I tap the back button from the TestActivity it returns me to the main activity of my app, not the previous app I was in when the service started TestActivity.
In second line you are using Intent.FLAG_ACTIVITY_CLEAR_TOP which is clearing your previous activity

startActivity opens last app in background instead of needed activity

I got an app which can receive an event (which is basically a message from a service in the background), and when it does, I am invoking this code:
Intent dialogIntent = new Intent(this, MainActivity.class);
dialogIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);
This code is supposed to return the app to the front so the user can handle the event.
This code works only if the app I'm working on was also the last app the user has been in. The code doesn't work in these scenarios:
1) The user has paused the app, opened another app and then paused it too so the 2nd app is the last in the background - in which case the last app will always open, even though I'm starting my MainActivity. For example, if I've moved my app to the background, opened Android's settings and then an event is received, the Android's settings screen will open instead of my app.
2) The user is inside another app. In which case, the app won't be brought to the front at all.
How can I made the specific app to be resumed from the background no matter what?
Use launcher category as below:
Intent resumeIntent = new Intent(context, MainActivity.class);
resumeIntent.setAction(Intent.ACTION_MAIN);
resumeIntent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(resumeIntent);
Also add android:launchMode="singleTop" in the manifest of your activity.
You can add android:launchMode="singleTop" in the manifest for the Activity.

ObservableObject updateValue doesn't reach the right instance after startActivity

I have a basic Activity which mainly allows the user to change settings and save them. I also have a BroadcastReceiver which is launched on SMS_RECEIVED.
The main point of the app is to vibrate whenever a certain message is received until the user taps a button to make it stop. The activity is only there to allow the user to change settings and press the "Stop" button.
In my onReceive method (BroadcastReceiver), I get the content of the last message received and make the phone vibrate if the message is equal to a certain string. All of that is working perfectly, the problem is when I want to make it stop. Right now, I'm trying to make a "Stop" button appear in the Activity when the phone starts vibrating.
I understand that UI elements should remain in the Activity and so what I'm trying to do is communicate between the Activity and my BroadcastReceiver. I've found here how to do that with an Observer. The problem though is that I want the app to function at any time, even at boot time. It's very easy with a BroadcastReceiver but since it requires the Activity to be shown to allow the user to stop the vibration, I have to start the activity if it isn't started already.
So what I do is this:
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("SMSReceived", true);
context.startActivity(i);
ObservableObject.getInstance().updateValue(true);
The problem is, when there is no instance launched, it creates a new one and sends the extra boolean correctly but the updateValue method doesn't seem to get called at all (due to the previous instance I suppose?) and inversely, when there is an instance launched (in the background) the extra boolean doesn't get passed and the updateValue method gets called correctly.
I suppose I could just launch the Activity on boot and immediately put it in the background but it could cause problems if the user closes the application, at which point it would simply stop working until the user started it again since the Observer would have no instance to send data to.
Do you guys have any idea of what I could do to solve my problem?
If it's not clear I can try to explain further.

Create new intent in background

I am looking for a way to launch another app from within my app but so that the focus is not changed from my app to the app launched.
I.e currently I have the new app launched via a intent, however when this is carried out the new app is launched and becomes the app in view, I need it to be kept in the background with my app still in view.
The reason for this?
I am developing an application for internal use that will act like a lock-screen to the device so although things must happen in the background the 'lock-screen' must always be on top.
I have done some research into intents and launching other apps but can not find anything about what I need.
Hope you can help thank you!
Currently the terminal is called like this:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("jackpal.androidterm", "jackpal.androidterm.RemoteInterface"));
intent.setAction("jackpal.androidterm.RUN_SCRIPT");
intent.putExtra("jackpal.androidterm.iInitialCommand", cmdString);
The reason it needs to be running in the background is so that the app can run commands in terminal without the user having access, but then they 'unlock' the screen they need to then be able to view the terminal and what commands are being run etc
You can not startActivity in Background.Instead start the activity and minimise the activity(in your case this activity is of different application) using moveTaskToBack(true);
In your case, put a condition based on your intent and its params and use moveTaskToBack(true); so that activity will be minimised only when your application launches.
This won't be possible, you will have to start a background Service that does the actual work and only launch the Activity you want to navigate to once your foreground Activity is finished. Depending on your architecture, you can store the Activity to call when your foreground Activity is finished and change it from the service. That way you will have your desire behaviour without having to actually call the Activity.
In addition to the answer from #Meher, in the intent for your current starting activity, you can add the flag FLAG_FROM_BACKGROUND.
With this you get rid of the "blinking" effect (the one where your activity shows for one fraction of second while it discovers wether to go to background)

Clear all current activities and start a new activity in background?

I have an application where after a certain amount of time I want all activities in the app's stack to be removed and replaced by a login activity screen. I've got a Runnable timer that issues this set of statements:
Intent intent = new Intent( ctx, mainActivity.class );
intent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP );
ctx.startActivity( intent );
This works fine when my app is in the foreground, however, when my app is in the background and I'm using another app (say, the Web Browser), the timer fires as it should and pops up the login activity screen to the foreground.
What I want is for the login activity to be activated and moved to the front of the stack, and have all other activities in the stack removed, but not have the app and activity popped to the foreground over any currently running app.
Is there any way to do this with a different method or flag? Thanks.
You could probably finish() the login activity in the onCreate event, conditional on an intent extra. Hopefully it would then be closed before it is displayed...

Categories

Resources