片段不保存实例状态

时间:2017-10-02 03:42:33

标签: java android android-fragments orientation android-lifecycle

当你改变Android设备的方向时,我正在尝试保存状态数据,因为我知道它会破坏活动和碎片。对于其他布局内部的片段,我是否需要做一些特殊的事情(我的主要活动xml中有两个片段)。我以为我可以使用saveInstanceState简单地处理保存数据,但似乎永远不会被调用,因为传递给onCreateView的bundle总是为null。有什么想法吗?我确定我在这里遗漏了一些明显的东西。

public class ControlFrag extends Fragment implements View.OnClickListener{


int hours, min, sec;
TextView text;
Button start;
Async async;
boolean clicked;

private static final String THREAD_STATUS = "thread status";
private static final String HOUR = "hour";
private static final String MINUTE = "minute";
private static final String SECOND = "second";

public ControlFrag() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_control, container, false);
    text = (TextView) v.findViewById(R.id.timer);
    start = (Button) v.findViewById(R.id.start);
    start.setOnClickListener(this);

    if (savedInstanceState != null) {
        text.setText("Test");
        boolean isRunning = savedInstanceState.getBoolean(THREAD_STATUS);
        if (isRunning) {
            sec = savedInstanceState.getInt(SECOND);
            text.setText("" + sec);
            async = new Async();

            async.execute(sec);
        }
    }
    else {
        async = new Async();
    }
    return v;
}

@Override
public void onClick(View view) {
    if (async.getStatus() != AsyncTask.Status.RUNNING) {
        async = new Async();
        async.execute(0);
    }
}


@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    if (async != null && async.getStatus() == AsyncTask.Status.RUNNING) {
        outState.putBoolean(THREAD_STATUS, true);
        outState.putInt(SECOND, sec);

        async.cancel(true);
    }
    else {
        outState.putBoolean(THREAD_STATUS, false);
    }
}


@Override
public void onDestroy() {
    super.onDestroy();
    if (async != null && async.getStatus() == AsyncTask.Status.RUNNING) {
        async.cancel(true);
        async = null;
    }
}

private class Async extends AsyncTask<Integer, String, Void> {
    @Override
    protected void onPostExecute(Void aVoid) {

        super.onPostExecute(aVoid);
    }

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);
        text.setText(values[0] + values[1] + values[2]);
    }

    @Override
    protected Void doInBackground(Integer... integers) {
        sec = integers[0];
        while (sec < 100) {
            try {
                sec++;
                String second;
                if(sec > 59){
                    sec = 0;
                }

                if (sec < 10) {
                    second = "0" + sec;
                } else {
                    second = "" + sec;
                }

                publishProgress("" + sec, ":" + sec, ":" + second);
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}

}

活动代码:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

public Button nextScreen;
public TextView timer;
public ControlFrag control;
public ListFrag list;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    control = new ControlFrag();
    list = new ListFrag();
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    if (savedInstanceState == null) {
       // transaction.replace(R.id.fragment_container, control, "portraitControl");
    }

    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        transaction.replace(R.id.fragment_container1, list, "landscapeList");
    }
    else {
        nextScreen = (Button) findViewById(R.id.nextScreen);
        nextScreen.setOnClickListener(this);
    }
    transaction.commit();
}

@Override
public  void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString("Test", "GO");
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
        nextScreen.setText(savedInstanceState.getString("Test"));
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();

}




@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {

    }
}

@Override
public void onClick(View view) {
    if (getSupportFragmentManager().findFragmentByTag("portraitControl") != null) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container, list, "portraitList");
        transaction.commit();
    }
    else {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container, control, "portraitControl");
        transaction.commit();
    }

}

}

0 个答案:

没有答案