不同的活动如何将数据传递给同一个类?

时间:2015-12-03 15:45:38

标签: android android-intent android-activity

我有两个按钮。

Button1转到A然后C

Button2去A然后B,最后是C.

这些活动之间传递了价值观。我现在遇到的问题是如何检查它们是否是从B到C的数据传递,或者只是从A到C,所以我可以为它们设置不同的条件。

活动A

  btnNext.setOnClickListener(new View.OnClickListener() {  //if button1 is clicked
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getApplicationContext(),C.class);  //pass spinner value and date to next class
                Name = name.getSelectedItem().toString();
                Weather = weather.getSelectedItem().toString();
                Status=status.getSelectedItem().toString();
                intent.putExtra("Name",Name);
                intent.putExtra("Weather",Weather);
                intent.putExtra("Status",Status);
                intent.putExtra("date2",date2);
                startActivity(intent);
            }
        });

   btnForce.setOnClickListener(new View.OnClickListener()  { // if button2 clicked
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getApplicationContext(),B.class);
                Name = name.getSelectedItem().toString();
                Weather = weather.getSelectedItem().toString();
                Status=status.getSelectedItem().toString();
                intent.putExtra("Name",Name);
                intent.putExtra("Weather",Weather);
                intent.putExtra("Status",Status);
                intent.putExtra("date2",date2);
                startActivity(intent);
            }
        });

活动B

 goDetails.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { // if next button is clicked
                Intent intent = new Intent(getApplicationContext(), C.class);
                sub = SubContractors.getText().toString();
                noP = NoPerson.getText().toString();
                noH=NoHours.getText().toString();
                intent.putExtra("sub",sub);
                intent.putExtra("noP",noP);
                intent.putExtra("noH",noH);
                intent.putExtra("name",name);
                intent.putExtra("weather",weather);
                intent.putExtra("status",status);
                intent.putExtra("date",date);
                startActivity(intent);
            }
        });
    }

活动c

        name = getIntent().getExtras().getString("Name"); // receive name from Information
        weather = getIntent().getExtras().getString("Weather"); //receive weather
        date = getIntent().getExtras().getString("date2"); //receive date
        status = getIntent().getExtras().getString("Status"); // receive status

如果有数据来自B,我该怎么写?

1 个答案:

答案 0 :(得分:1)

您需要传递一些值才能知道来电的来源。 您可以使用示例ρяσѕρєяK给您。或者更好的喷射试试

  

startActivityForResult(_new,REQUEST_CODE);

然后

  

getIntent()。的getAction()

看一下这篇文章,看看更多例子:
Using startActivityForResult, how to get requestCode in child activity?