2

我这里有一个奇怪的问题。这是我的片段的 onCreateView:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_game_question, container, false);

    btnAnswerA = (Button) view.findViewById(R.id.btnAnswerA);
    btnAnswerB = (Button) view.findViewById(R.id.btnAnswerB);
    btnAnswerC = (Button) view.findViewById(R.id.btnAnswerC);
    btnAnswerD = (Button) view.findViewById(R.id.btnAnswerD);

    btnAnswerA.setOnClickListener(this);
    btnAnswerB.setOnClickListener(this);
    btnAnswerC.setOnClickListener(this);
    btnAnswerD.setOnClickListener(this);

    if(savedInstanceState != null) {
        btnAnswerA.setVisibility(savedInstanceState.getInt(ANS_A_ENABLED_KEY));
        btnAnswerB.setVisibility(savedInstanceState.getInt(ANS_B_ENABLED_KEY));
        btnAnswerC.setVisibility(savedInstanceState.getInt(ANS_C_ENABLED_KEY));
        btnAnswerD.setVisibility(savedInstanceState.getInt(ANS_D_ENABLED_KEY));
    }

    return view;
}

和 onSaveInstanceState:

@Override
public void onSaveInstanceState(Bundle outState) {

    outState.putInt(ANS_A_ENABLED_KEY, btnAnswerA.getVisibility());
    outState.putInt(ANS_B_ENABLED_KEY, btnAnswerB.getVisibility());
    outState.putInt(ANS_C_ENABLED_KEY, btnAnswerC.getVisibility());
    outState.putInt(ANS_D_ENABLED_KEY, btnAnswerD.getVisibility());

    super.onSaveInstanceState(outState);
}

现在问题出在哪里:在 (savedInstanceState != null) 块中,按钮的可见性根本没有改变。即使我手动编写 - btnAnswerA.setVisibility(4) - 它也不会执行。虽然它确实执行了 Log 命令,但我看到 savedInstanceState 包含所需的值。为什么这样做:当用户旋转屏幕时,我需要保存按钮的状态。

如果您有任何想法为什么会发生以及如何解决它,我将非常感激!

4

0 回答 0