Android 将包从第一个活动传递到第三个活动
我正在尝试传递我的变量,如单击按钮时的第一个活动所示: 如果我在第二个活动中检索它们,效果很好。但是我需要直接将它们传递给第三个活动,而不是第二个活动Android 将包从第一个活动传递到第三个活动,android,android-activity,Android,Android Activity,我正在尝试传递我的变量,如单击按钮时的第一个活动所示: 如果我在第二个活动中检索它们,效果很好。但是我需要直接将它们传递给第三个活动,而不是第二个活动 Intent intent = new Intent(this, Step3Activity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); String getploc=txtFromTime.getText().toString(); String getfrm
Intent intent = new Intent(this, Step3Activity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
String getploc=txtFromTime.getText().toString();
String getfrmdate=txtFromTime.getText().toString();
//Create the bundle
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putString("pickuploc", getploc);
bundle.putString("fromdate", getfrmdate);
//Add the bundle to the intent
intent.putExtras(bundle);
//Fire that second activity
startActivity(intent);
在《第三种活动》中,我把它们理解为:
//Get the bundle
Intent i = getIntent();
Bundle bundle = i.getExtras();
/*Bundle bundle = getIntent().getExtras();*/
//Extract the data…
String pickuploc = bundle.getString("pickuploc");
String fromdate = bundle.getString("fromdate");
有人能告诉我将它们传递给第三个活动的正确方法吗。在第二个活动中,获取附加包,并在调用第三个活动的意图中使用putExtras进行设置 或者,如果需要更多控件,请手动复制它们。通过使用解决了此问题,如图所示
SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
data.edit().putString("PickLocation", getploc).commit();
data.edit().putString("FromDate", getfrmdate).commit();
并在第三个活动中将其称为:
SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String pickuploc = data.getString("PickLocation","");
String fromdate = data.getString("FromDate","");
考虑到:1。SharedReferences是基于文件的,所以您不应该在每次更改后调用commit,而是使用编辑器-do changes-commit。不要对快速变化的参数使用此方法,因为它需要从文件中写入和读取。2.因为它们是基于文件的,所以即使在活动完成后也会保留它们,直到它们被覆盖或删除。