Start snippet from action bar tab
I would like to call a fragment from another fragment that is inside the action bar tab? How to do it?
Obviously:
startActivity(new Intent(getActivity(), Maps.class));
will not work.
I would like to call:
public class Maps extends Fragment implements GoogleMap.OnMyLocationChangeListener
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
if (rootView != null)
{
ViewGroup parent = (ViewGroup) rootView.getParent();
if (parent != null)
parent.removeView(rootView);
}
try
{
rootView = inflater.inflate(R.layout.activity_maps, container, false);
}
catch (InflateException e)
{
/* map is already there, just return view as it is */
}
fa = getActivity();
if (initMap())
{
gps = new GPSTracker(fa);
curlat = gps.getLatitude();
curlong = gps.getLongitude();
gps.stopUsingGPS();
//Get the ZOOM working for given location.
}
else
{
Toast.makeText(getActivity().getApplicationContext(),"Sorry! Map not available!", Toast.LENGTH_SHORT).show();
}
return rootView;
}
private boolean initMap()
{
if (googleMap == null)
{
SupportMapFragment mapFrag = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map));
googleMap = mapFrag.getMap();
googleMap.setTrafficEnabled(true);
googleMap.setMyLocationEnabled(true);
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.setOnMyLocationChangeListener(this);
}
return (googleMap != null);
}
}
my Maps XML looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayoutforMap"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
from
I have a list of items in this class in Item Click. I am trying to open a map bypass.
public class Friends extends Fragment
{
View rootView = inflater.inflate(R.layout.activity_friends, container, false);
list = (ListView) rootView.findViewById(R.id.friends_list);
list.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
android.support.v4.app.FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Maps fragment = new Maps();
fragmentTransaction.replace(R.id.map, fragment);
fragmentTransaction.commit();
}
});
return rootView;
}
UPDATE 1:
I tried the following:
Fragment fragment = null;
fragment = new Maps();
if (fragment != null)
{
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.map, fragment).commit();
}
I am getting error in getFragmentManger ();
Type mismatch: cannot convert from android.support.v4.app.FragmentManager to android.app.FragmentManager
Let me know!
Thank!
source to share
enter the code into **Friend fragment**
.
public android.view.View onCreateView(android.view.LayoutInflater inflater,
android.view.ViewGroup container,
android.os.Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.yourlayout, null);
list=(ListView)view.findViewById(R.id.listId);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Map fragment=new Map();
((yourActivity)getActivity()).replacefragment(fragment);
}
});
return view;
};
in your activity, create one of the following ways.
private void replacefragment(Fragment fragment){
android.support.v4.app.FragmentManager fragmentManager = getActivity()
.getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.map, fragment);
fragmentTransaction.commit();
}
source to share
You are using android.support.v4.app.Fragment
and you are using getFragmentManager()
; it is an instance of android.app.FragmentManager instance. But you are using getSupportFragmentManager()
His works Fine.
replace this code
Fragment fragment = null;
fragment = new Maps();
if (fragment != null)
{
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.map, fragment).commit();
}
source to share