Android - 单片段数据在屏幕旋转时保存不正常

时间:2016-02-25 05:34:52

标签: android android-fragments

我在一个简单的项目上。我有一个活动,其中包含一个片段(使用java附加的片段)。 Fragment有2个Textview,它采用整数和字符串来动态更改,但在旋转屏幕时,片段无法恢复或继续动态更改。甚至我的按钮点击停止工作。但是onSaveInstanceState(Bundle outState)应该可以正常工作,bug在哪里。我的代码如下:

活动java代码:

package com.blogspot.imti.mygame;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class FragmentHolder extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_holder);

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        GameA gamea = new GameA();
        fragmentTransaction.add(R.id.holder_rl1, gamea, "gameATag");
        fragmentTransaction.commit();
    }
}

片段java代码:

package com.blogspot.imti.mygame;


import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


/**
 * A simple {@link Fragment} subclass.
 */
public class GameA extends Fragment implements View.OnClickListener{
    TextView a_tv2;
    EditText a_et1;
    Button a_b1;
    int stepClickCounter;
    String [] stepDescriptions = {"Step 1 working",
            "Step 2 working",
            "Step 3 working",
            "Step 4 working",
            "Step 5 working",
            "Step 6 working",
            "Step 7 working",
            "Step 8 working"};
    String stepMessage, stepText;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        ((AppCompatActivity) getActivity()).getSupportActionBar().hide();
        View view = inflater.inflate(R.layout.fragment_game, container, false);

        if(savedInstanceState!=null){
        stepClickCounter = savedInstanceState.getInt("stepCounter");        
            stepMessage = (String)savedInstanceState.get("stepDescription");
            TextView tv = (TextView)view.findViewById(R.id.gamea_tv2);
            tv.setText(stepMessage);
        }

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        a_tv2 = (TextView)getActivity().findViewById(R.id.gamea_tv2);
        a_b1 = (Button)getActivity().findViewById(R.id.gamea_b1);

        a_b1.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        stepClickCounter++;

        goSet();

    }
    public void goSet(){
        if (stepClickCounter==0) {
            stepMessage = stepDescriptions[0];
            a_tv2.setText(stepMessage);
            Toast.makeText(getActivity(), "step 1", Toast.LENGTH_LONG).show();

        }else if (stepClickCounter==1) {
            stepMessage = stepDescriptions[1];
            a_tv2.setText(stepMessage);
            Toast.makeText(getActivity(), "step 2", Toast.LENGTH_LONG).show();

        }else if (stepClickCounter==2) {
            stepMessage = stepDescriptions[2];
            a_tv2.setText(stepMessage);
            Toast.makeText(getActivity(), "step 3", Toast.LENGTH_LONG).show();

        }else if (stepClickCounter==3) {
            stepMessage = stepDescriptions[3];
            a_tv2.setText(stepMessage);
            Toast.makeText(getActivity(), "step 4", Toast.LENGTH_LONG).show();

        }else if (stepClickCounter==4) {
            stepMessage = stepDescriptions[4];
            a_tv2.setText(stepMessage);
            Toast.makeText(getActivity(), "step 5", Toast.LENGTH_LONG).show();

        }else if (stepClickCounter==5) {
            stepMessage = stepDescriptions[5];
            a_tv2.setText(stepMessage);
            Toast.makeText(getActivity(), "step 6", Toast.LENGTH_LONG).show();

        }else if (stepClickCounter==6) {
            stepMessage = stepDescriptions[6];
            a_tv2.setText(stepMessage);
            Toast.makeText(getActivity(), "step 7", Toast.LENGTH_LONG).show();
        }else if (stepClickCounter==7) {
            stepMessage = stepDescriptions[7];
            a_tv2.setText(stepMessage);
            //a_et1.setHint("Final Result Here");
            a_b1.setText("Finish");
        }else {
            Toast.makeText(getActivity(), "Show output", Toast.LENGTH_LONG).show();
        }
    }


    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("stepCounter", stepClickCounter);
       outState.putString("stepDescription", stepMessage);

    }
}

片段xml代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.blogspot.imti.mygame.GameA"
    android:background="#ECEFF1">

    <ScrollView
        android:id="@+id/gamea_sv1"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        >
        <RelativeLayout
            android:id="@+id/gamea_rl1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content">

            <TextView
                android:id="@+id/gamea_tv2"
                android:layout_width="wrap_content"
                android:layout_height="140dp"
                android:text="Step 1 working"
                android:textSize="22dp"
                android:textColor="#FF7043"
                android:textStyle="bold"
                android:paddingLeft="15dp"
                android:paddingRight="15dp"
                android:paddingTop="10dp"
                android:paddingBottom="5dp"/>


            <Button
                android:id="@+id/gamea_b1"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:text="NEXT STEP"
                android:textStyle="bold"
                android:textSize="24dp"
                android:textColor="#ECEFF1"
                android:background="#2196F3"
                android:layout_below="@id/gamea_tv2"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"/>
            <ImageView
                android:id="@+id/gamea_iv1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/birthdayicon"
                android:layout_below="@id/gamea_b1"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="10dp"/>
        </RelativeLayout>

    </ScrollView>

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

停止在屏幕旋转时调用onCreate()。 在AndroidManifest.xml中

<activity
    android:name="com.example.Activity"
    android:label="@string/app_name" 
    android:configChanges="keyboardHidden|orientation|screenSize">