从两个活动数据传递到第三 [英] Passing data from two activities to a third

查看:105
本文介绍了从两个活动数据传递到第三的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我共有3 activites。我通过从第一个活动的数据是这样的:

I have total 3 activites. I pass the data from the first activity like this:

 Intent i = new Intent(getApplicationContext(), History.class);
        i.putExtra("day", mDay);
        i.putExtra("month", mMonth+1);
        i.putExtra("year", mYear);
        startActivity(i);

获取数据:

Bundle extras = getIntent().getExtras(); 
    if(extras !=null) { .......

现在我通过将数据从第二个活动:

now I pass the data from the second activity:

Intent i = new Intent(getApplicationContext(), History.class);
                i.putExtra("Hday", mDay);
                i.putExtra("Hmonth", mMonth);
                i.putExtra("Hyear", mYear);
                startActivity(i);

和从第二活动获取数据:

and get the data from the second activity:

Bundle extras2 = getIntent().getExtras(); 
    if(extras2 !=null) {

所以,我有额外获得来自第一活动的数据,并extras2从第二活动获得的数据。但是,当我从任何活动传递的数据都临时演员不为空!
我做了什么错?

So, I have extras to get the data from the first activity, and extras2 to get the data from the second activity. But when I pass the data from any activity both extras are not null! What have I done wrong?

推荐答案

的问题是,你的第三个活动有不知道哪个活动送出的捆绑方式。您需要添加标识,这是什么类型的包,所以你可以进行相应的处理场。例如,在活动1:

The problem is that your third activity has no way of knowing which activity sent the bundle. You need to add a field that identifies what type of bundle this is so you can process accordingly. For instance, in activity 1:

Intent i = new Intent(getApplicationContext(), History.class);
        i.putExtra("activity", (int)1);
        i.putExtra("day", mDay);
        i.putExtra("month", mMonth+1);
        i.putExtra("year", mYear);
        startActivity(i);

然后在第三个活动:

Then in 3rd activity:

Bundle extras = getIntent().getExtras(); 
    if(extras !=null) { 
       int typeAct = extras.getInt("activity");
       if (typeAct == 1) {
.......

这篇关于从两个活动数据传递到第三的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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