通过两个活动传递数据?

时间:2014-08-10 02:01:17

标签: java android

对于我正在开发的应用程序,我让用户输入一个活动的一些信息,然后在下一个,用户将读取一些东西,以便他/她不会在其上放置任何信息,但是在第三个活动中,我想显示用户放在第一个活动中的内容。有没有办法可以将第1次到第3次活动的信息传递给第二次?

3 个答案:

答案 0 :(得分:1)

使用sharedPreferences在手机上存储该数据。在第3个活动上,阅读并使用它。 在这看到 - http://developer.android.com/guide/topics/data/data-storage.html#pref

答案 1 :(得分:0)

这可以通过多种方式完成!

为什么不使用Intents传递数据?


STEP-1 :< 例如::来自Activity1 >

借助意图将telefone传递给新活动

Intent i = new Intent(getApplicationContext(), Activity2.class);
i.putExtra("title",telefone);
i.putExtra("descrip",telefone);
startActivity(i);

STEP-2 :< 例如::来自Activity2 >

接收另一项活动中传递的字符串

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String title= extras.getString("title");
    String descrip= extras.getString("descrip");
}

STEP-3 :< 例如::来自Activity2 > 再次将数据传递给Activity3

接收另一个活动中传递的字符串

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String title= extras.getString("title");
    String descrip= extras.getString("descrip");
}

将数据传递给Activity3

 Intent i = new Intent(getApplicationContext(), Activity3.class);
    i.putExtra("title",telefone);
    i.putExtra("descrip",telefone);
    startActivity(i);

希望有所帮助! ..........如果您需要更多信息,请告诉我

答案 2 :(得分:0)

使用SharedPreference。保存在A1中并在A3中检索。

<强>初始化

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();

存储数据

editor.putBoolean("key_name", true); // Storing boolean - true/false
editor.putString("key_name", "string value"); // Storing string
editor.putInt("key_name", "int value"); // Storing integer
editor.putFloat("key_name", "float value"); // Storing float
editor.putLong("key_name", "long value"); // Storing long

editor.commit(); // commit changes

检索数据

// returns stored preference value
// If value is not present return second param value - In this case null
pref.getString("key_name", null); // getting String
pref.getInt("key_name", null); // getting Integer
pref.getFloat("key_name", null); // getting Float
pref.getLong("key_name", null); // getting Long
pref.getBoolean("key_name", null); // getting boolean

删除数据

editor.remove("name"); // will delete key name
editor.remove("email"); // will delete key email
editor.commit(); // commit changes

清算存储空间

editor.clear();
editor.commit(); // commit changes
相关问题