-1

Hello all i want to make use of a global integer variable that i shall be incrementing in 7 different activities according to the users right or wrong choise. The problem is that i every time i implement the variable in each different activity , the value is not kept. Instead i get the default value of the variable. What i want is that every increment i make to the variable is saved , when i use it again in the next variable. Any help appreciated. I have tried and failed :

public class MyApplication extends Application {

private int grade=0;

public int setGrade(int grade) {
    this.grade = grade;
}

public int getGrade() {
    return grade;
}

}

public class lessonOnePhoto extends Activity {

private int grade = ((MyApplication) this.getApplication()).getGrade();

if (rbtn[0].getText().toString().equals("Boy")) {
grade++;
}
else {
Toast.makeText(getApplicationContext(),"Wrong Choise",Toast.LENGHT_SHORT).show();
}
}

4 Answers4

2

The grade you are incrementing is local and private to your activity. It is also a primitive, rather than an object, so grade = .getGrade() will set the local variable to the same value as the global value, it is not some kind of reference.

Instead, do something like this:

MyApplication myApplication = ((MyApplication) this.getApplication());
myApplication.setGrade(myApplication.getGrade()++);

Or implement increment decrement methods.

public class MyApplication extends Application {

private int grade=0;

public int setGrade(int grade) {
    this.grade = grade;
}

public int getGrade() {
    return grade;
}

public void incrementGrade() {
    grade++;
}

public void decrementGrade() {
    grade--;
}
Simon
  • 14,407
  • 8
  • 46
  • 61
0

you have to increment the original application value .. not the copy to maintain the variable in between the activities

if (rbtn[0].getText().toString().equals("Boy")) {
grade++;
}

change to

if (rbtn[0].getText().toString().equals("Boy")) {
((MyApplication) this.getApplication()).setGrade(grade++)
}
Dinesh Kannan
  • 1,255
  • 13
  • 32
  • This won't work. If grade is changed somewhere else, the activity will not have the current value. – Simon Apr 28 '15 at 08:07
0

You can add one method in application class to increment value

public class MyApplication extends Application {

private int grade=0;

public int incrementGrade() {
    this.grade = grade + 1;
}

public int setGrade(int grade) {
    this.grade = grade;
}

public int getGrade() {
    return grade;
}
}

and increment when needed

MyApplication myApplication = ((MyApplication) this.getApplication());
myApplication.incrementGrade();

OR ================

Make that grade static and increment by accessing it in static way

 public static int grade = 0;

access it lie this

 MyApplication.grade ++;
Deniz
  • 12,332
  • 10
  • 44
  • 62
  • It's bad practice to make variables public. Instead, use getters and setters so that in the future, if the implementation of grade in the application changes (e.g. to shared preferences or a database) the rest of the code does not need to change. – Simon Apr 28 '15 at 08:09
0

You can get the result from the activities where the user enters the response and handle it from a MainActivity that manages all the responses.

Another option to avoid storing information in the Application class could be to have a Singleton with a Shared Instance that stores the global variables. However, the use of singletons is considered a bad practice in some cases.

Community
  • 1
  • 1
AlexGuti
  • 3,063
  • 1
  • 27
  • 28