Application always launching with main activity

Hello!

I have a problem creating an application that restores to the same activity after i press home button and launch the application again.
If i create a project 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 i do the same in Mono for Android, after i run the application second time, it opens the first activity (which is marked as MainLauncher).
I see that application is not restarting (i've subclassed application class to check this), but every time i go to the home screen and back to the application, it opens the main activity.
After searching the web, i've found that marking my activities with "LaunchMode=Android.Content.PM.LaunchMode.SingleTask, AlwaysRetainTaskState=true" would help, but it doesn't.

So the question is, how to make the application restore to the activity which was active, when i closed the app?
Thanks!

Answers

  • JonathanPryorJonathanPryor USXamarin Team Xamurai

    How are you initially launching your app? If you're launching this app from the IDE, this bug should be fixed in 4.2.8.

  • AtroposAtropos UAMember

    No, i'm not launching it from IDE. I've even tried making an APK file and running it on phone that never been used in development - same result...

  • JonathanPryorJonathanPryor USXamarin Team Xamurai

    Could you please file a bug with a test case? We haven't seen this before.

  • ScottDavisScottDavis USMember ✭✭

    I know this is a really old thread, but for anyone who's Google search results bring them here. I experienced a relaunching of the main activity in a Xamarin Forms project, after we implemented a "Rate my App" launcher that took users out of the app to the Play Store. From the Play Store, if you click the "Open" button to re-open the app. It launched a new activity, which doubled the memory footprint (eventually OOM crash) and created conflicts with some of our static app managers. For most people a Xamarin Forms application is going to be a "Single Activity" application. We resolved this issue by adding a LaunchMode to our application.

    [Activity(ScreenOrientation = ScreenOrientation.Portrait, MainLauncher = true, Icon = "@drawable/icon", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, LaunchMode = LaunchMode.SingleInstance)]
    public class MainActivity : FormsApplicationActivity
    

    This may not be the right way for everyone, but solved our problem. I don't understand Android well enough to understand why Google would launch a new main activity from the store rather than resume the application.

  • ScottDavisScottDavis USMember ✭✭

    Update:

    If you have two activities, like we do, one splash activity with a theme of "android:windowNoTitle" (to get around this bug that still exists in XamForms 2.3 https://forums.xamarin.com/discussion/21562/this-is-bug)

    which does nothing except make sure the app launches quickly (because it doesn't do anything) and then launches a second activity which is the Xamarin Forms implementation, you may want to set LaunchMode on both activities to SingleTask.

    According to this blog. https://www.mobomo.com/2011/06/android-understanding-activity-launchmode/

    "For ‘singleTask’ or ‘singleInstance’, the activity class uses the singleton pattern, and that instance will be the root activity of a new task."

    If you don't set the singleton instance for both, you will see both Activities in the "task manager" view of Android.

Sign In or Register to comment.