jace
jace

Reputation: 1674

Why Other Activity Life Cycle are not being called other than OnCreate() method

All activity life cycle are overridden so I am expecting that they will be triggered on some point but they don't. I tried to override all of them for a test though I only need the OnResume() method. Does somebody already encountered this scenario and solved it? I can't trigger it at all :(

What I tried to trigger them:
-Pressed home button to trigger OnPause and OnStop - Failed
-Return to App to trigger OnResume, OnRestart - Failed
-Pressed back button to trigger OnPause and OnStop - Failed
-Return to App to trigger OnResume, OnRestart - Failed
-Go next activity to trigger OnPause and OnStop - Failed
-Pressed back button to trigger OnResume of previous activity - Failed
-Reset App to trigger OnCreate, OnResume - Success on OnCreate, Failed on OnResume
-Kill the app to trigger OnPause, OnStop and OnDestroy- Failed

What's happening? I tried to put NoHistory to true but it only trigger the OnCreate. Set it to false and came back to previous issue

CODE:

using System;
using Android.App;
using Android.OS;
using Android.Widget;
using Android.Content;
using Android.Views;
using BLL;
using Android.Views.InputMethods;
using static Android.Views.View;
using Android.Preferences;


namespace H2POS
{
    /// <summary>
    /// Created By: Makath Valdez
    /// </summary>
    [Activity(Label = "Activity_Login", NoHistory = false, ScreenOrientation = Android.Content.PM.ScreenOrientation.SensorLandscape, Theme = "@style/Theme.FullScreenBaseDesign")]
    public class Activity_Login : Activity
    {
        #region Global Variables
        KeypadHandler keypadHandlerUser;
        KeypadHandler keypadHandlerPass;
        EditText etUsername;
        EditText etPassword;

        //keypad initialization
        Button btn1;
        Button btn2;
        Button btn3;
        Button btn4;
        Button btn5;
        Button btn6;
        Button btn7;
        Button btn8;
        Button btn9;
        Button btn0;
        Button btn00;

        ImageButton btnDel;

        Button btnLogin;
        LinearLayout llUsername;
        LinearLayout llPassword;
        #endregion
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.Layout_Login);
            Window.SetSoftInputMode(SoftInput.StateAlwaysHidden);

            try
            {
                m2_initialization();
                m4_setUpAllClickable();

                ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
                bool databaseInitialized = prefs.GetBoolean(ENTITIES.STATIC.ENT_SystemValues.databaseInitialized, false);

                if (!databaseInitialized)
                {
                    new BLL_HardCode().upsertSystemValues();
                    ISharedPreferences prefs2 = PreferenceManager.GetDefaultSharedPreferences(this);
                    ISharedPreferencesEditor editor = prefs2.Edit();
                    editor.PutBoolean(ENTITIES.STATIC.ENT_SystemValues.databaseInitialized, true);
                    // editor.Commit();    // applies changes synchronously on older APIs
                    editor.Apply();        // applies changes asynchronously on newer APIs
                }
            }
            catch (Exception e)
            {
                UIHelper.ToastMessage(this, ENTITIES.STATIC.ENT_NegativeResponse.somethingWentWrong);
            }
        }

        protected override void OnResume()
        {
            UIHelper.ToastMessage(this, "OnResume was called");
            base.OnResume();

            //check if auto date time is enabled
            new AccountSettings().IsAutoDateTimeEnabled(this);
        }

        protected override void OnPause()
        {
            UIHelper.ToastMessage(this, "OnPause was called");
            base.OnPause();
        }

        protected override void OnStop()
        {
            UIHelper.ToastMessage(this, "OnStop was called");
            base.OnStop();
        }

        protected override void OnDestroy()
        {
            UIHelper.ToastMessage(this, "OnDestroy was called");
            base.OnDestroy();
        }

        protected override void OnRestart()
        {
            UIHelper.ToastMessage(this, "OnRestart was called");
            base.OnRestart();
        }
    }

    //Other code removed
}

Upvotes: 1

Views: 297

Answers (1)

jace
jace

Reputation: 1674

I don't know what really happened but after I closed my Visual Studio then open it again with the project I have, other life cycle are now being called. I think the problem is in the Visual Studio.

Before restart, I cleaned it then rebuild then deployed to emulator. Same thing I did to deploy in my physical device but same result, other life cycle are not running.

After restart, same process and it's good now.

Clean then Rebuild didn't work but Restarting Visual Studio is awesome :D

Upvotes: 0

Related Questions