繁体   English   中英

将多个意图传递给多个活动

[英]Pass multiple Intents to multiple activities

我想传递此图片屏幕说明中所示的信息

我知道我的代码可以正常运行,因为一个Intent可以完美地工作。 但是,当我尝试放置两个或多个Intent ,似乎变得一团糟。 我还检查了这里以找到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")); }*/ 

如果您试图保存将在其他多个活动中使用的数据,则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", "");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM