tags:

views:

99

answers:

2

Hi,

First of all, I am aware my issue is against the philosophy of Android, but I have no choice, this application will run on a embedded car gps and I need to bring an activity to prevent from car accident, for example, when it's happen around the user. I have to put other activity on the back and bring my alert pop up without user manipulation like notification on the front.

Is there a way to bring manually an activity to the front, by resuming it like when you click on the android task switcher?

+2  A: 

Call getApplicationContext() in your Service which will give you a Context and then launch the target Activity as usual.

alex
Or, better yet, don't call `getApplicationContext()` and just call `startActivity()` on the `Service`.
CommonsWare
I have still this kind exception:"03-17 14:41:48.438: ERROR/AndroidRuntime(7895): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?"Despite my flag below:Code in the service:"Context ctx = this.getApplicationContext();startActivity(new Intent(ctx, MainActivity.class));Intent intent = new Intent(this, SplashScreenActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT|Intent.FLAG_ACTIVITY_NEW_TASK);startActivity(intent);"
Erwan Pinault
Anyway if I call startactivity with or without the context from getApplicationContext or from the current service I have the same exception, so I put the flag "FLAG_ACTIVITY_NEW_TASK" but it recreate the activity and I don't want this behaviour.
Erwan Pinault
In fact you have to use both getApplicationContext():Context ctx = this.getApplicationContext();Intent intent = new Intent(ctx, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);startActivity(intent);and use in your manifest: android:launchMode="singleInstance" option to avoiding to create a new instance each time
Erwan Pinault
You don't need to use singleInstance. In fact, please don't use it. Do you realize that it impacts how the semantics if that activity launches its own activities? Maybe that is what you want, but it is a 10 ton weight being applied to solve that problem. Typically you need no special launch mode -- note for example how you don't need to use any special launch mode for the main activity of your app, yet if the user launches your app again from home it brings the current stack forward instead of making a new instance. That is the normal behavior.
hackbod
A: 

This should really be a comment on Alex's answer but that is not possible for some reason.

You don't have to use getApplicationContext() at all as your Service is a Context.

Intent intent = new Intent(this, MainActivity.class);
alexanderblom
thank you!!!!!!
Erwan Pinault