I am creating a game in which the user is given hints for every question, when the user presses the hints button, then the hint count has to reduce by 1, I am having lots of activities which will have same type of logic. how to edit data and get it where ever we want. Please help me out
Asked
Active
Viewed 63 times
-1
-
first time click has to trigger the count and from then clicking the button should not trigger the count – user2819290 Oct 05 '13 at 14:31
2 Answers
1
You could just keep count value in a static variable inside your Application
class.
In your AndroidManifest.xml
you define.-
<application
android:allowBackup="true"
android:name=".YourApplicatinClass"
...
Then define YourApplicationApplication
class like.-
public class YourApplicationClass extends Application {
public static int cont = 0;
}
And access cont
value whenever you need with
YourApplicationClass.cont

ssantos
- 16,001
- 7
- 50
- 70
-
-
It should be a class within its own YourApplicationClass.java file. However, notice you can define static variables in any class you already have. – ssantos Oct 05 '13 at 14:41
-
On first click of the button in activity1, has to decrease the cont by 1, and in the first click of the button in activity2 has to decrease the cont by 1 and the final cont value should return -2. from the second click it should not trigger the cont value, is it possible to write a code for the above. Thanks for your response – user2819290 Oct 05 '13 at 14:53
-
Sure, whenever you want to decrease your cont, just do something like `YourApplicationClass.cont --` (equivalent to `YourApplicationClass.cont = YourApplicationClass.cont - 1` – ssantos Oct 05 '13 at 14:55
-
-
`TextView textView = (TextView) findViewById(R.id.yourTextViewId); textView.setText(String.valueOf(YourApplicationClass.cont));` – ssantos Oct 05 '13 at 15:00
-
I want the user to know how many hints that are left for him, so I want to show the cont value in every activity – user2819290 Oct 05 '13 at 15:09
1
You should just save it in SharedPreferences. Have a look at this Question, that should give you the hint how to work with it. You can write a static method to read an decrement the value saved in there
class Activity1{
onClickListener(){
GlobalSettings.getHits(context)
}
}
class Activity2{
onClickListener(){
GlobalSettings.getHits(context)
}
}
class GlobalSettings{
private static String PREFS_NAME = "myprefs";
private static String PREF_HITS = "hits";
private static int START_VALUE = 10;
public static int getHits(Context context){
SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
return settings.getInt(PREF_HITS, START_VALUE);
}
public static void incrementHits(Context context){
SharedPreferences settings = getSharedPreferences(PREFS_NAME , 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt(PREF_HITS, getHits(context) + 1);
editor.commit();
}
public static void decrementHits(Context context){
SharedPreferences settings = getSharedPreferences(PREFS_NAME , 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt(PREF_HITS, getHits(context) - 1);
editor.commit();
}
}
-
I have used Shared Prefs but I am not able to retrieve the value in other activity and modify it – user2819290 Oct 05 '13 at 14:41
-
Of course you can edit and retrieve the values from all activities from the same app. – A.S. Oct 05 '13 at 14:47
-
-
-
-
please read some Java Books and get to know it! Make a own File! This is a board for people how have already knowledge, or tried to get it by reading and searching, but the questions u are asking are not ment to be asked here, we do not prvide ready code, we provide answers! – A.S. Oct 05 '13 at 15:47
-
Thank you for your suggetions, i am glad that you have spent your valuable time for answering my questions... hope more cooperation from you A.S – user2819290 Oct 08 '13 at 06:46
-