1

I have a widget for my application. This application has two activity: The launcher and the main one.

On click on this widget, it opens the linked app, however for now I'm opening the launcher and then I start my main activity from its launcher activity even if it was already running on the background.

Is it possible, on click, to check if the main activity is already running on the background and if so, resuming it; or opening the launcher otherwise?

There is the code I'm using now to opening the launcher from the widget.

    //LISTENER TO OPEN THE APP ON CLICK
    Intent clickIntent = new Intent(context, SplashScreen.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, clickIntent, 0);
    // Get the layout for the App Widget and attach an on-click listener to the button
    views.setOnClickPendingIntent(R.id.widgetstationlayout, pendingIntent);
David Wasser
  • 93,459
  • 16
  • 209
  • 274
Virthuss
  • 3,142
  • 1
  • 21
  • 39
  • 1
    Try [Resume the Top Activity instead of starting the Launcher Activity](http://stackoverflow.com/a/21022876/2931650) – Roshan Dec 16 '15 at 07:03
  • This solved my problem, I tried this before but it makes me realize that my implementation was wrong then. Thanks. – Virthuss Dec 17 '15 at 07:10

1 Answers1

3

To bring your application to the foreground in whatever state it was left in, use this:

Intent intent = getPackageManager().
    getLaunchIntentForPackage("your.package.name");
startActivity(intent);

If your app is already running, this will bring the existing task to the front in whatever state it was in when it was moved to the background. If your app is not running, this will launch your app from the beginning.

This behaves as if you clicked on the app icon.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Thanks for your answer, however my problem has been solved thanks to the links given in the first comment of the original post :) – Virthuss Dec 17 '15 at 07:09
  • Too bad. That answer is actually a bad choice for your problem. – David Wasser Dec 17 '15 at 07:10
  • As I'm not truly expert with the behaviors of activities and fragments life cycles on android, may you explain me why? The solution provided on the answer seemed to be a good choice... – Virthuss Dec 17 '15 at 07:38
  • That answer is a hack. It is a workaround because the problem doesn't occur in the first place if you resume the application correctly (the way I've shown you in this answer). And, as one of the comments says, assuming you had 15 activities in your app, you would need to put that code in each of the 15 activities. It's just bad programming. – David Wasser Dec 17 '15 at 08:53