方向变化的android方向、android

2023-09-07 15:57:41 作者:君子藏器于身`帶之而渡

我使用getLastNonConfigurationInstance()来保存对象,同时改变方向在我的活动。现在是pcated德$ P $。什么是另类的最佳方式?该文件说,使用碎片。但我需要使用的活动。

I am using getLastNonConfigurationInstance() to save object while changing orientation in my activity. now it is deprecated. What is the best way of alternative? the documentation says "use Fragment". But i need to use activity.

推荐答案

有关节能状态,可以使用的onSaveInstanceState(包savedInstanceState)。你可以恢复已保存的状态无论是在的onCreate onRestoreInstanceState(包savedInstanceState)

For saving state, use onSaveInstanceState(Bundle savedInstanceState). You can restore saved state either in onCreate or in onRestoreInstanceState(Bundle savedInstanceState).

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
      // Save UI state changes to the savedInstanceState.
      // This bundle will be passed to onCreate if the process is
      // killed and restarted.
      savedInstanceState.putBoolean("MyBoolean", true);
      savedInstanceState.putDouble("myDouble", 1.9);
      savedInstanceState.putInt("MyInt", 1);
      savedInstanceState.putString("MyString", "Hello Android");
      super.onSaveInstanceState(savedInstanceState);
    }

借助捆绑本质上是存储键 - 值的方法对地图,     并且它会被传递给的onCreate,也onRestoreInstanceState在哪里     你会提取值是这样的:

The Bundle is essentially a way of storing a Key-Value Pair" map, and it will get passed in to onCreate and also onRestoreInstanceState where you would extract the values like this:

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
      super.onRestoreInstanceState(savedInstanceState);
      // Restore UI state from the savedInstanceState.
      // This bundle has also been passed to onCreate.
      boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
      double myDouble = savedInstanceState.getDouble("myDouble");
      int myInt = savedInstanceState.getInt("MyInt");
      String myString = savedInstanceState.getString("MyString");
    }
 
精彩推荐
图片推荐