猿问

处理屏幕旋转而不会丢失数据-Android

我变得疯狂起来,弄清楚什么是处理屏幕旋转的最佳方法。我在这里阅读了数百个问题/答案,但我真的很困惑。


如何在重新创建活动之前保存myClass数据,以便无需进行其他无用的初始化就可以保留重绘活动的所有内容?


有没有比包裹更清洁,更好的方法?


我需要处理旋转,因为我想在横向模式下更改布局。


public class MtgoLifecounterActivity extends Activity {


    MyClass myClass;


    // Called when the activity is first created

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);


        If ( ?? first run...myClass == null ? ) {

            myClass = new MyClass();

        } else {

            // do other stuff but I need myClass istance with all values.

        }

        // I want that this is called only first time. 

        // then in case of rotation of screen, i want to restore the other instance of myClass which

        // is full of data.

    }


慕斯王
浏览 815回答 3
3回答

青春有我

可以使用覆盖方法onSaveInstanceState()和onRestoreInstanceState()。或者停止调用onCreate()屏幕旋转,只需在清单xml中添加此行android:configChanges="keyboardHidden|orientation"注意:您的自定义类必须实现Parcelable以下示例。@Override&nbsp; &nbsp; public void onSaveInstanceState(Bundle outState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onSaveInstanceState(outState);&nbsp; &nbsp; &nbsp; &nbsp; outState.putParcelable("obj", myClass);&nbsp; &nbsp; }@Overrideprotected void onRestoreInstanceState(Bundle savedInstanceState) {&nbsp;// TODO Auto-generated method stub&nbsp;super.onRestoreInstanceState(savedInstanceState);&nbsp;myClass=savedInstanceState.getParcelable("obj"));}public class MyParcelable implements Parcelable {&nbsp; &nbsp; &nbsp;private int mData;&nbsp;public int describeContents() {&nbsp; &nbsp; &nbsp;return 0;&nbsp;}&nbsp;/** save object in parcel */&nbsp;public void writeToParcel(Parcel out, int flags) {&nbsp; &nbsp; &nbsp;out.writeInt(mData);&nbsp;}&nbsp;public static final Parcelable.Creator<MyParcelable> CREATOR&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= new Parcelable.Creator<MyParcelable>() {&nbsp; &nbsp; &nbsp;public MyParcelable createFromParcel(Parcel in) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return new MyParcelable(in);&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;public MyParcelable[] newArray(int size) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return new MyParcelable[size];&nbsp; &nbsp; &nbsp;}&nbsp;};&nbsp;/** recreate object from parcel */&nbsp;private MyParcelable(Parcel in) {&nbsp; &nbsp; &nbsp;mData = in.readInt();&nbsp;}}

弑天下

这里的问题是您正在丢失应用程序的“状态”。在OOP中,什么是状态?变量!究竟!因此,当您丢失变量的数据时。现在,您可以执行以下操作,找出丢失状态的变量。旋转设备时,当前活动将被完全破坏,即通过onSaveInstanceState() onPause() onStop() onDestroy() 进行创建,而新创建的活动将通过onCreate() onStart() onRestoreInstanceState完全创建。粗体的两个方法onSaveInstanceState()保存当前活动的实例,该实例将被销毁。onRestoreInstanceState此方法恢复上一个活动的保存状态。这样,您就不会丢失应用程序的先前状态。这是您使用这些方法的方式。&nbsp;@Override&nbsp; &nbsp; public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onSaveInstanceState(outState, outPersistentState);&nbsp; &nbsp; &nbsp; &nbsp; outState.putString("theWord", theWord); // Saving the Variable theWord&nbsp; &nbsp; &nbsp; &nbsp; outState.putStringArrayList("fiveDefns", fiveDefns); // Saving the ArrayList fiveDefns&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onRestoreInstanceState(Bundle savedInstanceState, PersistableBundle persistentState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onRestoreInstanceState(savedInstanceState, persistentState);&nbsp; &nbsp; &nbsp; &nbsp; theWord = savedInstanceState.getString("theWord"); // Restoring theWord&nbsp; &nbsp; &nbsp; &nbsp; fiveDefns = savedInstanceState.getStringArrayList("fiveDefns"); //Restoring fiveDefns&nbsp; &nbsp; }编辑:更好的方法:上述维护数据的方法并不是在生产代码/应用程序中维护数据的最佳方法。Google IO 2017引入了ViewModel以保护您的数据免遭配置更改(例如屏幕旋转)。使用变量将所有数据保留在活动中并不是一个好的软件设计,并且违反了“ 单一职责原则”,因此使用ViewModel与活动分开您的数据存储。ViewModel将负责显示数据。活动将负责如何显示数据。如果存储数据的复杂性越来越高,请使用其他存储库类。这只是分离类及其职责的一种方法,这在制作结构良好的应用程序时会走很长一段路。
随时随地看视频慕课网APP

相关分类

Android
我要回答