Skip to content Skip to sidebar Skip to footer

Static Value Android Studio

I have two Activities. And a static integer called as counter. So if I press a button in activity 'A' then counter = counter + 1. Here is the code from activity a: public static i

Solution 1:

Dont use static data, this is a bad approach and is not a common OOP-way to develope, instead try passing data between activities...

Act1

Intent intent = newIntent(activity2.this, activity1.class);
intent.putExtra("message", message);
startActivity(intent);

Act2:

Bundlebundle= getIntent().getExtras();
Stringmessage= bundle.getString("message");

Android development web is giving an introduction to this: http://developer.android.com/training/basics/firstapp/starting-activity.html

Solution 2:

After your edit, I can see that you need a "global varfiable" that can be read/write for all the activities:

Answer : All activities are embedded in an Application, so if you habe fields/members in the application you can access to them with a stadard setter/getter

you need:

Define an application

publicclassMyApplicationextendsApplication {

    privateint counterVariable;

    publicintcounterVariable() {
        returnthis.counterVariable;
    }

    publicvoidsetCounterVariable(int someVariable) {
        this.counterVariable = someVariable;
    }
}

add the App to the manifest:

<application 
  android:name="MyApplication" 
  android:icon="@drawable/icon" 
  android:label="@string/app_name">

Then in your activities get and set the variable like so:

// cast to Application and call the setter
((MyApplication) this.getApplication()).counterVariable(1);

// cast to Application and call the getter
int counter = ((MyApplication) this.getApplication()).getCounterVariable ();

Solution 3:

Please use the below code:

// Generalized form of Avoiding the Static value holding:

publicclassSPDataHandler {


 private Context mContext;
 private SharedPreferences mPreference;
 publicSPDataHandler(Context context) {
        this.mContext = context;
        this.mPreference = mContext.getSharedPreferences("SAMPLE_SP", Context.MODE_PRIVATE);
   }
    /**
     * COMMON SETTER FOR INTEGER DATA
     */privatevoidsetIntegerData(String key, int value) {
        SharedPreferences.Editoreditor= mPreference.edit();
        editor.putInt(key, value);
        editor.commit();
    }
    /**
     * COMMON GETTER FOR INTEGER SP DATA
     */privateintgetIntegerSpData(String key, int defaultValue) {
        returnthis.mPreference.getInt(key, defaultValue);
    }


    // Your Getter and SetterpublicintgetmCount() {
        returnthis.getIntegerSpData("Count", 1);
    }

    publicvoidsetmCount(int cont) {
        this.setIntegerData("Count", cont);
    }
}

// Your Activity ASPDataHandlerhandler=newSPDataHandler(this);
intcount= handler.getmCount();
cmdOk.setOnClickListener(newView.OnClickListener() {
        @OverridepublicvoidonClick(View v) {
        count = count + 1;
        handler.setmCount(count); // Change the logic based on your requirementif (count == 5)
        {
             tagihan.txtShip1.setTextColor(Color.parseColor("#000000"));
             tagihan.txtNilai1.setTextColor(Color.parseColor("#000000"));
             tagihan.txtSupir1.setTextColor(Color.parseColor("#000000"));
        }
}


// Your Activity BSPDataHandlerhandler=newSPDataHandler(this);
intcount= handler.getmCount();
cmdSuccess.setOnClickListener(newView.OnClickListener() {
        @OverridepublicvoidonClick(View v) {
       count = count + 1;
        handler.setmCount(count); // Change the logic based on your requirementif (count  == 5)
        {
             tagihan.txtShip1.setTextColor(Color.parseColor("#000000"));
             tagihan.txtNilai1.setTextColor(Color.parseColor("#000000"));
             tagihan.txtSupir1.setTextColor(Color.parseColor("#000000"));
        }
}

Post a Comment for "Static Value Android Studio"