简体   繁体   English

如何将数据从 MainActivity 传递到多个活动

[英]How to pass data from MainActivity to multiple activities

My project looks something like this:我的项目看起来像这样:

  1. MainActivity主要活动
  2. SelectActivity选择活动
  3. MapActivity地图活动

I have data on my MainActivity that i need to send to MapActivity, the problem is that i can not go straight to MapActivity without have selected an item from my SelectActivity.我的 MainActivity 上有我需要发送到 MapActivity 的数据,问题是如果没有从我的 SelectActivity 中选择一个项目,我无法将 go 直接发送到 MapActivity。

I was doing the following:我正在做以下事情:

    Intent intent = new Intent(MainActivity.this, MapSelectActivity.class);
    Bundle b = new Bundle();
    b.putDouble("Lon",longitude);
    b.putDouble("Lat",latitude);
    intent.putExtras(b);
    startActivity(intent);

But the app just started crashing due to the thing i mention earlier that i can not go into the activity without have been selected something from the SelectActivity.但是由于我之前提到的事情,应用程序刚刚开始崩溃,如果没有从 SelectActivity 中选择某些内容,我就无法 go 进入活动。

Anythought on how can I make this?.任何想法我怎么能做这个? I hope i've made myself clear.我希望我已经说清楚了。 Thank you in advace for your help.提前感谢您的帮助。

As you navigate from MainActivity to SelectActivity and then from SelectActivity to MapActivity当您从MainActivity导航到SelectActivity ,然后从SelectActivityMapActivity

And you want to send data from MainActivity to MapActivity , then you can send this data in two stages:并且您想将数据从MainActivity发送到MapActivity ,那么您可以分两个阶段发送这些数据:

  • Stage 1: from MainActivity to SelectActivity第 1 阶段:MainActivitySelectActivity
  • Stage 2: from SelectActivity to MapActivity第二阶段:SelectActivityMapActivity

In MainActivity:在 MainActivity 中:

Intent intent = new Intent(MainActivity.this, SelectActivity.class);
Bundle b = new Bundle();
b.putDouble("Lon",longitude);
b.putDouble("Lat",latitude);
intent.putExtras(b);
startActivity(intent);

In SelectActivity:在选择活动中:

// get the bundle sent from MainActivity to SelectActivity
Bundle b = getIntent().getExtras();
Intent intent = new Intent(SelectActivity.this, MapActivity.class);

// add more data to bundle from the SelectActivity
// b.addXX();

intent.putExtras(b);
startActivity(intent);

In MapActivity:在地图活动中:

// get the bundle sent from SelectActivity to MapActivity
Bundle b = getIntent().getExtras();
b.getDouble("Lon");
b.getDouble("Lat");

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

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