Why is android:launchMode="singleTop" in AndroidManifest.xml instead of "singleTask" or "singleInstance"?

580 views
Skip to first unread message

sidiabale

unread,
Sep 30, 2015, 4:27:34 AM9/30/15
to CodenameOne Discussions
Hi,

Why is android:launchMode="singleTop" in AndroidManifest.xml? As far as I can tell, the CN1 Android implementation works on the basis of a single activity (and I suppose single instance). According to the official documentation, I'd expect it to be "singleTask" or "singleInstance" (see the table in the section on 'android:launchMode' on this page: http://developer.android.com/intl/ko/guide/topics/manifest/activity-element.html#lmode)

I ask because I'm facing a problem similar to the one mentioned here (unanswered). Scenario:
  1. I have my app running in the foreground (I think it also happened when the app was in the background)
  2. I click a notification from the status bar which among other things tries to start my app via an intent (see code snippet below)
  3. The result is that my app is shown but blank (only the title is diplayed; the rest is white). I've also confirmed that in this case, no CN1 lifecycle methods (e.g. start() or stop()) are called.
  4. However, if the app is not running at all and I repeat steps (2) and (3), everything works fine.
I have a feeling that this occurs because of the android:launchMode but I may be wrong. In any case, how can I solve the problem sketched above?

BTW, here's the relevant code. It is run in the context of a BroadcastReceiver:

protected void doSomething(Context context, Intent intent) {
    
    Class<? extends Activity> cls = getActivity(context, intent);
    Intent activityIntent;
    if (uriString != null) {
        activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uriString));
    } else {
        activityIntent = new Intent(context, cls);
    }

    activityIntent.putExtras(intent.getExtras());
    activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    activityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    
    /*
     In order to remove dependency on android-support-library-v4
     The reason why we differentiate between versions instead of just using context.startActivity
     for all devices is because in API 11 the recommended conventions for app navigation using
     the back key changed.
     */
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addParentStack(cls);
        stackBuilder.addNextIntent(activityIntent);
        stackBuilder.startActivities();
    } else {
        context.startActivity(activityIntent);
    }
}


If you are experiencing an issue please mention the full platform your issue applies to:
IDE: NetBeans/Eclipse/IDEA
Desktop OS
Simulator
Device

sidiabale

unread,
Sep 30, 2015, 5:35:23 AM9/30/15
to CodenameOne Discussions
Here's an update :
  • I've confirmed that strange blank screen behavior only occurs when the app was in foreground before clicking the notification (so, if I first cause the app to go to background e.g. by clicking the home button and then click the notification, everything works fine)
  • I've also confirmed that it goes wrong only via the TaskStackBuilder approach (so if block at the bottom of the code segment I posted). Maybe that's a hint to what's going wrong... I've looked at the documentation for TaskStackBuilder (http://developer.android.com/intl/ko/reference/android/support/v4/app/TaskStackBuilder.html) and I see no reason why this should be a problem in CN1-built apps.
So the problem still persists. More concretely: Why does this behavior occur when a CN1 app is started via TaskStackBuilder?

PS: Maybe some missing info from my original question

Device: Android. Reproduced at least on 4.1.2 and 5.0.1. With the current implementation, it will occur on all devices from v4.1 (=Build.VERSION_CODES.JELLY_BEAN) and onwards. According to the TaskStackBuilder documentation, it should apply from v3.0 which means the problem will be seen on even more devices so that's not nice.

sidiabale

unread,
Sep 30, 2015, 5:37:43 AM9/30/15
to CodenameOne Discussions
Maybe also handy is some relevant background on tasks and back stacks: http://developer.android.com/intl/ko/guide/components/tasks-and-back-stack.html

Shai Almog

unread,
Sep 30, 2015, 11:04:35 PM9/30/15
to CodenameOne Discussions
We support the build hint:
android.launchMode=singleInstance
and I think singleTask is also supported.

I'm not sure why the default is the way that it is and what would make sense here.

sidiabale

unread,
Oct 1, 2015, 3:29:06 AM10/1/15
to CodenameOne Discussions
Hi 

Thanks for the pointer. As far as I can see that build hint is not documented here: http://www.codenameone.com/manual/advanced-topics.html

Can you please update the list?

I'll try with singleInstance and provide an update.

sidiabale

unread,
Oct 1, 2015, 6:27:59 AM10/1/15
to CodenameOne Discussions
Hi Shai,

I added android.launchMode=singleInstance to my project but the resulting code still has android.launchMode=singleTop. Are you sure that the build hint you mentioned is defined? 

I also tried overridding the launchMode using build hint android.xactivity="android.launchMode=singleInstance" but I got the following error:

/home/ec2-user/android-sdk/tools/ant/build.xml:407: org.xml.sax.SAXParseException; lineNumber: 19; columnNumber: 52; Attribute "launchMode" bound to namespace "http://schemas.android.com/apk/res/android" was already specified for element "activity".

On Thursday, 1 October 2015 05:04:35 UTC+2, Shai Almog wrote:

Chen Fishbein

unread,
Oct 1, 2015, 10:57:14 AM10/1/15
to CodenameOne Discussions
Hi,
This build hint is not supported anymore.
You can build with source and then manually change the manifest.
But why do you think that's the issue of the app? Try to check if you see an exception in the ddms when you get the blank screen.

I'm not 100% sure regarding the launchMode but I remember other modes caused various issues so I would try to avoid changing this.
Reply all
Reply to author
Forward
0 new messages