Infeasible
Status Update
Comments
ma...@gmail.com <ma...@gmail.com> #2
Your approach 1 is correct. If you add SupportMapFragment to transaction inside onActivityCreated then you will have getMap() returning object in onStart or onResume (unless of course Google Play Services is not available).
It is not really async, but you have to wait for SupportMapFragment.onCreateView to be called, which is after your Fragment onActivityCreated and before onStart.
public class MyFragment extends Fragment {
private SupportMapFragment fragment;
private GoogleMap map;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.layout_with_map, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FragmentManager fm = getChildFragmentManager();
fragment = (SupportMapFragment) fm.findFragmentById(R.id.map_container);
if (fragment == null) {
fragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.map_container, fragment).commit();
}
}
@Override
public void onResume() {
super.onResume();
if (map == null) {
map = fragment.getMap();
map.addMarker(new MarkerOptions().position(new LatLng(0, 0)));
}
}
}
It is not really async, but you have to wait for SupportMapFragment.onCreateView to be called, which is after your Fragment onActivityCreated and before onStart.
public class MyFragment extends Fragment {
private SupportMapFragment fragment;
private GoogleMap map;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.layout_with_map, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FragmentManager fm = getChildFragmentManager();
fragment = (SupportMapFragment) fm.findFragmentById(R.id.map_container);
if (fragment == null) {
fragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.map_container, fragment).commit();
}
}
@Override
public void onResume() {
super.onResume();
if (map == null) {
map = fragment.getMap();
map.addMarker(new MarkerOptions().position(new LatLng(0, 0)));
}
}
}
ma...@gmail.com <ma...@gmail.com> #3
I am using this exactly as proposed. It works on my Android 2.3 device and I can turn the devices, which will successfully draw the map again.
BUT: If I replace the fragment, where the map fragment is in (in this code example MyFragment) with a different fragment and then come back, I get an IllegalStateException with "Activity has been destroyed". This is funny, because I am in the onActivityCreated method.
I can display the map only once.
Any ideas?
BUT: If I replace the fragment, where the map fragment is in (in this code example MyFragment) with a different fragment and then come back, I get an IllegalStateException with "Activity has been destroyed". This is funny, because I am in the onActivityCreated method.
I can display the map only once.
Any ideas?
[Deleted User] <[Deleted User]> #4
I am also facing exactly the same issue.
kh...@gmail.com <kh...@gmail.com> #5
same issue here .....
anyone got the solution ????
anyone got the solution ????
ma...@gmail.com <ma...@gmail.com> #6
This was actually a programming error, hard to detect.
When you switch the frame you usually do that from a different activity or frame, e.g.:
if (fragmentMap == null || !fragmentMap.isAdded())
fragmentMap = new Fragment_Directions();
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.actMain_contentFrame, fragmentMap);
transaction.commit();
fm.executePendingTransactions();
In my case I did not do the ==null check since I created the fragment already.
I will then call replace with an old fragment, which has been destroyed when a different fragment is loaded into its place. Fragment stays on device turn.
So, instead of debugging your frame code, debug your frame calling code and check if the fragment reference is still valid!
When you switch the frame you usually do that from a different activity or frame, e.g.:
if (fragmentMap == null || !fragmentMap.isAdded())
fragmentMap = new Fragment_Directions();
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.actMain_contentFrame, fragmentMap);
transaction.commit();
fm.executePendingTransactions();
In my case I did not do the ==null check since I created the fragment already.
I will then call replace with an old fragment, which has been destroyed when a different fragment is loaded into its place. Fragment stays on device turn.
So, instead of debugging your frame code, debug your frame calling code and check if the fragment reference is still valid!
pu...@gmail.com <pu...@gmail.com> #8
can you please tell me what this is? I would like to deepen
http://wdfshare.blogspot.com
pu...@gmail.com <pu...@gmail.com> #9
[Comment deleted]
sh...@gmail.com <sh...@gmail.com> #10
hi
i am using having a MainActivity which extends fragmentactivity.
then mainactivity uses drawer layout in it.
Now on clicking menu items in drawer layout i am switching to different fragments.
//Fragment fragment = null;
FragmentManager fragmentManager = getSupportFragmentManager();
switch (selectedPosition) {
case 0:
fragmentManager.beginTransaction()
.replace(R.id.content_frame, oneFragment).commit();
break;
case 1:
fragmentManager.beginTransaction()
.replace(R.id.content_frame, twoFragment).commit();
break;
case 2:
fragmentManager.beginTransaction()
.replace(R.id.content_frame, threeFragment).commit();
break;
case 4:
break;
default:
Toast.makeText(getApplicationContext(), "No Implementation Yet",
Toast.LENGTH_SHORT).show();
break;
}
now my Onefragment layout contains another fragment
package com.intelli.pickup.track_order;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.intelli.pickup.R;
public class TrackOrderFragment extends SupportMapFragment {
SupportMapFragment fragment;
GoogleMap map;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View root = inflater
.inflate(R.layout.fragment_track_order, null, false);
FragmentManager fm = getChildFragmentManager();
fragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
if (fragment == null) {
fragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.map, fragment).commit();
}
return root;
}
@Override
public void onResume() {
super.onResume();
if (map == null) {
map = fragment.getMap();
map.addMarker(new MarkerOptions().position(new LatLng(0, 0)));
}
}
@Override
public void onDetach() {
super.onDetach();
try {
java.lang.reflect.Field childFragmentManager = Fragment.class
.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
i am using this code.
when i come to this fragment first time. its working fine.
but if i go to other fragment and come back.
it crashes with following error.
06-17 09:21:55.072: E/AndroidRuntime(13784): FATAL EXCEPTION: main
06-17 09:21:55.072: E/AndroidRuntime(13784): Process: com.intelli.pickup, PID: 13784
06-17 09:21:55.072: E/AndroidRuntime(13784): android.view.InflateException: Binary XML file line #29: Error inflating class fragment
06-17 09:21:55.072: E/AndroidRuntime(13784): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
06-17 09:21:55.072: E/AndroidRuntime(13784): at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
06-17 09:21:55.072: E/AndroidRuntime(13784): at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
06-17 09:21:55.072: E/AndroidRuntime(13784): at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
06-17 09:21:55.072: E/AndroidRuntime(13784): at com.intelli.pickup.track_order.TrackOrderFragment.onCreateView(TrackOrderFragment.java:28)
06-17 09:21:55.072: E/AndroidRuntime(13784): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
06-17 09:21:55.072: E/AndroidRuntime(13784): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
06-17 09:21:55.072: E/AndroidRuntime(13784): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
06-17 09:21:55.072: E/AndroidRuntime(13784): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-17 09:21:55.072: E/AndroidRuntime(13784): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
06-17 09:21:55.072: E/AndroidRuntime(13784): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
06-17 09:21:55.072: E/AndroidRuntime(13784): at android.os.Handler.handleCallback(Handler.java:739)
06-17 09:21:55.072: E/AndroidRuntime(13784): at android.os.Handler.dispatchMessage(Handler.java:95)
06-17 09:21:55.072: E/AndroidRuntime(13784): at android.os.Looper.loop(Looper.java:135)
06-17 09:21:55.072: E/AndroidRuntime(13784): at android.app.ActivityThread.main(ActivityThread.java:5312)
06-17 09:21:55.072: E/AndroidRuntime(13784): at java.lang.reflect.Method.invoke(Native Method)
06-17 09:21:55.072: E/AndroidRuntime(13784): at java.lang.reflect.Method.invoke(Method.java:372)
06-17 09:21:55.072: E/AndroidRuntime(13784): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
06-17 09:21:55.072: E/AndroidRuntime(13784): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
06-17 09:21:55.072: E/AndroidRuntime(13784): Caused by: java.lang.IllegalArgumentException: Binary XML file line #29: Duplicate id 0x7f070031, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
06-17 09:21:55.072: E/AndroidRuntime(13784): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:296)
06-17 09:21:55.072: E/AndroidRuntime(13784): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
06-17 09:21:55.072: E/AndroidRuntime(13784): ... 18 more
i am using having a MainActivity which extends fragmentactivity.
then mainactivity uses drawer layout in it.
Now on clicking menu items in drawer layout i am switching to different fragments.
//Fragment fragment = null;
FragmentManager fragmentManager = getSupportFragmentManager();
switch (selectedPosition) {
case 0:
fragmentManager.beginTransaction()
.replace(R.id.content_frame, oneFragment).commit();
break;
case 1:
fragmentManager.beginTransaction()
.replace(R.id.content_frame, twoFragment).commit();
break;
case 2:
fragmentManager.beginTransaction()
.replace(R.id.content_frame, threeFragment).commit();
break;
case 4:
break;
default:
Toast.makeText(getApplicationContext(), "No Implementation Yet",
Toast.LENGTH_SHORT).show();
break;
}
now my Onefragment layout contains another fragment
package com.intelli.pickup.track_order;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.intelli.pickup.R;
public class TrackOrderFragment extends SupportMapFragment {
SupportMapFragment fragment;
GoogleMap map;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View root = inflater
.inflate(R.layout.fragment_track_order, null, false);
FragmentManager fm = getChildFragmentManager();
fragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
if (fragment == null) {
fragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.map, fragment).commit();
}
return root;
}
@Override
public void onResume() {
super.onResume();
if (map == null) {
map = fragment.getMap();
map.addMarker(new MarkerOptions().position(new LatLng(0, 0)));
}
}
@Override
public void onDetach() {
super.onDetach();
try {
java.lang.reflect.Field childFragmentManager = Fragment.class
.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
i am using this code.
when i come to this fragment first time. its working fine.
but if i go to other fragment and come back.
it crashes with following error.
06-17 09:21:55.072: E/AndroidRuntime(13784): FATAL EXCEPTION: main
06-17 09:21:55.072: E/AndroidRuntime(13784): Process: com.intelli.pickup, PID: 13784
06-17 09:21:55.072: E/AndroidRuntime(13784): android.view.InflateException: Binary XML file line #29: Error inflating class fragment
06-17 09:21:55.072: E/AndroidRuntime(13784): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
06-17 09:21:55.072: E/AndroidRuntime(13784): at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
06-17 09:21:55.072: E/AndroidRuntime(13784): at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
06-17 09:21:55.072: E/AndroidRuntime(13784): at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
06-17 09:21:55.072: E/AndroidRuntime(13784): at com.intelli.pickup.track_order.TrackOrderFragment.onCreateView(TrackOrderFragment.java:28)
06-17 09:21:55.072: E/AndroidRuntime(13784): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
06-17 09:21:55.072: E/AndroidRuntime(13784): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
06-17 09:21:55.072: E/AndroidRuntime(13784): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
06-17 09:21:55.072: E/AndroidRuntime(13784): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-17 09:21:55.072: E/AndroidRuntime(13784): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
06-17 09:21:55.072: E/AndroidRuntime(13784): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
06-17 09:21:55.072: E/AndroidRuntime(13784): at android.os.Handler.handleCallback(Handler.java:739)
06-17 09:21:55.072: E/AndroidRuntime(13784): at android.os.Handler.dispatchMessage(Handler.java:95)
06-17 09:21:55.072: E/AndroidRuntime(13784): at android.os.Looper.loop(Looper.java:135)
06-17 09:21:55.072: E/AndroidRuntime(13784): at android.app.ActivityThread.main(ActivityThread.java:5312)
06-17 09:21:55.072: E/AndroidRuntime(13784): at java.lang.reflect.Method.invoke(Native Method)
06-17 09:21:55.072: E/AndroidRuntime(13784): at java.lang.reflect.Method.invoke(Method.java:372)
06-17 09:21:55.072: E/AndroidRuntime(13784): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
06-17 09:21:55.072: E/AndroidRuntime(13784): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
06-17 09:21:55.072: E/AndroidRuntime(13784): Caused by: java.lang.IllegalArgumentException: Binary XML file line #29: Duplicate id 0x7f070031, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
06-17 09:21:55.072: E/AndroidRuntime(13784): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:296)
06-17 09:21:55.072: E/AndroidRuntime(13784): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
06-17 09:21:55.072: E/AndroidRuntime(13784): ... 18 more
su...@gmail.com <su...@gmail.com> #11
public class Home_Map extends Fragment {
GoogleMap googleMap;
FragmentManager myFragmentManager;
SupportMapFragment mySupportMapFragment;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.fragment_home__map, container, false);
//googleMap.setMyLocationEnabled(true);
try {
// Loading map
initilizeMap();
googleMap.setMyLocationEnabled(true);
} catch (Exception e) {
e.printStackTrace();
}
return rootView;
}
private void initilizeMap() {
try
{
if (googleMap == null) {
myFragmentManager = getFragmentManager();
mySupportMapFragment = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map2);
googleMap = mySupportMapFragment.getMap();
if (googleMap == null) {
Toast.makeText(getActivity().getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
} catch (Exception e) { Toast.makeText(getActivity().getApplicationContext(), ""+e, 1).show();
// TODO: handle exception
}
}
@Override
public void onResume() {
super.onResume();
initilizeMap();
}
@Override
public void onDetach() {
// TODO Auto-generated method stub
super.onDetach();
try {
Field childFragmentManager = Fragment.class
.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
If I replace the fragment, where the map fragment is in (in this code example MyFragment) with a different fragment and then come back, I get an IllegalStateException
GoogleMap googleMap;
FragmentManager myFragmentManager;
SupportMapFragment mySupportMapFragment;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.fragment_home__map, container, false);
//googleMap.setMyLocationEnabled(true);
try {
// Loading map
initilizeMap();
googleMap.setMyLocationEnabled(true);
} catch (Exception e) {
e.printStackTrace();
}
return rootView;
}
private void initilizeMap() {
try
{
if (googleMap == null) {
myFragmentManager = getFragmentManager();
mySupportMapFragment = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map2);
googleMap = mySupportMapFragment.getMap();
if (googleMap == null) {
Toast.makeText(getActivity().getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
} catch (Exception e) { Toast.makeText(getActivity().getApplicationContext(), ""+e, 1).show();
// TODO: handle exception
}
}
@Override
public void onResume() {
super.onResume();
initilizeMap();
}
@Override
public void onDetach() {
// TODO Auto-generated method stub
super.onDetach();
try {
Field childFragmentManager = Fragment.class
.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
If I replace the fragment, where the map fragment is in (in this code example MyFragment) with a different fragment and then come back, I get an IllegalStateException
lm...@google.com <lm...@google.com> #12
Are you still seeing this in the most recent version of Google Play Services (Version 8.4)? If so, please file this as a new defect issue with a demo project reproducing the issue, and details of devices you've been seeing this on. We're not aware of any issues regarding swapping in fragments. This sounds like an issue with the android framework, or the way you are replacing fragments. We suggest you reach out on Stack Overflow, or file an issue on the Android Issue Tracker.
[Deleted User] <[Deleted User]> #13
Comment has been deleted.
[Deleted User] <[Deleted User]> #14
package com.jobs.Jobseeker.Fragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.jobs.Adapter.Jobseeker_Adapter.CustomInfoWindowGoogleMap;
import com.jobs.Adapter.Jobseeker_Adapter.Jobadapter;
import com.jobs.Jobseeker.Distance_map;
import com.jobs.Jobseeker.SearchjobsMap;
import com.jobs.R;
import java.util.ArrayList;
import java.util.List;
import network.NetworkConnection;
/**
* Created by vibin on 19-Apr-18.
*/
public class MapJoblistfrag extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
public MapJoblistfrag() {
// Required empty public constructor
}
private SupportMapFragment fragment;
private GoogleMap googleMap;
NetworkConnection net;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
String Managerid, Siteid;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_mapjoblist, container, false);
net = new NetworkConnection(MapJoblistfrag.this.getActivity());
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FragmentManager fm = getChildFragmentManager();
fragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
if (fragment == null) {
fragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.map, fragment).commit();
}
}
@Override
public void onResume() {
super.onResume();
if (googleMap == null) {
googleMap = fragment.getMap();
MarkerOptions marker = new MarkerOptions().position(new LatLng(13.106745, 80.096951));
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.ping));
googleMap.addMarker(marker);
MarkerOptions marker1 = new MarkerOptions().position(new LatLng(13.073226, 80.260921));
marker1.icon(BitmapDescriptorFactory.fromResource(R.drawable.ping));
googleMap.addMarker(marker1);
MarkerOptions marker2 = new MarkerOptions().position(new LatLng(13.040503, 80.233692));
marker2.icon(BitmapDescriptorFactory.fromResource(R.drawable.ping));
googleMap.addMarker(marker2);
MarkerOptions marker3 = new MarkerOptions().position(new LatLng(13.059537, 80.242479));
marker3.icon(BitmapDescriptorFactory.fromResource(R.drawable.ping));
googleMap.addMarker(marker3);
MarkerOptions marker4 = new MarkerOptions().position(new LatLng(13.055125, 80.222121));
marker4.icon(BitmapDescriptorFactory.fromResource(R.drawable.ping));
googleMap.addMarker(marker4);
MarkerOptions marker5 = new MarkerOptions().position(new LatLng(12.922915, 80.127456));
marker5.icon(BitmapDescriptorFactory.fromResource(R.drawable.ping));
CustomInfoWindowGoogleMap customInfoWindow = new CustomInfoWindowGoogleMap(MapJoblistfrag.this.getActivity());
googleMap.setInfoWindowAdapter(customInfoWindow);
googleMap.addMarker(marker5);
// googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
// @Override
// public void onInfoWindowClick(Marker marker) {
// Intent intent = new Intent(MapJoblistfrag.this.getActivity(), Distance_map.class);
// intent.putExtra("lat", 13.106745);
// intent.putExtra("longi", 80.096951);
// startActivity(intent);
// }
// });
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(13.059537, 80.242479), 13));
if (googleMap == null) {
Toast.makeText(MapJoblistfrag.this.getActivity(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
} }
}
}
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.jobs.Adapter.Jobseeker_Adapter.CustomInfoWindowGoogleMap;
import com.jobs.Adapter.Jobseeker_Adapter.Jobadapter;
import com.jobs.Jobseeker.Distance_map;
import com.jobs.Jobseeker.SearchjobsMap;
import com.jobs.R;
import java.util.ArrayList;
import java.util.List;
import network.NetworkConnection;
/**
* Created by vibin on 19-Apr-18.
*/
public class MapJoblistfrag extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
public MapJoblistfrag() {
// Required empty public constructor
}
private SupportMapFragment fragment;
private GoogleMap googleMap;
NetworkConnection net;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
String Managerid, Siteid;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_mapjoblist, container, false);
net = new NetworkConnection(MapJoblistfrag.this.getActivity());
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FragmentManager fm = getChildFragmentManager();
fragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
if (fragment == null) {
fragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.map, fragment).commit();
}
}
@Override
public void onResume() {
super.onResume();
if (googleMap == null) {
googleMap = fragment.getMap();
MarkerOptions marker = new MarkerOptions().position(new LatLng(13.106745, 80.096951));
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.ping));
googleMap.addMarker(marker);
MarkerOptions marker1 = new MarkerOptions().position(new LatLng(13.073226, 80.260921));
marker1.icon(BitmapDescriptorFactory.fromResource(R.drawable.ping));
googleMap.addMarker(marker1);
MarkerOptions marker2 = new MarkerOptions().position(new LatLng(13.040503, 80.233692));
marker2.icon(BitmapDescriptorFactory.fromResource(R.drawable.ping));
googleMap.addMarker(marker2);
MarkerOptions marker3 = new MarkerOptions().position(new LatLng(13.059537, 80.242479));
marker3.icon(BitmapDescriptorFactory.fromResource(R.drawable.ping));
googleMap.addMarker(marker3);
MarkerOptions marker4 = new MarkerOptions().position(new LatLng(13.055125, 80.222121));
marker4.icon(BitmapDescriptorFactory.fromResource(R.drawable.ping));
googleMap.addMarker(marker4);
MarkerOptions marker5 = new MarkerOptions().position(new LatLng(12.922915, 80.127456));
marker5.icon(BitmapDescriptorFactory.fromResource(R.drawable.ping));
CustomInfoWindowGoogleMap customInfoWindow = new CustomInfoWindowGoogleMap(MapJoblistfrag.this.getActivity());
googleMap.setInfoWindowAdapter(customInfoWindow);
googleMap.addMarker(marker5);
// googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
// @Override
// public void onInfoWindowClick(Marker marker) {
// Intent intent = new Intent(MapJoblistfrag.this.getActivity(), Distance_map.class);
// intent.putExtra("lat", 13.106745);
// intent.putExtra("longi", 80.096951);
// startActivity(intent);
// }
// });
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(13.059537, 80.242479), 13));
if (googleMap == null) {
Toast.makeText(MapJoblistfrag.this.getActivity(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
} }
}
}
Description
Currently i found two ways:
1. I can do it using ChildFragmentManager that will add SupportMapFragment. This approach im currently using. However it has disadvantage because child fragment transaction is asynchronous and its hard to guarantee that getMap won't return null.
2. Another way is to extend SupportMapFragment store mapView from super onCreateView (mapView = super.onCreateView(inflater, container, savedInstanceState);) and insert it to inflated layout. However in this case on device map is not rendered until i touch it.
Please add recommended approach to samples or documentation.