Forms App restarts Every time when I bring it back from the background.

The App restarts from the root login page every time I bring it back from the Background, it doesn't continue. I tried with a lot of solutions that I found online. Nothing worked.
I tried to handle it from native mainactivity under OnCreate( )(refer the code below) :

            var myIntent = new Intent();
    if (!IsTaskRoot && myIntent.HasCategory(Intent.CategoryLauncher) 
                && myIntent.Action != null && myIntent.Action.Equals(Intent.ActionMain))
            {
                Finish();
                return;
            }

And also, I tried changing various options for activity launch mode from the Manifest . None of them worked. Few of the answers suggested that this may be due to Memory Management( Low Cost /Low Memory Devices ) but this is not the case with me.
After a lot of research I came to know that I have to Disable **"Don't Keep Activities" **option from the Developer Options Settings - this actually worked and I stopped facing the mentioned issue but I am not able to control this setting programmatically. Any help with this issue will be appreciated. Thanks

Answers

  • AhmadFrazAhmadFraz Member ✭✭

    You don't need to set this option programmatically. Run your application in release mode and test whether the app is behaving the same.

  • PoorneshV15PoorneshV15 Member ✭✭
    edited April 16

    @AhmadFraz It is the same case even when I run with Release mode..

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭

    This is definitely not normal behaviour. In order for us to help you, can you please post:

    • Version information (version of Xamarin.Android and Xamarin.Forms).
    • Your MainActivity.OnCreate() code.
    • Your App.xaml.cs code

    If possible, a project/solution which demonstrates the problem so we can try see if we can reproduce it.

  • PoorneshV15PoorneshV15 Member ✭✭

    @JamesLavery
    I am actually using lower version of Xamarin ( Xamarin.forms Version - 2.3.4.270 ) due to some constraints. Do you think that may be the reason for this issue. Sorry , I can't Upgrade the Xamarin Version and check it as it is causing some other issues while launching the application.

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭

    I doubt the Xamarin.Forms version is the problem. Please post your code.

  • PoorneshV15PoorneshV15 Member ✭✭

    @JamesLavery MainActivity.OnCreate()

      protected override void OnCreate(Bundle bundle)
            {
                App.ScreenHeight = (int)(Resources.DisplayMetrics.HeightPixels / Resources.DisplayMetrics.Density);
                App.ScreenWidth = (int)(Resources.DisplayMetrics.WidthPixels / Resources.DisplayMetrics.Density);
                TabLayoutResource = Resource.Layout.Tabbar;
                ToolbarResource = Resource.Layout.Toolbar;
    
                ServicePointManager.ServerCertificateValidationCallback +=
                (sender, cert, chain, sslPolicyErrors) => true;
    
                base.OnCreate(bundle);
    
    /*   var myIntent = new Intent();
                if (!IsTaskRoot && myIntent.HasCategory(Intent.CategoryLauncher) 
                    && myIntent.Action != null && myIntent.Action.Equals(Intent.ActionMain))
                {
                    Finish();
                    return;
                }  */
    
    
    
                activity = this;
    
                global::Xamarin.Forms.Forms.Init(this, bundle);
                global::XamForms.Controls.Droid.Calendar.Init();
                global::ZXing.Net.Mobile.Forms.Android.Platform.Init();
    
                LoadApplication(new App());
            }
    

    and I haven't actually included anything that can be reason for this issue in my App.xaml.cs

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭
    edited April 16

    What about the declaration of your MainActivity class itself, and its attributes? For example:

    namespace MyappApp.Droid
    {
        [Activity(Label = "MyappApp.Android", Icon = "@drawable/icon", Theme = "@style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
        public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
        {
            protected override void OnCreate(Bundle bundle)
            {
                TabLayoutResource = Resource.Layout.Tabbar;
                ToolbarResource = Resource.Layout.Toolbar;
    
                base.OnCreate(bundle);
    
    
                global::Xamarin.Forms.Forms.Init(this, bundle);
    
    
                App app = new App();
                LoadApplication(app);
            }
        }
    }
    

    I'd still be interested in what you have coded in your App.xaml.cs - you never know!

    Has the app always misbehaved like this, or only just started?

  • PoorneshV15PoorneshV15 Member ✭✭

    @JamesLavery The app has always misbehaved..
    public partial class App : Application
    {
    public static int ScreenHeight;
    public static int ScreenWidth;

        public DeviceOrientations Portrait { get; }
    
        public App()
        {
            CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("EN-US");
            InitializeComponent();
    
    
    
    
            // 60 minutes for Default TimeOut
            Global.TimeSpanToTimeout = new System.TimeSpan(0, 30, 0);
            Global.BackgroundImage = "bgImage.png";
    
    
    
        }
    
    
        public static class BuildSettings
        {
       // Some Code
    
        }
        public enum Environment
        {
    
        }
    
        /// <summary>
        /// Scannser SDK ENUM - Scandit, ZXing 
        /// </summary>
        public enum ScannerSDK
        {
           //Some lib
        }
        protected override void OnStart()
        {
            // Handle when your app starts
        }
    
        protected override void OnSleep()
        {
    
        }
    
        protected override void OnResume()
        {
    
        }
    }
    
  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭

    Can you post your full MainActivity.cs code please? Or a cut-down full project which exhibits the behaviour.

  • amirvenusamirvenus USMember ✭✭✭

    I am thinking it's because your memory is filled up or your Java Heap size is not large enough.

  • TaylorDTaylorD USMember ✭✭✭

    I would suggest either remove the OnStart, OnSleep, OnResume override methods from your App.xaml.cs or place the base calls in the method as shown below if you are actually using the methods:

    protected override void OnStart()
    {
        // Handle when your app starts
        base.OnStart();
    }
    
    protected override void OnSleep()
    {
        base.OnSleep();
    }
    
    protected override void OnResume()
    {
        base.OnResume();
    }
    

    I had the same issue awhile back when I wasn't calling the base calls. Without calling those, the application/XF framework doesn't do the appropriate logic in order to be able to bring the app back from the background state.

Sign In or Register to comment.