0

I'm on a simple project. I have one activity which contains one fragment (fragment attached using java). The Fragment has 2 Textview which takes integer and string to change dynamically, but while rotating the screen, fragment not working for restoring or continuing the dynamic change. Even my button click stop working. But onSaveInstanceState(Bundle outState) should work properly, where is the bug. My code below:

Activity java code:

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();
    }
}

Fragment java code:

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);

    }
}

Fragment xml code:

<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 Answers1

0

Stop calling onCreate() on screen rotation. In your AndroidManifest.xml

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