活动A之间的数据传送到C? [英] transfer data between activity A to C?

查看:118
本文介绍了活动A之间的数据传送到C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个活动的整数值传递给B,为C. A包含的热量,B指定你如何锻炼和C计算最终摄入热量的默认方式;但数据不会传送到活动℃。

 意向书I =新意图(getApplicationContext(),ActivityC.class);捆绑额外=新包();
i.putExtras(临时演员);
extras.putDouble(clllo卡路里);
startActivity(新意图(com.ti7a.fitness.ActivityB));

然后activityC:

 双值= 1.54354;捆绑额外= getIntent()getExtras()。如果(临时演员!= NULL){值= extras.getDouble(clllo);
               }
        }


解决方案

您可以使用应用程序上下文来保存你的数据。

办法使用的应用程序上下文。

扩展应用程序类,并添加一个属性来保存这些都需要在不同的活动不同的数据。因此,在您的活动,您可以访问应用程序上下文和获取数据。由于应用程序上下文是一个单身它会在每一次活动相同的实例。

  MyApplication的appContext =(所有MyApplication)getApplicationContext();
appContext.data =您的数据;

在任何其他活动,你可以访问数据的方式相同。

  MyApplication的appContext =(所有MyApplication)getApplicationContext();

您还需要添加

 的android:为MyApplicationNAME =

在清单文件应用程序标签。

I want to pass an integer value from activity A to B, to C. A contains the calories, B specifies how you exercise and C calculates your final calories intake the default way; but data is not transferred to activity C.

Intent i = new Intent(getApplicationContext(), ActivityC.class);

Bundle extras = new Bundle();
i.putExtras(extras); 
extras.putDouble("clllo", calories);
startActivity(new Intent("com.ti7a.fitness.ActivityB"));

then in activityC:

double value=1.54354;

Bundle extras = getIntent().getExtras();

if(extras!=null){

value = extras.getDouble("clllo"); 
               }
        }

解决方案

You could use the application context to hold your data.

Way to use app context.

Extend the 'Application' class and add a attribute to hold the different data which are needed in the different activities. So in your activity you can access the application context and get the data. As the application context is a singleton it will be the same instance in every activity.

MyApplication appContext = (MyApplication) getApplicationContext();
appContext.data= YOUR DATA;

In any other activity you can access that data the same way.

MyApplication appContext = (MyApplication) getApplicationContext();

You also need to add

android:name=".MyApplication"

to 'application' tag in the manifest file.

这篇关于活动A之间的数据传送到C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆