15

I am writing an SDK to be used in hosting app. My SDK creates a notification that needs to resume the app, just the same way as you press on the tasks button and select the app, or long press on Home button and select your app.

Here is what I been trying to do:

        PackageManager packageManager = context.getPackageManager();
        intent = packageManager.getLaunchIntentForPackage(context.getPackageName());
        intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
        intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 10, intent, flags);
        Notification notification = new NotificationCompat.Builder(context).
            setContentIntent(pendingIntent).
            ...
            build();

        getNotificationManager().notify(NOTIFICATION_ID, notification);

I been testing this on host app with one launcher activity with lunching mode "default"(no launching mode been set in the manifest) and my sdk also got 1 activity with lunch mode "singleTask".

  1. So I lunch the app
  2. Start my SDK activity it fires a test notification in onCreat method.
  3. I press home
  4. I click on the notification.

After doing those steps I expect to be returned to my activity but instead it opens another instance of the host launcher activity. What am I missing? How do I make this work?

Ilya Gazman
  • 27,805
  • 19
  • 119
  • 190
  • 1
    flow is not very clear. – Rahul Tiwari Sep 29 '15 at 14:03
  • 1
    @RahulTiwari So is your comment ;) – Ilya Gazman Sep 29 '15 at 14:06
  • :) I am not able to understand a few things clearly : is SDK a separate app? or part of your application? are both activities are part of same application? – Rahul Tiwari Sep 29 '15 at 14:14
  • also `intent = packageManager.getLaunchIntentForPackage(context.getPackageName());` will give you launcher activity for your app what you have defined in manifest. this can not be name of activity in your SDK unless you mentioned it in manifest of your host app. – Rahul Tiwari Sep 29 '15 at 14:31
  • did you google or search stackoverflow? because i did and found [this](http://stackoverflow.com/questions/30544165/starting-app-only-if-its-not-currently-running/30586288#30586288) -surprisingly i answered to it – Elltz Sep 30 '15 at 03:41
  • Once the activity goes in background, its not guaranteed that it will stay in memory. So its up to the programmer to maintain the state of the activity and ensure that it presents a consistent UI to the user. You can use shared preferences to save the state of the activity. – Ramesh Prasad Oct 07 '15 at 08:51

3 Answers3

1

If you wanna resume from where you pause, then remove flag "NEW_TASK". It will not make new task. Hope you find it helpful in your task.

user4232
  • 582
  • 1
  • 12
  • 37
Dharvik shah
  • 1,921
  • 10
  • 27
0

As per your requirement host app needs to set "singleTop" launching mode on launcher activity and from your SDK you can not set it. Somehow you need to tell host app to set this flag.

Once you set "singleTop" launching mode you will receive new intent by overriding onNewIntent() method and your activity will be in same state as previous.

Parth
  • 127
  • 8
0

In your AndroidManifest.xml file, go to your main activity's declaration and use android:launchMode="singleTask" like this:

<activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:launchMode="singleTask"
    android:screenOrientation="portrait"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
             <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

This way, if the task is already running in the background, the system will bring it on the foreground letting the user continue what he was doing before the activity was pushed on the background.

Bennik2000
  • 336
  • 4
  • 14
PinoyCoder
  • 1,092
  • 8
  • 13