简体   繁体   中英

Fragments seem to be stacking on top of each other - causing problems, android

I've been pulling my hair out over this for the whole day, as I'm very new to fragments. Basically, I have a fragment in an app I have that keeps track of a player's currency. I want to reuse one fragment throughout the entire app, as it has references to a singleton class that stores the players currency and sets a TextView to display this currency.

The problem is that in every activity I initialize a new fragment when the activity starts, like so:

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.fmang2, new fragment1());
        ft.commit();

this causes the fragments to stack, which is a problem because the fragments are constantly adding currency to the player's balance. So, if I have 3 created 3 activities, players will be receiving currency 3x as fast as they should be.

How do I fix this? I've tried creating fragments with XML too, but that doesn't help. For reference, here is the code in my fragment:

package com.example.bunzclicker;


import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.example.bunzclicker.bunz.Bunz;

import java.math.BigDecimal;
import java.util.Timer;
import java.util.TimerTask;

public class fragment1 extends Fragment {
    private TextView bunz_count;
    private TextView money_count;
    private Bunz bunz;
    private Handler handler;
    private HandlerThread mHandlerThread;
    int delay = 1000;
    View view;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        bunz = Bunz.getInstance();
        handler = new Handler();


        view = inflater.inflate(R.layout.fragment1, container, false);



        handler.postDelayed(new Runnable(){
            public void run(){
                update(view);
                handler.postDelayed(this, delay);
            }
        }, delay);






        return view;
    }





    public void update(View view){
        bunz_count = (TextView) view.findViewById(R.id.final_bunz_count);
        money_count = (TextView) view.findViewById(R.id.final_money_count);
        //System.out.println(bunz.getBaker1());


        BigDecimal number = ((BigDecimal.valueOf
                (bunz.getBaker1()).multiply(BigDecimal.valueOf(.1))));
//        ).add((BigDecimal.valueOf(bunz.getBaker2()).multiply(BigDecimal.valueOf(.2)))).
//                add((BigDecimal.valueOf
//                        (bunz.getBaker3()).multiply(BigDecimal.valueOf(.4)))).
//                add((BigDecimal.valueOf
//                        (bunz.getBaker4()).multiply(BigDecimal.valueOf(.8)))).
//                add((BigDecimal.valueOf(bunz.getBaker5()).multiply(BigDecimal.valueOf(1)))).
//                add((BigDecimal.valueOf(bunz.getBaker6()).multiply(BigDecimal.valueOf(2)))).
//                add((BigDecimal.valueOf(bunz.getBaker7()).multiply(BigDecimal.valueOf(4)))).
//                add((BigDecimal.valueOf(bunz.getBaker8()).multiply(BigDecimal.valueOf(5)))).
                //add((BigDecimal.valueOf(bunz.getBaker9()).multiply(BigDecimal.valueOf(10))));
        //System.out.println(number);
        bunz.setBunz(bunz.getBunz().add((number)));
        bunz_count.setText("Bunz: " + bunz.getBunz());
        money_count.setText("Money: " + bunz.getMoney());
        System.out.println("bunz" + bunz.getBunz());

    }
}



Your Handler keeps posting update() even when the fragment has been put on pause, you should move the handler code to onResume() and use handler.removeCallbacksAndMessages(null) in onPause to prevent update() calls when the fragment is paused. You'll need to do something along the following lines,

@Override
public void onResume() {
    super.onResume();
    handler = new Handler();
    handler.postDelayed(new Runnable(){
        public void run(){
            update(view);
            handler.postDelayed(this, delay);
        }
    }, delay);
}

@Override
public void onPause() {
    handler.removeCallbacksAndMessages(null);
    super.onPause();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM