'Keep chronometer running when changing fragments or screen is rotated

Hi I am new in android studio and i made a chronometer with a couple of other functions in my app but i have a problem when I change the fragment or screen rotates everything is good except my chronometer is set back to 00:00:00. Can someone help me please and show me how can I keep my chronometer running when those changes are made or even let it run when app is closed... here is my code:

public class HomeFragment extends Fragment {

private Chronometer chronometer;
private Button buttonStart, buttonMinus;
private TextView mTVCount, gTVCount, lTVCount;
private int mCount;
private Double gCount=0.00, lCount=0.00;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


}


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

    chronometer = v.findViewById(R.id.simpleChronometer);
    gTVCount = v.findViewById(R.id.textView67);
    mTVCount = v.findViewById(R.id.textView62);
    lTVCount = v.findViewById(R.id.textView69);
    buttonStart = (Button) v.findViewById(R.id.button);
    buttonMinus = (Button) v.findViewById(R.id.button2);
    buttonStart.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            increment();
            chronometer.setBase(SystemClock.elapsedRealtime());
            chronometer.start();


            if (mCount > 20) {
                Toast.makeText(buttonStart.getContext(), "Du wirst zu einem Schornstein!",
                        Toast.LENGTH_SHORT).show();
            } else if (mCount >= 15) {
                Toast.makeText(buttonStart.getContext(), "Du willst doch aufhören...oder?",
                        Toast.LENGTH_SHORT).show();
            } else if (mCount >= 10) {
                Toast.makeText(buttonStart.getContext(), "Ok Großer, mach eine Pause!",
                        Toast.LENGTH_SHORT).show();
            } else if (mCount >= 5) {
                Toast.makeText(buttonStart.getContext(), "Ganz Langsam!",
                        Toast.LENGTH_SHORT).show();
            } else if (mCount > 0) {
                Toast.makeText(buttonStart.getContext(), "Nicht so schlimm...",
                        Toast.LENGTH_SHORT).show();
            }
        }


        private void increment() {
            mCount++;
            mTVCount.setText(String.valueOf(mCount));
            lCount += 5;
            lTVCount.setText(String.valueOf(String.format("%.0f", lCount) + " min"));

            if (lCount >= 60) {
                lTVCount.setText(String.valueOf(String.format("%.2f", lCount / 60)) + " h");
            }

            gCount += 0.36;
            gTVCount.setText(String.valueOf(String.format("%.2f", gCount) + "€"));
        }
    });

    Chronometer timeElapsed = (Chronometer) v.findViewById(R.id.simpleChronometer);
    timeElapsed.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
        @Override
        public void onChronometerTick(Chronometer cArg) {
            long time = SystemClock.elapsedRealtime() - cArg.getBase();
            int h = (int) (time / 3600000);
            int m = (int) (time - h * 3600000) / 60000;
            int s = (int) (time - h * 3600000 - m * 60000) / 1000;
            String hh = h < 10 ? "0" + h : h + "";
            String mm = m < 10 ? "0" + m : m + "";
            String ss = s < 10 ? "0" + s : s + "";
            cArg.setText(hh + ":" + mm + ":" + ss);
        }
    });
    buttonMinus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            decrement();
        }

        private void decrement() {
            mCount--;
            mTVCount.setText(String.valueOf(mCount));
            lCount -= 5;
            lTVCount.setText(String.valueOf(String.format("%.0f", lCount) + " min"));

            if (lCount >= 60) {
                lTVCount.setText(String.valueOf(String.format("%.2f", lCount / 60)) + " h");
            }

            gCount -= 0.36;
            gTVCount.setText(String.valueOf(String.format("%.2f", gCount) + "€"));
        }
    });
    if (savedInstanceState != null){
        mCount = savedInstanceState.getInt("count");
        mTVCount.setText(String.valueOf(mCount));
        gCount = savedInstanceState.getDouble("geld");
        gTVCount.setText(String.valueOf( gCount) + "€");
        lCount = savedInstanceState.getDouble("leben");
        lTVCount.setText(String.valueOf(String.format("%.0f", lCount) + " min"));

        if (lCount >= 60) {
            lTVCount.setText(String.valueOf(String.format("%.2f", lCount / 60)) + " h");
        }

    }


    return v;

}


@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("count", mCount);
    outState.putDouble("geld", gCount);
    outState.putDouble("leben", lCount);
    super.onSaveInstanceState(outState);
}

}



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source