Search code examples
javaandroidoncreateonresume

Initializing variables in onCreate, but updating them in onResume()


Here is my situation: I have an OnCreate code like the following:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bunz = Bunz.getInstance(); //getting instance of bunz
        bunz.setBunz(50);
        bunz.setMoney(0);
        bunz.setIncrement(1);
        Button upgradeButton = (Button) findViewById(R.id.upgradeButton);
        upgradeButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                Intent startIntent = new Intent(getApplicationContext(), UpgradeMenu.class);

                startActivity(startIntent);
            }
        });
        moneyCount = (TextView) findViewById(R.id.moneyCount);
        bunzCount = (TextView) findViewById(R.id.bunzCount);
        ImageButton bun = (ImageButton) findViewById(R.id.bun);

    }

Notice how in my OnCreate code, I do 2 things; first, I initialize all the values I need:

        bunz.setBunz(50);
        bunz.setMoney(0);
        bunz.setIncrement(1);

and then I display these values on TextViews and set up some Buttons and intents:

      Button upgradeButton = (Button) findViewById(R.id.upgradeButton);
        upgradeButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                Intent startIntent = new Intent(getApplicationContext(), UpgradeMenu.class);

                startActivity(startIntent);
            }
        });
        moneyCount = (TextView) findViewById(R.id.moneyCount);
        bunzCount = (TextView) findViewById(R.id.bunzCount);
        ImageButton bun = (ImageButton) findViewById(R.id.bun);

I'm new to Android studio, and here is the problem I'm having. I want to use onResume() to update these values in the TextView (I update them in another activity) every time I go back to this activity. However, if I move all the code in onCreate into onResume, then every time I go back to this activity, the values will be set to 50,0, and 1. I understand I could use a boolean, so that onCreate() triggers the first time the app is launched, but onResume() doesn't trigger, and then onResume() triggers after that, and simply copy and paste the second half of the onCreate code into onResume(), but that seems inefficient, and isn't how Android studio is designed to work. Can I somehow initialize the values in another location?

I have a global Bunz class that looks like the following:

public class Bunz {
    private int bunz;
    private int money;
    private int increment;
    //singleton code


    private static Bunz instance;
    private Bunz(){

    }
    public static Bunz getInstance(){
        if (instance == null){
            instance = new Bunz();
        }
        return instance;
    }

    public int getBunz() {
        return bunz;
    }

    public void setBunz(int num){
        bunz = num;
    }

    public int getMoney(){
        return money;
    }
    public void setMoney(int num){
        money = num;
    }
    public int getIncrement(){
        return increment;
    }
    public void setIncrement(int num){
        increment = num;
    }

}

so maybe I could initialize these values here somehow?

Thanks!


Solution

  • here's one thing you could alternatively do:

     public static Bunz getInstance(){
            if (instance == null){
                instance = new Bunz();
                instance.setBunz(50);
                instance.setMoney(0);
            }
            return instance;
        }
    

    in your instance creation here, try setting the values you want here, instead of in onCreate of the app.

    you could just be making the changes in the constructor as well.