如何将两个类意图值传递给另一个类?

时间:2013-06-29 06:09:47

标签: android android-intent

我的应用程序我有两个类将一个intent值传递给另一个类,但在Received类中,如何将两个intent值设置为一个相同的变量

鬃毛,第一个Activity将意图传递给该类,并且在另一个Activity将值传递给同一个类并且在Received类中将intent值设置为相同的变量

1 个答案:

答案 0 :(得分:0)

您可以使用SharedPreference维护数据。 你可以在两个活动中使用以下方法保存你的价值

private void SavePreferences(String key, String value) {
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

U可以在任何活动中检索该值。

private void showPreferences(String key){
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        String savedPref = sharedPreferences.getString(key, "");
        myTextView.setText(savedPref);
       }

仍然如果你想使用Intent.U可以使用它。 添加两个发件人活动

传递值:

Intent intent = new Intent(getBaseContext(), SecondActivity.class);
intent.putExtra("EXTRA_item", item);
startActivity(intent);

这里,“项目”的值随着你的传递而变化。 您可以在Receiver Activity中检索它:

获取SecondActivity中的值:

Intent intent = getIntent();
String string = intent.getStringExtra("EXTRA_item");

此处,字符串将获取最后一个Sender Activity Intent的“item”值。

相关问题