方向变化的android [英] Orientation Changes in android

查看:123
本文介绍了方向变化的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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");
    }

这篇关于方向变化的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆