我想传递信息,如图screen description
所示我知道我的代码正在运行,因为有一个Intent
它可以完美运行。但是,当我尝试放两个或更多Intent
时,它似乎搞砸了。我还检查here找到了一个sutibale解决方案,但我认为我不能以同样的方式使用它。提前致谢
- 更新 -
仍然没有工作
我在保存方面这样做了
Intent saveIntent = new Intent(this, MainActivity.class);
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("time", displayTime.getText().toString());
editor.commit();
startActivity(saveIntent);
这是我的回忆
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String time = preferences.getString("time",null);
if (time != null)
getTime.setText(time);
主要活动:
private Button createNewEvent;
private Button showMyEvents;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createNewEvent = (Button) findViewById(R.id.bCreateNewEvent);
showMyEvents = (Button) findViewById(R.id.bShowMyEvents);
}
public void buttonOnClick(View v) {
switch (v.getId()) {
case R.id.bCreateNewEvent:
Intent createNewEventIntent = new Intent(this, CreateNewEventActivity.class);
startActivity(createNewEventIntent);
break;
case R.id.bShowMyEvents:
Intent myEventsIntent = new Intent(this,MyEventsActivity.class);
startActivity(myEventsIntent);
break;
}
}
}
屏幕1保存按钮:
case R.id.bSaveNewEvent:
Intent putIntent = new Intent(getApplicationContext(),MyEventsActivity.class);
// String text = displayTime.getText().toString();
putIntent.putExtra("time",displayTime.getText().toString());
Intent saveIntent = new Intent(this, MainActivity.class);
startActivity(saveIntent);
startActivity(putIntent);
屏幕2 getExtras
public class MyEventsActivity extends AppCompatActivity {
TextView getTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_events);
getTime = (TextView) findViewById(R.id.tvGetTime);
Intent in = getIntent();
String name = in.getStringExtra("time");
getTime.setText(name);
/* Bundle extras = getIntent().getExtras();
if (extras != null){
getTime.setText(extras.getString("time"));
}*/
答案 0 :(得分:1)
如果您尝试保存需要在其他多项活动中使用的数据,SharedPreferences
将是最简单的解决方案。
https://developer.android.com/training/basics/data-storage/shared-preferences.html
如果要保存值,可以使用:
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("time", displayTime.getText().toString());
editor.commit();
然后,当您想稍后获得偏好时,您将使用:
SharedPreferences preferences = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String time = preferences.getString("time", "");