In Reminder1.java I have the int hourOfDay2 and int minute2 variables. These equals with the hourOfDay and minute variable of the TimePickerDialog.
In myfile.java i want to examine the value of these variables. How to do that?
One thing I've seen posted here on SO a few times, and that I've used for global variables, is an extended Application class, like so:
public class GlobalVars extends Application {
private static int hourOfDay2;
private static int minute2;
public static int getHourOfDay() {
return hourOfDay2;
}
public static int getMinute() {
return minute2;
}
public static void setHourOfDay(int hour) {
hourOfDay2 = hour;
}
public static void setMinute(int minute) {
minute2 = minute;
}
}
Add it to your Application tag in the manifest, like so:
<application android:name=".GlobalVars" />
Then, in your main class's onCreate, or wherever necessary, just call GlobalVars.setMinute(int) to initialize them, then you can access them the same way in any other class, with int x = GlobalVars.getMinute().
In android, you can use Bundle to pass values.
Related
I am Using GlobalVariable file to hold data during my entire application, but what happens is it returns null value if i switch to another application and returns back.
below is my code :
In Manifest :
<application
..
android:name=".MyApplication" >
For Class of global variables :
public class MyApplication extends Application {
public int rowId = 0;
}
inside the activities
int mRowId = ((MyApplication) getApplicationContext()).rowId;
you need to make it static and final but use it with getters and setters only.
Something like the following
public class MyApplication extends Application {
public final static int rowId = 0;
dont forget your initializing and setters and getters please :
int rowId;
public int getRowId() {
return rowId;
}
public void setRowId(int rowId) {
this.rowId = rowId;
}
to set the values from outside the class, something like:
MyApplication.rowId = //whatever returns int
to get the values from outside/inside the class, something like:
int TempInt = MyApplication.rowId; // TempInt will have the value of rowId
check What are setters and getters :
https://dzone.com/articles/why-should-i-write-getters-and-setters
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();
}
}
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--;
}
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++)
}
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 ++;
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.
in java i know this code is a good programming practice,
but i read some article there are good programming practice that
is bad for Android, i just want to know if this type of code
can affect the Aplication Performance issue when it comes to android programing?
for example
public class Main {
static int age = getAge(10); /***************** THIS LINE */
public static void main(String[] args) {
System.out.println(age);
}
private static int getAge(int i) { /***************** THIS METHOD */
i = i + 1;
return i;
}
}
This situation seems perfectly fine and wouldn't effect performance.
I personally would be careful with this practice though, you could potentially call a method dependant on variables that are yet to be initialized.
public class Main {
static int age = getDogYears(10);
int dogRatio; // dogRatio is not yet initialized
public static void main(String[] args) {
System.out.println(age);
dogRatio = 7;
}
private static int getDogyears(int i) {
i = i * dogRatio; // null pointer exception because dogRatio is not initialized
return i;
}
}
If the problem is really this simple though I would also ask why you wouldn't just make age = 11;
You're not going to see any noticeable performance hits, and I can't see anything wrong with the code, but it could be improved.
This method is a lot easier to read and cleans up the code a little:
private static int getAge(int i){
return i++;
}
You also then have to wonder why you even need it. As you're passing it an integer and not the value of a variable, why not just initialise it without the method call?
static int age = 11;
I have a class called Scouting, and it runs a function in a different class ScoutingFormData(different java file in the same package). I want it so that an integer defined in Scouting can be edited from ScoutingFormData. I defined the int:public int SFID=-1; in the main class of Scouting, but I can't figure out how to edit that int from ScoutingFormData.
Don't make your instance fields public, use getter and setters.
public int getField() {
return field;
}
public void setField(int field) {
this.field = field;
}
This is if your field needs to be an instance field.
If you need a field that belongs to the class ScoutingObject you need to make it static
public static int SFID=-1;
Then you can access it like this:
ScoutingObject.SFID
add static modifer to it so it belongs to the class.
If you mean objectwise. Use getters and setters.
Or you can change it directly by doing ScoutingObject.SFID=?; //in your ScoutingFormData class.
Use getters and setters and avoid using public attributes.
Make these methods in your Scouting class:
public int getMyInteger()
{
return myInteger;
}
public void setMyInteger(int newIntegerValue)
{
this.myInteger = newIntegerValue;
}
Where you have your private int myInteger.
In your ScoutingFormData class your can get and set the values:
setMyInteger(23); // The integer myInteger in the Scouting class is now set to 23
int newInteger = getMyInteger(); // The integer newInteger has been initialized to myIntegers value
One of the Activities in my app starts/binds to a service (also part of my app). I would like that service to continue running as long as the app as a whole is still in the foreground, regardless of which Activity is active. But I need to make sure that the service is stopped when the app as a whole is paused (home button/back button).
How can I do that on an application level rather than an Activity level?
The easiest way is to have a singleton which keeps a track of the state of each activity, e.g showing just one activity as an example:
public class ActivityStates {
private static ActivityStates ref = null;
private static int firstAct = ACTIVITY_GONE;
public static synchronized ActivityStates getInstance() {
if (ref == null) {
ref = new ActivityStates();
}
return ref;
}
public int getFirstAct() {
return firstAct;
}
public void setFirstAct(int arg) {
this.firstAct = arg;
}
}
..
and define some static constants that you can import
public static final int ACTIVITY_GONE = 0;
public static final int ACTIVITY_BACKGROUND = 1;
public static final int ACTIVITY_FOREGROUND = 2;
then in each activity have a method
private void setActivityState(int state){
ActivityStates as = ActivityStates.getInstance();
as.setFirstAct(state);
}
Then in your onResume(), onPause, onDestroy() you can set the activitiy's state when you enter these methods, e.g in onResume have
setActivityState(ACTIVITY_FOREGROUND)
in onDestroy() have
setActivityState(ACTIVITY_GONE)
Then in you service, or wherever you want , you can use the get methods to find out the state of each activity and decide what to do.