-1

我有一个活动,它使用如下意图将数据发送到另一个活动:

public void periodDateSharedPreferences(int calculatedPeriodYear, int calculatedPeriodMonth, int calculatedPeriodDay)
{
  SharedPreferences periodDatePreferences = PreferenceManager.getDefaultSharedPreferences(this);
  SharedPreferences.Editor editor = periodDatePreferences.edit();
  editor.putInt("periodChosenDay",calculatedPeriodDay);
  editor.putInt("periodChosenMonth",calculatedPeriodMonth);
  editor.putInt("periodChosenYear",calculatedPeriodYear);
  editor.commit();
    Toast.makeText(birthDate.this,"The date was saved", Toast.LENGTH_LONG).show();
    Intent saved2 = new Intent(birthDate.this,MenuActivity.class);
      saved2.putExtra("DueDateChanged", true);
startActivity(saved2);
    finish();
 }

和其他活动收到此意图如下:

static String DueDateChanged = "com.app.antiwal7amel.DueDateChanged";

Intent intent = getIntent();
        String message = intent.getStringExtra(MenuActivity.DueDateChanged);
        tv.setText(message);
        super.onResume();

但是当我运行它时,我在意图中收到了一个空数据!哪里有问题 ?

4

2 回答 2

3

您应该使用与放置数据相同的键来检索数据:

String message = intent.getStringExtra("DueDateChanged");

正如你所使用的

saved2.putExtra("DueDateChanged", true); 

注意: 您放置的是布尔值而不是字符串,以添加字符串

saved2.putExtra("DueDateChanged", "yourString"); 
于 2012-12-19T08:23:50.360 回答
1
    Intent intent = new Intent(this, MenuActivity.class);
    intent.putExtras("yourBoolName", true);

额外检索意图:

@Override
protected void onCreate(Bundle savedInstanceState) {
    Boolean yourBool = getIntent().getExtras().getBoolean("yourBoolName");
}
于 2012-12-19T08:33:42.240 回答