Evan
Evan

Reputation: 2040

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!

Upvotes: 1

Views: 1425

Answers (2)

Taseer
Taseer

Reputation: 3492

While your code uses statics, which I believe is unnecessary. Statics are not your average goto solution, they come with a hefty price of an object not eligible for GC.

You can get the result from the second activity via onActivityResult method.

First, start second activity using startAtivityForResult() //This takes in a request code(Int), it can be whatever you set.

First activity

Intent intent = new Intent(this, SecondActivity.class);
startActivityForResult(intent , 100);

Second Activity

//Do you work in the second activity, generate new data

Intent returnIntent = new Intent();
returnIntent.putExtra("bunz", 100);
returnIntent.putExtra("money", 200);
returnIntent.putExtra("increment", 2);
setResult(Activity.RESULT_OK, returnIntent);
finish();

Capture Second Activity Result

This code is supposed to be written in your first activity.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == 100) {  //Remember the code we set in startActivityForResult? This is where we identify our work
     if(resultCode == Activity.RESULT_OK){   //Code to check if data is passed
        Int bunz =data.getIntExtra("bunz")
        bunz.setBunz(bunz)

       .....
    }
  }
}

Upvotes: 1

a_local_nobody
a_local_nobody

Reputation: 8191

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.

Upvotes: 3

Related Questions