Static Value Android Studio

匿名 (未验证) 提交于 2019-12-03 01:45:01

问题:

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 int counter = 0; cmdOk.setOnClickListener(new View.OnClickListener() {         @Override         public void onClick(View v) {         counter = counter + 1;         if (counter == 5)         {              tagihan.txtShip1.setTextColor(Color.parseColor("#000000"));              tagihan.txtNilai1.setTextColor(Color.parseColor("#000000"));              tagihan.txtSupir1.setTextColor(Color.parseColor("#000000"));         } } 

And here it is from activity b :

cmdSuccess.setOnClickListener(new View.OnClickListener() {         @Override         public void onClick(View v) {         a.counter = a.counter + 1;         if (a.counter == 5)         {              tagihan.txtShip1.setTextColor(Color.parseColor("#000000"));              tagihan.txtNilai1.setTextColor(Color.parseColor("#000000"));              tagihan.txtSupir1.setTextColor(Color.parseColor("#000000"));         } } 

My problem is when i tried to press a button from activity a 3 times it work perfectly. So the values are 3 now.

But when i tried press a button from activity b, the value is going restart into 0. Actually i didn't destroy activity a.

So what i want is the value is going continouosly even i press from activity a or b.

Any ideas ?

Edited :

I have edit the code. Tagihan activity is what im trying to accomplished. So when the counter is 5, then tagihan activity is changing.

回答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 = new Intent(activity2.this, activity1.class); intent.putExtra("message", message); startActivity(intent); 

Act2:

Bundle bundle = getIntent().getExtras(); String message = bundle.getString("message"); 

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



回答2:

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

Solution: 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

public class MyApplication extends Application {      private int counterVariable;      public int counterVariable() {         return this.counterVariable;     }      public void setCounterVariable(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 (); 


回答3:

Please use the below code:

// Generalized form of Avoiding the Static value holding:

public class SPDataHandler {    private Context mContext;  private SharedPreferences mPreference;  public SPDataHandler(Context context) {         this.mContext = context;         this.mPreference = mContext.getSharedPreferences("SAMPLE_SP", Context.MODE_PRIVATE);    }     /**      * COMMON SETTER FOR INTEGER DATA      */     private void setIntegerData(String key, int value) {         SharedPreferences.Editor editor = mPreference.edit();         editor.putInt(key, value);         editor.commit();     }     /**      * COMMON GETTER FOR INTEGER SP DATA      */     private int getIntegerSpData(String key, int defaultValue) {         return this.mPreference.getInt(key, defaultValue);     }       // Your Getter and Setter       public int getmCount() {         return this.getIntegerSpData("Count", 1);     }      public void setmCount(int cont) {         this.setIntegerData("Count", cont);     } }  // Your Activity A  SPDataHandler handler = new  SPDataHandler(this); int count = handler.getmCount(); cmdOk.setOnClickListener(new View.OnClickListener() {         @Override         public void onClick(View v) {         count = count + 1;         handler.setmCount(count); // Change the logic based on your requirement         if (count == 5)         {              tagihan.txtShip1.setTextColor(Color.parseColor("#000000"));              tagihan.txtNilai1.setTextColor(Color.parseColor("#000000"));              tagihan.txtSupir1.setTextColor(Color.parseColor("#000000"));         } }   // Your Activity B SPDataHandler handler = new  SPDataHandler(this); int count = handler.getmCount(); cmdSuccess.setOnClickListener(new View.OnClickListener() {         @Override         public void onClick(View v) {        count = count + 1;         handler.setmCount(count); // Change the logic based on your requirement         if (count  == 5)         {              tagihan.txtShip1.setTextColor(Color.parseColor("#000000"));              tagihan.txtNilai1.setTextColor(Color.parseColor("#000000"));              tagihan.txtSupir1.setTextColor(Color.parseColor("#000000"));         } } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!