简体   繁体   English

从Google Map获取结果并将数据传输回上一个片段

[英]Get result from Google Map and transfer data back to previous fragment

I have created a Google Map feature for my android app. 我已经为我的Android应用程序创建了Google Map功能。 When the user Goes on FragmentA they are asked to input some information such as name and description of a Job. 当用户进入FragmentA时,要求他们输入一些信息,例如作业的名称和描述。 When they get lower down on that page they can select a Map button that allows them to open a Google Map that I have created. 当他们在该页面上下降时,他们可以选择“地图”按钮,以允许他们打开我创建的Google地图。 On this fragment, FragmentMap, they can select a location. 在这个片段FragmentMap上,他们可以选择一个位置。 Once they are happy they can press a green tick/check icon that displays a PopUp asking if they are happy with the location. 他们满意后,可以按绿色的勾号/复选图标,显示弹出窗口,询问他们对位置是否满意。

When the user selects the Yes button I want the address that they have selected from the map, to be transferred from FragmentMap back to FragmentA. 当用户选择“是”按钮时,我希望将他们从地图中选择的地址从FragmentMap传输回FragmentA。 I also need FragmentA to retain all of the users information that they had already entered before pressing the Map button. 在按“地图”按钮之前,我还需要FragmentA保留他们已经输入的所有用户信息。

For Example, the user has entered A Job Name, Description and Collection Date and they then press the map. 例如,用户输入了工作名称,描述和收集日期,然后他们按地图。 They then choose a location on the map and confirm it is correct. 然后,他们在地图上选择一个位置,并确认该位置正确。 Then they will go back to FragmentA where all of the details previously entered are still in the EditText fields as well as the addres from the Map that the user has selected. 然后,他们将返回FragmentA,在该处先前输入的所有详细信息以及用户选择的Map中的地址都仍在EditText字段中。

FragmentA, Where the user can input data, such as Avdert Name. FragmentA,用户可以在其中输入数据,例如Avdert Name。 When they press the Map Button they will be Displayed with FragmentMap 当他们按下地图按钮时,它们将与FragmentMap一起显示

FragmentA

Once the user has chosen a location and confirmed it they will be displayed with this PopUp. 一旦用户选择了一个位置并确认后,它将与该PopUp一起显示。 If they press yes the Address, as shown, will be transferred back to FragmentA 如果他们按是,则如图所示的地址将被转移回FragmentA

确认位置

My Question is, how can I transfer data from my Map fragment back to FragmentA and retain all previous data that was in FragmentA. 我的问题是,如何将数据从Map片段传输回FragmentA,并保留FragmentA中以前的所有数据。 I was thinking about using a bundle to transfer data from FragmentA to FragmentMap and then pass it back again. 我正在考虑使用捆绑包将数据从FragmentA传输到FragmentMap,然后再次将其传递回去。 Is their a more efficient and easy way? 他们是更有效,更简便的方法吗?

Note 注意

My question is different to others that i have found as its about a map and retaing information from the previous fragment 我的问题与我发现的其他问题不同,因为它是有关上一个片段的地图和保留信息的

You may call setTargetFragment() when you start the Fragment B from A. Example: 从A启动片段B时,可以调用setTargetFragment()。示例:

FragmentB fragmentB = FragmentB.newInstance();
fragmentB.setTargetFragment(FragmentA.this, REQUEST_CODE);
getFragmentManager().beginTransaction().replace(R.id.container, 
fragmentB).commit();

and then when you want to pass data back to fragment A from B, you can call the following code: 然后,当您要将数据从B传递回片段A时,可以调用以下代码:

getTargetFragment().onActivityResult(
            getTargetRequestCode(),
            Activity.RESULT_OK,
            new Intent().putExtra("datafrom B", "datafrom B")
);

And get it from the onActivityResult() method in your fragment A: 并从片段A中的onActivityResult()方法获取它:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode==REQUEST_CODE && resultCode==Activity.RESULT_OK) {
    String datafromB = data.getStringExtra("datafrom B");   
}
}

You can pass you information to the Activity that contains both the fragments like ((MainActivity)getActivity()).mapLocationUpdated(latlng) and create the mapLocationUpdated in your activity, then pass the information to the previous fragment by finding the fragment(with getSupportFragmentManager().findFragmentByTag(tag) and passing it the info. 您可以将信息传递给包含((MainActivity)getActivity()).mapLocationUpdated(latlng)类的两个片段的Activity并在您的活动中创建mapLocationUpdated ,然后通过找到该片段(使用getSupportFragmentManager().findFragmentByTag(tag)将信息传递给上一个片段getSupportFragmentManager().findFragmentByTag(tag)并向其传递信息。

You can also create an interface that the activity implements (like MapLocationInteractor that has the method mapLocationUpdated(latlng) and call your activity like this : ((MapLocationInteractor)getActivity()).mapLocationUpdated(latlng) . 您还可以创建活动实现的接口(例如具有MapLocationInteractor mapLocationUpdated(latlng)方法的MapLocationInteractor ,并这样调用您的活动: ((MapLocationInteractor)getActivity()).mapLocationUpdated(latlng)

Don't forget to check for nulls in the getActivity and findFragment parts. 不要忘记检查getActivity和findFragment部分中的null。

您可以通过在特定片段中创建方法并传递google map实例来实现此目的

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

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