onSaveInstanceState () and onRestoreInstanceState ()

前端 未结 13 2185
孤城傲影
孤城傲影 2020-11-22 04:48

I\'m trying to save and restore the state of an Activity using the methods onSaveInstanceState() and onRestoreInstanceState().

相关标签:
13条回答
  • 2020-11-22 05:54

    I can do like that (sorry it's c# not java but it's not a problem...) :

    private int iValue = 1234567890;
    
    function void MyTest()
    {
        Intent oIntent = new Intent (this, typeof(Camera2Activity));
        Bundle oBundle = new Bundle();
        oBundle.PutInt("MYVALUE", iValue); //=> 1234567890
        oIntent.PutExtras (oBundle);
        iRequestCode = 1111;
        StartActivityForResult (oIntent, 1111);
    }
    

    AND IN YOUR ACTIVITY FOR RESULT

    private int iValue = 0;
    
    protected override void OnCreate(Bundle bundle)
    {
        Bundle oBundle =  Intent.Extras;
        if (oBundle != null)
        {
            iValue = oBundle.GetInt("MYVALUE", 0);
            //=>1234567890
        }
    }
    
    private void FinishActivity(bool bResult)
    {
        Intent oIntent = new Intent();
        Bundle oBundle = new Bundle();
        oBundle.PutInt("MYVALUE", iValue);//=>1234567890
        oIntent.PutExtras(oBundle);
        if (bResult)
            {
                SetResult (Result.Ok, oIntent);
            }
        else
            SetResult(Result.Canceled, oIntent);
        GC.Collect();
        Finish();
    }
    

    FINALLY

    protected override void OnActivityResult(int iRequestCode, Android.App.Result oResultCode, Intent oIntent)
    {
        base.OnActivityResult (iRequestCode, oResultCode, oIntent);
        iValue = oIntent.Extras.GetInt("MYVALUE", -1); //=> 1234567890
    }
    
    0 讨论(0)
提交回复
热议问题