由于特定的屏幕旋转android丢失数据

时间:2014-02-04 14:11:00

标签: android

在你投票之前阅读

您好

我目前遇到一个问题:特定屏幕旋转时数据丢失,
portrait to landscape:我没有丢失任何数据 风景画像:我丢失了数据。

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    getStringFromArray(puzzle);
    getSharedPreferences("firstrun", MODE_PRIVATE).edit().putString("current", currentstring).commit();
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    currentstring = getSharedPreferences("firstrun", MODE_PRIVATE).getString("current", "1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1");
    getArrayFromString(currentstring);
}

我可以从中做出的唯一意义是,当从横向到纵向而不是从纵向到横向时,会调用onCreate()方法。
它还必须以某种方式将初始变量保存到活动之前。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    KEY_DIFFICULTY = getIntent().getStringExtra((String) KEY_DIFFICULTY);
    Log.d("PANIC HERE", "" + KEY_DIFFICULTY);

    if (getSharedPreferences("firstrun", MODE_PRIVATE).getBoolean("firstrun", true)) {
        String easy1 = ""+
                "null,null,null,null,null,null,7,2,null," +
                "3,7,null,null,null,9,null,1,null," +
                "1,null,8,3,7,null,null,null,null," +
                "8,null,null,5,null,null,2,null,6," +
                "null,2,4,null,3,null,1,9,null," +
                "7,null,6,null,null,4,null,null,8," +
                "null,null,null,null,2,6,5,null,1," +
                "null,6,null,8,null,null,null,7,4," +
                "null,1,5,null,null,null,null,null,null";
        getSharedPreferences("firstrun", MODE_PRIVATE).edit().putString("easy1", easy1).commit();
        getSharedPreferences("firstrun", MODE_PRIVATE).edit().putString("easy2", "null").commit();
        getSharedPreferences("firstrun", MODE_PRIVATE).edit().putString("easy3", "null").commit();

        getSharedPreferences("firstrun", MODE_PRIVATE).edit().putString("medium1", "null").commit();
        getSharedPreferences("firstrun", MODE_PRIVATE).edit().putString("medium2", "null").commit();
        getSharedPreferences("firstrun", MODE_PRIVATE).edit().putString("medium3", "null").commit();

        getSharedPreferences("firstrun", MODE_PRIVATE).edit().putString("hard1", "null").commit();
        getSharedPreferences("firstrun", MODE_PRIVATE).edit().putString("hard2", "null").commit();
        getSharedPreferences("firstrun", MODE_PRIVATE).edit().putString("hard3", "null").commit();

        getSharedPreferences("firstrun", MODE_PRIVATE).edit().putString("current", "null").commit();

        getSharedPreferences("firstrun", MODE_PRIVATE).edit().putBoolean("firstrun", false).commit();
    }

    if(KEY_DIFFICULTY != null){
        puzstring = KEY_DIFFICULTY +"1";
        currentstring = getSharedPreferences("firstrun", MODE_PRIVATE).getString(puzstring, "1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1");
        getSharedPreferences("firstrun", MODE_PRIVATE).edit().putString("current", currentstring).commit();
    }else{
        currentstring = getSharedPreferences("firstrun", MODE_PRIVATE).getString("current", "1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1");
    }
    getArrayFromString(currentstring);
    grid = new GridView(this);
    setContentView(grid);
    grid.requestFocus();
    startpuzzle = puzzle;
}

2 个答案:

答案 0 :(得分:0)

您的问题听起来很奇怪,但我认为您必须覆盖方法onPauseonResume,因为当您旋转屏幕时,这些方法将被调用,因此您必须在旋转屏幕时存储您的状态( onPause)并在重绘屏幕时恢复状态(onResume)。 请详细了解活动生命周期http://developer.android.com/guide/components/activities.html,了解其工作原理非常重要。

答案 1 :(得分:0)

当手机将风景转为纵向时调用了onDestroy()方法,但是当从纵向转为横向时则不然。 只有在活动被销毁时才会调用onCreate()方法。