Bootstrap

在不丢失数据的情况下处理屏幕旋转 - Android

我正在疯狂地弄清楚处理屏幕旋转的最佳方法是什么。

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

有没有比 Parcelable 更清洁、更好的方法?

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

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.
    }

解决办法:

在清单的活动标签中,你应该提到


如果您使用的是 Android 2.3(API 级别 13)及以上,请使用


它应该工作。

它仅适用于活动标签而不适用于应用程序标签

好的,今天的知识就分享到这儿