Save data when changing Fragment

0

I want to save the values that are in the counter to when I change the fragment and go back to that, the values I've counted are the. I tried to use SavedInstance so it did not work, not saved, I would like some help with my code.

public class Home extends Fragment {

private Button add, dim;
private TextView opt3010;
private TextView opt3020;
private TextView opt360;
private TextView opt380;
private TextView opt390;
private TextView opt780;
private TextView vst3268;
private TextView vst3250;
private TextView mon;
private TextView vay;
private int cont1, cont2, cont3, cont4, cont5, cont6, cont7, cont8, cont9, cont10 = 0;
private int totalMaq = 0;





@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


    View v = inflater.inflate(R.layout.fragment_home, container, false);

    opt3010 = (TextView) v.findViewById(R.id.opt3010);
    opt3020 = (TextView) v.findViewById(R.id.opt3020);
    opt360 = (TextView) v.findViewById(R.id.opt360);
    opt380 = (TextView) v.findViewById(R.id.opt380);
    opt390 = (TextView) v.findViewById(R.id.opt390);
    opt780 = (TextView) v.findViewById(R.id.opt780);
    vst3250 = (TextView) v.findViewById(R.id.vst3250);
    vst3268 = (TextView) v.findViewById(R.id.vst3268);
    mon = (TextView) v.findViewById(R.id.mon);
    vay = (TextView) v.findViewById(R.id.vay);
    add = (Button) v.findViewById(R.id.button);



    add = (Button) v.findViewById(R.id.button);
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cont1++;

            opt3010.setText("" + cont1);
            totalMaq++;
        }
    });

    add = (Button) v.findViewById(R.id.button2);
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cont2++;

            opt3020.setText("" + cont2);
            totalMaq++;
        }
    });

    add = (Button) v.findViewById(R.id.button6);
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cont3++;

            opt360.setText("" + cont3);
            totalMaq++;
        }
    });

    add = (Button) v.findViewById(R.id.button12);
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cont4++;

            opt380.setText("" + cont4);
            totalMaq++;
        }
    });

    add = (Button) v.findViewById(R.id.button10);
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cont5++;

            opt390.setText("" + cont5);
            totalMaq++;
        }
    });

    add = (Button) v.findViewById(R.id.button11);
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v){
            cont6++;

            opt780.setText("" + cont6);
            totalMaq++;
        }
    });

    add = (Button) v.findViewById(R.id.button13);
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cont8++;

            vst3268.setText("" + cont8);
            totalMaq++;
        }
    });

    add = (Button) v.findViewById(R.id.button14);
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cont9++;

            vst3250.setText("" + cont9);
            totalMaq++;
        }
    });

    add = (Button) v.findViewById(R.id.button8);
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cont10++;

            mon.setText("" + cont10);

        }
    });

    add = (Button) v.findViewById(R.id.button15);
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cont7++;

            vay.setText("" + cont7);

        }
    });

    dim = (Button) v.findViewById(R.id.button5);
    dim.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(cont1<=0){

            }else {
                cont1--;
                opt3010.setText("" + cont1);
                totalMaq--;
            }
        }
    });

    dim = (Button) v.findViewById(R.id.button4);
    dim.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(cont2<=0){

            }else {
                cont2--;
                opt3020.setText("" + cont2);
                totalMaq--;
            }
        }
    });

    dim = (Button) v.findViewById(R.id.button7);
    dim.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(cont3<=0){

            }else {
                cont3--;
                opt360.setText("" + cont3);
                totalMaq--;
            }
        }
    });

    dim = (Button) v.findViewById(R.id.button16);
    dim.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(cont4<=0){

            }else {
                cont4--;
                opt380.setText("" + cont4);
                totalMaq--;
            }
        }
    });

    dim = (Button) v.findViewById(R.id.button9);
    dim.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(cont5<=0){

            }else {
                cont5--;
                opt390.setText("" + cont5);
                totalMaq--;
            }
        }
    });

    dim = (Button) v.findViewById(R.id.button17);
    dim.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v){
            if(cont6<=0){

            }else {
                cont6--;
                opt780.setText("" + cont6);
                totalMaq--;
            }
        }
    });

    add = (Button) v.findViewById(R.id.button19);
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(cont8<=0){

            }else{
                cont8--;
                vst3268.setText("" + cont8);
                totalMaq--;
            }
        }
    });

    add = (Button) v.findViewById(R.id.button18);
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(cont9<=0){

            }else{
                cont9--;
                vst3250.setText("" + cont9);
                totalMaq--;
            }
        }
    });

    add = (Button) v.findViewById(R.id.button20);
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(cont10<=0){

            }else{
                cont10--;
                mon.setText("" + cont10);
            }
        }
    });

    add = (Button) v.findViewById(R.id.button21);
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(cont7<=0){

            }else{
                cont7--;
                vay.setText("" + cont7);
            }

        }
    });
    if (savedInstanceState!= null){
       cont1 = savedInstanceState.getInt("conta");
       opt3010.setText(String.valueOf(cont1));
    }

    return v;}

@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
    super.onSaveInstanceState(outState);

    outState.putInt("conta", cont1);
}

}

    
asked by anonymous 14.05.2018 / 01:10

0 answers