-1

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

ssantos
  • 16,001
  • 7
  • 50
  • 70

2 Answers2

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
  • where shoul I define yourApplicationApplication – user2819290 Oct 05 '13 at 14:40
  • 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
  • How to display the cont value in activities in TEXTVIEW, – user2819290 Oct 05 '13 at 14:59
  • `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();    
    }

}
Community
  • 1
  • 1
A.S.
  • 4,574
  • 3
  • 26
  • 43