为什么不调用OnCreate()方法之外的其他活动生命周期

时间:2018-06-27 02:58:43

标签: android xamarin xamarin.android android-lifecycle activity-lifecycle

所有活动生命周期都被覆盖,因此我希望它们会在某个时候被触发,但事实并非如此。尽管我只需要OnResume()方法,但我还是尝试覆盖所有它们进行测试。有人已经遇到这种情况并解决了吗?我根本无法触发它:(

我试图触发它们的原因:
-按主页按钮触发OnPause和OnStop-失败
-返回到应用以触发OnResume,OnRestart-失败
-按后退按钮触发OnPause和OnStop-失败
-返回到应用以触发OnResume,OnRestart-失败
-转到下一个活动以触发OnPause和OnStop-失败
-按后退按钮触发上一个活动的OnResume-失败
-重置应用以触发OnCreate,OnResume-OnCreate成功,OnResume失败
-杀死应用以触发OnPause,OnStop和OnDestroy-失败

发生了什么事?我试图将NoHistory设置为true,但这只会触发OnCreate。将其设置为false并返回上一期

代码:

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
}

1 个答案:

答案 0 :(得分:0)

我不知道真正发生了什么,但是在我关闭Visual Studio然后用我拥有的项目再次打开它之后,现在正在调用其他生命周期。我认为问题出在Visual Studio中。

在重新启动之前,我先对其进行了清理,然后进行了重建,然后将其部署到了模拟器中。我确实在物理设备中部署了同样的东西,但结果却相同,其他生命周期都没有运行。

重启后,同样的过程,现在很好。

清理然后重新构建无法正常工作,但是重新启动Visual Studio很棒:D