-2

i currently have the problem that i don't know exactly how to manage my variables.

Currently I have 5 activities. And thereby 5 Java classes. From class 3 the Var. are always to be seen.

The first two classes are only provided with buttons. In the third class I defined my variables (public static int Money = 100). Over class 4 I come to class 5 with which the variables are changed (Butto (-10)). By implement an update I can change the var. by clicking on a button (In class 5) (e.g. 90 money).

Here's my problem. If I go to activity 4 now (with the back button to the right of the home button), it's still worth the old value (100). If I go to class 3, it´s still the same value. When I want to go back now I land on activity 2 which is empty. But when I go now again in 3 or 4, the values are updated (now 90 money). How can this be done directly?

What is the best way to deal with such variables? I certainly have 10 other values who are affected by the problem.

Thats the code in class 3, where I defind the Variable, equals class 4.

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_class3);

    //Header
    TextView MoneyView = findViewById(R.id.outputmoney);
    MoneyView.setText(String.valueOf(class3.money));......}

Thets the declaration in class 4 and 5

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_class4);

TextView moneyView = findViewById(R.id.outputmoney);
MoneyView.setText(String.valueOf(class3.money));

BG and thank you

  • Check this question. Hope it solves your problem. https://stackoverflow.com/q/2091465/9611523 –  May 25 '18 at 18:12
  • You should definitely make your "variables" (*fields* in Java) to `private` and use getters and setters. I'm not sure if your problem comes from that, but it's a really good way of managing your fields. You will be able to easily handle the access to your fields. – Lutzi May 25 '18 at 18:21

2 Answers2

0

I am not extremely familiar with Android development, and there might be a quicker way to accomplish this, but you might consider implementing the singleton pattern in this scenario; if you are not familiar with it, that means to create a new class (separate from any of your activities) of which only one instance exists in memory for the entire lifecycle of the application. When a value is provided by the user, have the activity in turn set the value on your singleton object, which will be accessible to any of your activities. Here is a reference if needed: https://www.tutorialspoint.com/design_pattern/singleton_pattern.htm

0

As the other suggested, you may want to use a Singleton. I would recommend never using static values within any Activities. If you need that value globally, move it to your Application level.

I'll just give you a basic example, but you can do a lot more with a solution similar to this.

First, create a class extending Application.

public class MyApp extends Application() {

     public static int money = 100;
     // any other values you want in here

}

And make sure you update your manifest to use this new class.

<application
        android:name="com.example.MyApp"
        //...
        >

Now, in any Activity you can access it.

MoneyView.setText(String.valueOf(MyApp.money));

Also, if you open an Activity, modify that value, and then return to a previous Activity, it will not reflect the updated value. To update it to the updated value, you're going to want to move your logic to onResume.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_class3);
}

protected void onResume() {
    //Header
    TextView MoneyView = findViewById(R.id.outputmoney);
    MoneyView.setText(String.valueOf(MyApp.money));
}
advice
  • 5,778
  • 10
  • 33
  • 60