Bug 8286 - Application always launching with main activity
Summary: Application always launching with main activity
Status: RESOLVED UPSTREAM
Alias: None
Product: Android
Classification: Xamarin
Component: General ()
Version: 4.2.x
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2012-11-08 14:09 UTC by Allie Miller
Modified: 2012-12-17 11:16 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
example project (369.96 KB, application/zip)
2012-11-08 14:09 UTC, Allie Miller
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED UPSTREAM

Description Allie Miller 2012-11-08 14:09:53 UTC
Created attachment 2885 [details]
example project

Case #19550

Unable to create an application that restores to the same activity after pressing home button and launch the application again. If a project is created in Java, with two activities, run it, switch to the second activity, go to the home screen and back, the application will be opened on the second activity. But if attempted in Mono for Android, after running the application second time, it opens the first activity (which is marked as MainLauncher). The application is not restarting (It is subclassed application class to check this), but every time you return to the home screen and back to the application, it opens the main activity. The following should help: "LaunchMode=Android.Content.PM.LaunchMode.SingleTask, AlwaysRetainTaskState=true" but it doesn't.

Not launching it from IDE. Also attempted making an APK file and running it on phone that never been used in development with similar result.

If  monodroid project is launched, get to the home screen, long press home button and switch to my app, it opens on the same activity that was left, but if you run it again by clicking on icon in launcher - it starts from the main activity.

Example attached
Comment 1 Jonathan Pryor 2012-11-08 15:07:05 UTC
> switch to the second activity, go to the home screen and back, 
> the application will be opened on the second activity.

What is the meaning of "and back" in the above sentence?

1. Go to the Home screen, click the Back button.
2. Go to the Home screen, re-launch the Application.

(I'm assuming it's not (1), because that doesn't do anything on my test devices...)

Which Mono for Android version is this?

Which target device/API level is this?

I'm unable to reproduce on a Motorola Xoom w/ Android 4.0.4, nor a Galaxy Nexus w/ Android 4.1.2.
Comment 2 Maxim 2012-11-09 04:38:23 UTC
The meaning of "and back" in the above sentence was "2".
But i've misunderstood the behaviour, the bug exists but differs a bit from what i wrote above:
So
Target API : 8
Mono for Android version: 2.7
Test devices: 
Android 4.0.4 (ZTE V880D), 4.0.3 (HTC One V), 2.3.6 (Samsung GT-S5830i), couple of others, same behaviour on every phone.
The test project is the same as attached in first question.

Compile the project, make an APK file, do not run from IDE, copy on device.
Install APK file by clicking on it in file manager. After installation finishes, choose Open (not Done), to open application. Switch to second activity. Press home to exit to home screen. Run application once again from launcher. First activity appears (this is the bug, it should open the second activity)
Kill app in task manager. Run app from launcher. It now works well - if you press home on second activity and then run it once again from launcher, it will restore on the second activity.

If you do not select "Open" after apk install, go to the home screen and open application, it works well.
Comment 3 Jonathan Pryor 2012-11-09 15:59:55 UTC
So if I interpret this right:

1. Initially start app from the launcher, things work fine.

2. Initially start app from Browser (or whatever app you used to install the .apk within the device), things break.

This sounds rather like Bug #1919, except it's an app on-device performing the launch as opposed to the IDE...

@Maxim: Can you read the logcat output and find the first launch intent? It'll look something like:

> I/ActivityManager(  314): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=bxc_6696.bxc_6696/bxc6696.Activity1 u=0} from pid 630

What I'm primary curious about is what actions and categories are being specified when launching the app.

I also wonder what happens with your Java app when you do this.

As far as I'm currently aware, this is all controlled by Android itself, using the Android-controlled Activity stack. I suspect that the problem is due to task affinities, and that your Browser/file manager is using FLAG_ACTIVITY_NEW_TASK, keeping it conceptually separate/distinct from the when the user starts the app  through the launcher.

http://developer.android.com/guide/components/tasks-and-back-stack.html#Affinities

Hopefully the ActivityManager START message can provide additional information.
Comment 4 Maxim 2012-12-17 07:03:58 UTC
Sorry for the long delay

I've finally managed to solve the issue.
The problem was in android itself - every time i started the app from launcher, new instance of root activity was added to the top of the activity stack, i.e. home->child activity->home->child activity, etc.
This was in both java and monodroid test apps.
I've added following code to OnCreate method of my root activity:

if (!IsTaskRoot)
{
    string action = this.Intent.Action;
    if (this.Intent.HasCategory(Intent.CategoryLauncher) && !string.IsNullOrEmpty(this.Intent.Action) && action == Intent.ActionMain)
    {
        Finish();
        return;
    }
}

And everything started working as expected.
Thanks a lot!
Comment 5 Jonathan Pryor 2012-12-17 11:16:43 UTC
Closing as per Comment #4.