2

When user press "Home Button" my app go to Background, but is still running. I need bring my app to front again. For example i've this code:

Context ctx=getApplicationContext();
Intent i =ctx.getPackageManager().getLaunchIntentForPackage("com.example.test");      
ctx.startActivity(i);

but this code is to open app in different activity, there are a method or something to do this "correctly"?

user3591034
  • 21
  • 1
  • 2

2 Answers2

3

You can simulate the "launching" of the app the same way that Android launches the app when the user selects it from the list of available apps. If the user starts an application that is already running, Android just brings the existing task to the foreground (which is what you want). Do it like this:

Intent intent = new Intent(context, SplashScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // You need this if starting
                                                // the activity from a service
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);

(Source: https://stackoverflow.com/a/12075313/3529926)

Community
  • 1
  • 1
1

You can set this flag to your intent

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

and set the launch mode of your activity to single task in manifest (add this is your activity tags):

android:launchMode="singleTask"
DDsix
  • 1,966
  • 1
  • 18
  • 23