2016-02-12 77 views
0

我有两个活动。和一个称为计数器的静态整数。静态值Android Studio

所以,如果我在活动'A'然后counter = counter + 1按下按钮。

下面是活动代码:

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")); 
     } 
} 

这里,它是从活动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")); 
     } 
} 

我的问题是,当我试图从活动按一个按钮3次它完美地工作。所以现在的值是3。

但是,当我尝试按下活动b的按钮,价值将重新启动为0.其实我没有销毁活动a。

所以我想要的是价值会持续不断,即使我按活动a或b。

任何想法?

编辑:

我编辑的代码。塔吉汗的活动就是我试图完成的事情。所以当计数器是5时,tagihan活动正在改变。

+0

在活动中共享静态动态变量不是首选,因为您几乎无法控制活动生命周期。你试图完成什么问题? –

+0

你必须显示使用这两种活动的完整来源。 –

+0

如何将活动a的实例传递给b。你可以请秀吗? – KDeogharkar

回答

1

不使用静态数据,这是一种糟糕的方法,不是一种常见的OOP开发方式,而是尝试在活动之间传递数据...

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开发网站是给介绍这个: http://developer.android.com/training/basics/firstapp/starting-activity.html

1

你的编辑后,我可以看到你需要一个“全局变量”,可以b e读/写的所有活动:

解决方案: 所有活动都嵌入到应用程序,所以如果你HABE在应用领域/成员可以访问到它们与stadard 的setter /吸气

您需要:

定义应用程序

public class MyApplication extends Application { 

    private int counterVariable; 

    public int counterVariable() { 
     return this.counterVariable; 
    } 

    public void setCounterVariable(int someVariable) { 
     this.counterVariable = someVariable; 
    } 
} 

添加到应用程序清单:

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

然后获取和设置变量,像这样:

// 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(); 
0

请使用下面的代码:

//广义避免静态值保持形式:

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")); 
     } 
}