简体   繁体   English

Java:向不同的活动发送意图

[英]Java: sending intents to different activities

I'm creating a google maps application and I need to send over ALL the information of the markers which the user has created. 我正在创建一个谷歌地图应用程序,我需要发送用户创建的所有标记信息。 However, i do not want to open the activity afterwards, only send the data and then open the activity through a separate button. 但是,我不想以后再打开活动,仅发送数据,然后通过单独的按钮打开活动。 Any suggestions to how i should approach this issue? 关于我应如何解决此问题的任何建议?

here is the code where the marker is created, and sent over to the other activity, but i don't want to start the activity that is receiving the info :( 这是在其中创建标记并将其发送到其他活动的代码,但是我不想开始接收信息的活动:(

@Override
protected void onActivityResult(int requestCode, int resultCode,
                                Intent data) {
    if (requestCode == GET_DETAILS) {
        if (resultCode == RESULT_OK) {
            List<MarkerOptions> markers = new ArrayList<MarkerOptions>();

            String marker_title=data.getStringExtra("title");

            MarkerOptions markerOptions = new MarkerOptions()
                    .position(new LatLng(lat, lon))
                    .title(marker_title);
            Marker m = mMap.addMarker(markerOptions);
            markers.add(markerOptions);

            MarkerOptions[] markersArray = markers.toArray(new MarkerOptions[markers.size()]);
            Intent intent = new Intent(this, MarkerListActivity.class);
            intent.putExtra("markers", markersArray);
            startActivity(intent);

            mMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(lat, lon)));

        }
    }
}

Also, another thing, what does the code: 另外,另一件事是什么代码:

List<MarkerOptions> markers = new ArrayList<MarkerOptions>();

do? 做? I'm new to java and programming so I'm unsure why there are TWO data structures rather than one. 我是java和程序设计的新手,所以我不确定为什么有两个数据结构而不是一个。

I think you are not getting what is the point of Intents for this case. 我想你没有得到什么点Intents这种情况。 Intents are used to start one Activity from another (among other things). Intents用于从另一个Activity (其中包括一个Activity )开始。 As you say, you can pass data to the Activity that is about to be started in the Intent if the data is going to be needed by the Activity that is about to start. 如您所说,如果要启动的Activity将需要数据,则可以将数据传递到将要在Intent启动的Activity It seems to me that you want to send the data to the Activity via the Intent so every Activity instance of that class gets that data. 在我看来,你想将数据发送到Activity通过Intent所以每次Activity该类的实例获取数据。 It does not work that way. 它不能那样工作。 The data you send via the Intent is not "saved" for all instances of the Activity , so starting a new Activity via a Button at a later time, will not be able to access the data that was sent on a previous Intent . 您通过Intent发送的数据并未针对该Activity所有实例“保存”,因此,在以后通过Button启动新的 Activity时,将无法访问在先前Intent上发送的数据。

In short, it seems that the Intent mechanism is not what you want for what you are trying to do. 简而言之,似乎Intent机制并不是您想要做的事情。 It looks like you want the Marker to be persisted in a SharedPreference or a Database , so then every instance of any Activity can get the data. 看起来您希望将Marker保存在SharedPreferenceDatabase ,以便任何Activity每个实例都可以获取数据。

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

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