方向更改后查看不更新

时间:2016-07-14 11:04:11

标签: java android landscape

我有一个适配器类MyAdapter -

 @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ViewHolder viewHolder = (ViewHolder) view.getTag();
        viewHolder.time.setText(strText);
        viewHolder.time.setTextColor(0xff000000);
        viewHolder.time.setTextSize(17);
        viewHolder.time.setVisibility(View.VISIBLE);
        viewHolder.time.setBackgroundColor(nColor);
        viewHolder.time.setPadding(25,25,25,25);
        MyFragment my= new MyFragment();
        my.setLandScape1(strText,nColor);
        view.invalidate();
        }
    }

MyFragment

public void setLandScape1(String time1,int time_Bcolor1){
    this.delay_time1 = time1;
    this.delay_time_BColor1 = time_Bcolor1;
}

@Override
public void onSaveInstanceState(Bundle outState) {
    outState.putString("Time",delay_time1);
    outState.putInt("BColor",delay_time_BColor1);
    super.onSaveInstanceState(outState);
}

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null) {
       delay_time1 = savedInstanceState.getString("Time");
        Log.d("hi","delay_time1 = " + delay_time1);
        delay_time_BColor1 = savedInstanceState.getInt("BColor");
        Log.d("hi","delay_time_BColor1  =" + delay_time_BColor1);
    } }

日志是 -

水平转动时,

delay_time1 = 45

delay_time_BColor1  = 65000

再次垂直时,

 delay_time1 = 0

delay_time_BColor1  = 0

为什么在转为肖像时变为0,为什么在横向拍摄时,我会在onCreate()中获取值,但仍然无法更新视图。

1 个答案:

答案 0 :(得分:0)

将逻辑从onCreate移至onRestoreInstanceState()

重新创建视图后,系统会调用onRestoreInstanceState而不是onCreate()