I know there are already some questions on global methods and variables in android but I'm running into problems with static methods probably due to my less experience with objectoriented programming. So here is what I want to have:
I am writing an app which counts points which the user can earn for certain things he does. Because of that I want to call the method addPoints from different activities and services. This method should also set the points textview in the main activity and some other things.
I realized it by adding a static variable
static int sPoints;
in the MainActivity, that I use as a "global" variable in each activity.
However, with the addPoints method I have some problems. If I use a non-static method, I have to create an instance of MainActivity in the other activities, which is not very nice and changing the values of that instance does not have an effect on the actual MainActivity.
If I use a static function it works fine as long as I don't want to use non-static methods like in this example:
public static void addPoints(Context context, int points){
int levelBefore, levelAfter;
levelBefore = getLevelFromPoints(sPoints);
sPoints = sPoints + points;
levelAfter = getLevelFromPoints(sPoints);
if(levelBefore!=levelAfter){
String rank = getRankFromLevel(levelAfter);
levelTextView.setText("Lvl. " + String.valueOf(levelAfter));
Toast.makeText(context, "Congrats! You reached the next level!", Toast.LENGTH_LONG).show();
}
}
Here I can't easily use levelTextView.setText and I run into this problem in many other cases. Moreover, I've read that using static methods is not good, anyway.
So would the correct way be creating an instance of MainActivity each time and then call addPoints on it which has to return the new number of points? Or is there another way (I hope so, because both above ways seem to be not very satisfying).
a. Static methods can safely be used in case your work can not be accomplished by ShardPreferences and requires the use of same code at multiple classes like in your case.
b. First create an interface that will pass the updated rank to respective activities or classes
public interface ScoreUpdater {
void updateScore (String rank);
}
c. then implement it in all activities where required to use, MainActivity in this case
public class MainActivity extends AppCompatActivity implements ScoreUpdater{
//
//other methods and codes
//
#Override
public void updateScore(String rank) {
runOnUiThread(new Runnable() {
#Override
public void run() {
levelTextView.setText("Lvl. " + String.valueOf(levelAfter));
Toast.makeText(MainActivity.this.getApplicationContext(), "Congrats! You reached the next level!", Toast.LENGTH_LONG).show();
}
});
}
}
d. then implement your static methods. not sure where you have declared few variables. so my method below is on guess work.
public static void addPoints(Context context, int points){
//not sure where you are declaring sPoints
int levelBefore, levelAfter;
levelBefore = getLevelFromPoints(sPoints);
sPoints = sPoints + points;
levelAfter = getLevelFromPoints(sPoints);
if(levelBefore!=levelAfter){
String rank = getRankFromLevel(levelAfter);
if(context instanceof ScoreUpdater){
((ScoreUpdater)context).updateScore(rank);
}
}
}
private static int getLevelFromPoints(int points){
//your operations
return points;
}
Related
I'm making a quiz app, there is main activity and it contains fragments which are questions like (Radio Button, Checkbox, Drag and drop questions) . How to collect the Score from all the fragments.
Hold the score variable in the Main Activity
private int score=0;
write public functions to get and set the score in the activity
public void setScore(int score){
this.score=score;
}
public int getScore(){
return this.score
}
Now in the fragments you can get and set score using
score=((MainActivity)getContext()).getScore()
((MainActivity)getContext()).setScore(score)
There are multiple ways to do that.
You can add Set/Get method in Your activity and fetch that from
fragment when you need them.
Create static variable in Activity class and you can access with 'ActivityClassName'.
Use Shared Preference and get access of that data anywhere. You can reset when you need and update from any class.
You can use as your app requires.
Just keep the variable static in which your are storing your score .
for eg : public static int score=0
by writing static it will have reference in memory and will be available until the
activity is running .
Create a Java class and name it as DataHolder. Define score variable as a global variable. Create getter & setter method as static. When you get score, set the values using set method. when you want to get score, use get method. Its simple java. Best thing is you can get and set score from any activity or any fragment using this method. Try below code.
DataHolder.java
public class DataHolder {
private static String Score="";
public static void set_Score(String s){
DataHolder.Score = s;
}
public static String get_Score(){
return DataHolder.Score;
}
}
To set score in DataHolder class, use below code from any fragment or activity.
String Score = ""; //get Score to this variable
DataHolder.set_Score(Score);
try using public static final key word if your value is not going to be changed through out the app else use public static before the var type that will remain accessible in all files with out instantiation and value could be initialized at any instance .
Example:
public static int score;
public static final int score=5;
Second way is by using sharedprefrences , you can get help from here .
This question is not manly for Android, but I will use an android example to explain.
I want to create a class that should override the method onTouchListener, to be used on any object that uses those touch methods.
Since after a touch input, the method starts a function, and since it's a class, it can be instantiate several times (and I want to be instantiate several times), I want to prevent that two instances are called at the same time.
I guess I could create a variable inside the class, that assured that if it's true, it can run the method, but I need to check it for all of the class instances.
And since I want to make this a library, I need to do this inside the class itself.
So my question is,
(Java Question) How can I know that a variable from any instance of a class is true?
(Android Question) Or if this is not possible, How can I prevent multiple touch events at the same time?
You could achieve that with static data and/or method for this class. A static element can be accessible from anywhere using the class name:
ex: MyClass.isAlreadyRunning();
you could for example store in an array all the running status of the instances, and create a static method that checks the array if one is already running.
You can think about making a Singleton Class. Wiki Link
Singleton classes allows to create ONLY one instance/object of that class and then reuse it.
You can use a static field in this context. A static field is identicaly for all instances of an class.
You can do it maybe like this:
public Example implements TouchInterface{
private static boolean actuallyRunning = false;
public void touchExecute(){
if(!actuallyRunning){
actuallyRunning = true;
callYourFunction();
actuallyRunning = false;
}
}
}
You can use singleton class if you want one one instance and if you want to handle the multiple touch events
you can disable as soon as your handler start and enable after that.
you can add a time span in normal practice it is one sec but it depends on you handler's complexity
Like this example:
// Make ur activity class to implement View.OnClickListener
public class MenuPricipalScreen extends Activity implements View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
// setup listeners.
findViewById(R.id.imageView2).setOnClickListener(MenuPricipalScreen.this);
findViewById(R.id.imageView3).setOnClickListener(MenuPricipalScreen.this);
....
}
.
.
.
// variable to track event time
private long mLastClickTime = 0;
//View.OnClickListener.onClick method defination
#Override
public void onClick(View v) {
// Preventing multiple clicks, using threshold of 1 second
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) {
return;
}
mLastClickTime = SystemClock.elapsedRealtime();
// Handle button clicks
if (v == R.id.imageView2) {
// Do ur stuff.
}
else if (v == R.id.imageView2) {
// Do ur stuff.
}
...
}
.
.`.
You should put "public synchronized" void, so it will prevent to be called in parallel. If you must override, then put a synchronized object in the method so it will do the same.
I'm doing some big refactoring operations relative to some performance improvements in an android app which is using a class with lot of static variables and even static activity references which are then use through the app ! So I was looking for some best practices in Android to store data and give to these data a global access in my app.
First I removed all the activity references to avoid any memory leak, but I'm still looking to know what is the best practice regarding static variables which need to be used anywhere in the android app.
I read many times (example1, exemple2) : using static variables is not necessary a good practices and it's better/cleaner to use one singleton class with getter and setter to have access to my global variables whatever the activity where I am. So what I've started to think is a class which could looks like this one :
public class AppSingleton extends Application {
private static AppSingleton appInstance;
// different stored data, which could be relative to some settings ..
private String setting1;
private String setting2;
private AppSingleton() {
super();
appInstance = new AppSingleton();
}
public static AppSingleton getAppInstance() {
if (appInstance == null) {
appInstance = new AppSingleton();
}
return appInstance;
}
// Getter and Setter for global access
public String getSetting1() {return setting1;}
public void setSetting1(String setting1) {this.setting1 = setting1;}
public String getSetting2() {return setting2;}
public void setSetting2(String setting2) {this.setting2 = setting2;}
}
Then I can use for example :
// Get the application instance
AppSingleton appS = (App) getApplication();
// Call a custom application method
appS.customAppMethod();
// Call a custom method in my App singleton
AppSingleton.getInstance().customAppSingletonMethod();
// Read the value of a variable in my App singleton
String var = AppSingleton.getInstance().getCustomVariable;
For me AppSingleton sounds good because this singleton which restrics ths instantiation of this class to one object, also this class is not destroyed until there are any undestroyed Activity in the application so it means I can keep my global data in the current lifecycle of my app for example from a 'Log in'. But also I can maintain the state of my global variables from my getters/setters.
But then I also had a look on the official android documentation about Performance Tips which say it's good to use static variable it's faster and don't forget to avoid internal getter and setter it's too expansive !
I'm a bit confused about all of these and I'm really keen to learn more about that topic. What is the best practices about using one class to provide an access to some variables which are needed in different part of my code ? Is the class above AppSingeleton is something which could be interesting to use in terms of architecture and performance ?
Is it a good idea to use a singleton pattern for managing global variables in android ?
those lines are completely wrong on your code:
private AppSingleton() {
super();
appInstance = new AppSingleton();
}
public static AppSingleton getAppInstance() {
if (appInstance == null) {
appInstance = new AppSingleton();
}
return appInstance;
}
you cannot instantiate new Application, the Android framework instantiates it. Change to this:
private AppSingleton() {
super();
appInstance = this; // keep ref to this application instance
}
public static AppSingleton getAppInstance() {
return appInstance;
}
Regarding the accessing of global variables. I believe it's more organized to have those singletons somewhere else on your application. The application class have different responsibilities you should not overload it with different tasks. That's OO clean coding.
Also, sometimes there's not that much reason in an Android app to have getters/setters for everything, because u don't need as much access control as in bigger projects. But this should be considered case-by-case about the necessity and not be used a general rule.
So you could for example have it like:
public class Globals {
private static final Globals instance = new Globals();
public static Globals get() { return instance; }
public String value1 = "Hello"
public int value2 = 42;
}
then on your code call as needed:
Log.d(TAG, Globals.get().value1);
Globals.get().value1 = "World";
Log.d(TAG, Globals.get().value1);
Log.d(TAG, "Value2 = " + Globals.get().value2);
I am trying to initialize a class that calls another class that uses AsyncTask. I am using GetDataFromDB gDataFromDB = new GetDataFromDB() but that does not initialize the class, it just gives me access to any static methods in the class. So what do I do to get the onCreate method to run? I have tried using intent but keep getting an error because this is a static class
public class FacadeDataFromDB extends Activity {
static ArrayList<HashMap<String, String>> visitorsList;
private static FacadeDataFromDB dataFromDB;
static boolean accessDB = false;
private FacadeDataFromDB() {
}
public static void initInstance() {
}
public static FacadeDataFromDB getInstance() {
if (dataFromDB == null) {
// Create the instance
dataFromDB = new FacadeDataFromDB();
}
return dataFromDB;
}
public static void setData() {
if (!accessDB) {
GetDataFromDB gDataFromDB = new GetDataFromDB();
accessDB = true;
}
// visitorsList = gDataFromDB.returnInfoFromDB();
}
public static ArrayList<HashMap<String, String>> getVisitorForDay() {
// TODO Auto-generated method stub
setData();
return visitorsList;
}
}
GetDataFromDB is the other class that I am calling. The current class is a static class and uses a singleton because I only want one initialization of the class the gets data from the db. If you have more questions or want me to post code let me know. Thanks
It seems to me that your two classes FacadeDataFromDB GetDataFromDB should not inherit Activity
Activities are made for GUI and user-interaction (I don't see any in your example) and their life-cycle is managed by the framework : you never create them manually with new.
See the android tutorial : https://developer.android.com/guide/components/activities.html and Activity javadoc : https://developer.android.com/reference/android/app/Activity.html.
I'm not sure that you completely understand the Android runtime. You should start Activities using Intent objects, not by creating them with the new keyword as you are. To ensure that your onCreate() method is called within your Activity, you could launch an explicit Intent from some other Activity/Context: Intent intent = new Intent(currentContext, FacadeDataFromDB.class);.
Also, when it comes to Activities, you shouldn't use private constructors. See this post for reasons why.
Here is the declaration in 'MainActivity.java'
private static String competition = null;
I've created a setter function which adds value to it.
public static void setCompetition(String competition1) {
competition = competition1;
}
I've created a getter function to get the value in another class from the same package:
public static String getCompetition() {
return competition;
}
However it returns null.
Here is how I tried to use it in a function
public void onReceive(Context context, Intent intent) {
with in the class AlarmNotify which extends BroadcastReceiver:
final String competition = MainActivity.getCompetition();
I think you are not invoking setCompetition anywhere. To validate that, try with this piece of code -
public static void setCompetition(String competition1) {
System.out.println("Set competition to "+competition1);
competition = competition1;
}
If you do not see any printed message, then setCompetition is not being invoked. Ensure that this is being invoked.
Here is the mistake I did:
I was calling a network based async task, which takes few seconds to retrieve the data. It works in the background.
I was calling another function which was trying to access these values. It was being fired instantaneously, even before the async task could run. That is why it was returning null.
Async task hadn't returned those values.
Ultimately I put the call to the other function in the postExecute method of the Async.
Hope that helps anyone who makes the same mistake as I did.