主要活动:
public void ifHangerButtonAction(View view)
{
Intent intent = new Intent(MainActivity.this, FastingHanger.class);
startActivity(intent);
}
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
System.out.println("globalVariable.getFastingHourValue()="+globalVariable.getFastingHourValue());
if(globalVariable.getFastingHourValue()!=null)
{
intended_fast_image_view.setText(globalVariable.getFastingHourValue());
System.out.println("config_text_view="+config_text_view);
}
else
intended_fast_image_view.setText("00:00");
if(globalVariable.getName()!=null)
{
config_text_view.setText(globalVariable.getName());
}
else
config_text_view.setText("24"+" "+"/"+" "+"0");
if(globalTimer.tv!=null)
{
actual_text_view.setText((CharSequence) globalTimer.tv);
}
else
actual_text_view.setText("00:00:00");
}
第二项活动
public void homeScreen()
{
globalVariable.setFastingHourValue(fastingHourValue);
System.out.println("globalVariable.setFastingHourValue(fastingHourValue)="+fastingHourValue);
globalVariable.setName(getName);
System.out.println("globalVariable.setName(getName)="+getName);
finish();
}
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if(globalVariable.getFastingHourValue()!=null)
{
System.out.println(" if globalVariable.getFastingHourValue()="+globalVariable.getFastingHourValue());
image_view3.setText(globalVariable.getFastingHourValue());
System.out.println("on resume");
}
else {
System.out.println(" else globalVariable.getFastingHourValue()="+globalVariable.getFastingHourValue());
image_view3.setText("24:00");
}
if(globalVariable.getStartingTime()=="00:00")
{
start_image_view.setText(globalVariable.getStartingTime());
}
else
start_image_view.setText("00:00");
if(globalVariable.getEndingTime()!=null)
{
end_image_view.setText(globalVariable.getEndingTime());
}
else
end_image_view.setText("00:00");
if(globalVariable.getFeedingTimeValue()=="00:00")
{
image_view4.setText(globalVariable.getFeedingTimeValue());
}
else
image_view4.setText("00:00");
//rs
if(globalVariable.getFastingHourValue()==image_view3.getText().toString())
{
System.out.println("image_view3 globalVariable.getFastingHourValue()="+globalVariable.getFastingHourValue());
image_view3.setText(globalVariable.getFastingHourValue());
}
MyApplication.activityResumed();
System.out.println( "in resume of fasting hanger");
}
答案 0 :(得分:0)
使用bundle传递值...
Bundle b = new Bundle();
b.putInt("key", value);
Intent loginIntent = new Intent();
loginIntent.putExtras(b);
loginIntent.setClass(MainActivity.this,TestActivity.class);
然后在下一个活动中获取数据..
Bundle b3 = getIntent().getExtras();
String txt= b3.getString("key");
答案 1 :(得分:0)
您可以在以下活动之间传递数据:
在MainActivity中:
Intent intent = new Intent();
intent.setClass(this, Other_Activity.class);
intent.putExtra("EXTRA_ID", "SOME DATAS");
startActivity(intent);
在Other_Activity中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String datas= extras.getString("EXTRA_ID");
if (datas!= null) {
// do stuff
}
}
同时检查:Android Intents
希望这有帮助。
答案 2 :(得分:0)
您也可以像使用
一样使用Bundle传递数据值Bundle b = new Bundle();
b.putInt("key", value);
Intent loginIntent = new Intent();
loginIntent.putExtras(b);
loginIntent.setClass(MainActivity.this,TestActivity.class);
您也可以使用单例对象
来完成此操作http://en.wikipedia.org/wiki/Singleton_pattern
public class SingletonDemo {
private static SingletonDemo instance = null;
private SingletonDemo() { }
public static SingletonDemo getInstance() {
if (instance == null) {
if (instance == null) {
instance = new SingletonDemo ();
}
}
return instance;
}
}
并在其中包含如下方法:
private Context context;
init(Context context){
this.context = context.getApplicationContext();
}
并记得将其称为初始化单身人士。
Application方法和Singleton方法之间的区别以及Singleton更好的原因在于文档相同的功能以更加模块化的方式