I want to show another google maps fragment v2 - android

I'm developing android tablet project. There is dualpane layout. In the left side there is leftscrollfrg fragment, it's also execute right side, mainmapfragment, it opens the map. In the left side, i click createproject button and when i click the button it hides the mainmapfragment and adds another fragment which is viewpager fragment. In the first page there is CreateProjectFrg1 fragment and inside there is button which adds another map fragment (CreateProjectMapFrg).
The problem is when the second map opened, the first one is showed like frozen. But i can see the new opened map because the old one padded from left and right. And also when i turn off the screen and back on, the old one map which frozen is gone.
How can i solve this problem?
Note: In the hierarchy view, i cannot see the frozen map.
public class LeftScrollFrg extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View vw = inflater.inflate(R.layout.frg_left_behind, container, false);
frg_map = (MainMapFragment) getActivity()
.getSupportFragmentManager().findFragmentByTag(
MAP_FRAGMENT_TAG);
if (frg_map == null) {
frg_map = new MainMapFragment();
FragmentTransaction ft = getFragmentManager()
.beginTransaction();
ft.replace(R.id.above_framelayout, frg_map, MAP_FRAGMENT_TAG);
ft.addToBackStack(MAP_FRAGMENT_TAG);
ft.commit();
}
imgbtn_createproject.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
hideMainMapFrg();
openCreateProjectFrg();
}
});
imgbtn_map.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
hideCreateProjectFrg();
openMapFrg();
}
});
return vw;
}
private void hideMainMapFrg() {
fm = getActivity().getSupportFragmentManager();
frg_map = (MainMapFragment) fm
.findFragmentByTag(MAP_FRAGMENT_TAG);
if (fm != null) {
ft = fm.beginTransaction();
if (frg_map != null) {
if (frg_map.isVisible()) {
ft.hide(frg_map);
}
}
}
ft.commit();
}
private void openMapFrg() {
fm = getActivity().getSupportFragmentManager();
frg_map = (MainMapFragment) fm
.findFragmentByTag(MAP_FRAGMENT_TAG);
if (fm != null) {
ft = fm.beginTransaction();
if (frg_createproject != null) {
if (frg_createproject.isVisible()) {
ft.hide(frg_createproject);
}
}
if (frg_map == null) {
frg_map = new MainMapFragment();
ft.add(R.id.above_framelayout, frg_map, MAP_FRAGMENT_TAG);
} else {
if (frg_map.isHidden()) {
ft.show(frg_map);
}
}
ft.addToBackStack(MAP_FRAGMENT_TAG);
ft.commit();
}
}
}
public class MainMapFragment extends SupportMapFragment implements
LocationListener, LocationSource, OnInfoWindowClickListener,
OnMapClickListener {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
view = inflater.inflate(R.layout.frg_mainproject, container, false);
return view;
}
}
//frg_mainproject.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="#dimen/rightpanel_margin" >
<fragment
android:id="#+id/main_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
/>
</RelativeLayout>
public class CreateProjectFrg1 extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View vw = inflater.inflate(R.layout.frg_createproject1, container,
false);
imgvw_createprojectmap.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
openCreateMap();
}
});
return vw;
}
private void openCreateMap() {
fm = getActivity().getSupportFragmentManager();
frg_map = (CreateProjectMapFrg) fm
.findFragmentByTag(CREATEPROJECTMAP_FRAGMENT_TAG);
if (fm != null) {
ft = fm.beginTransaction();
if (frg_map == null) {
frg_map = new CreateProjectMapFrg();
ft.add(R.id.above_framelayout, frg_map, CREATEPROJECTMAP_FRAGMENT_TAG);
}
ft.addToBackStack(CREATEPROJECTMAP_FRAGMENT_TAG);
ft.commit();
}
}
}
public class CreateProjectMapFrg extends SupportMapFragment implements
OnMapClickListener, OnMarkerClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.frg_createprojectmap, container,
false);
} catch (InflateException e) {
}
setViewsResource(view);
setUpMapIfNeeded();
return view;
}
private void setUpMapIfNeeded() {
if (mMap == null) {
mMap = ((SupportMapFragment) getActivity()
.getSupportFragmentManager().findFragmentById(
R.id.frg_createproject_map)).getMap();
if (mMap != null) {
setUpMap();
}
}
}
}
//frg_createprojectmap.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="#+id/frg_createproject_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
Updated code

This is https://code.google.com/p/gmaps-api-issues/issues/detail?id=5027
As a workaround you can explicitely remove / replace the MapFragment from the fragment manager instead of just hiding it. If you don't want to remove the fragment as a whole, you can also implement your own Fragment that just uses MapView and detaches that just before the fragment is hidden (might need some more custom code).

Related

Google map not displayed in Fragment subclass

I am created a map and it displayed in fragment in another activity .In my application has only one Activity class.I need to display it in Fragment .
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mapAtm"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
public class Atm extends Fragment {
private SupportMapFragment mSupportMapFragment;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.atm_main,
container, false);
mSupportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapAtm);
if (mSupportMapFragment == null) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
mSupportMapFragment = SupportMapFragment.newInstance();
fragmentTransaction.replace(R.id.mapAtm, mSupportMapFragment).commit();
}
if (mSupportMapFragment != null) {
mSupportMapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
if (googleMap != null) {
googleMap.getUiSettings().setAllGesturesEnabled(true);
LatLng marker_latlng = new LatLng(150, -150);
CameraPosition cameraPosition = new CameraPosition.Builder().target(marker_latlng).zoom(15.0f).build();
CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
googleMap.moveCamera(cameraUpdate);
}
}
});
}
return view;
}
}
Their is no error in Emulator.But after installing in Mobile;application will terminate when clicking in Atm Map

java.lang.IllegalArgumentException: No view found for id 0x7f0a002b (com.sapplication:id/content) for fragment ManagemntPageFragment

i want to redirect activity to fragment when clicking button but it gives me error.
here is my activity class
public class EventDetailsNotif extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.eventdetails);
ImageButton imgmenu = (ImageButton) findViewById(R.id.imgmenu);
imgmenu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loadFragmentObj = new LoadFragment(getFragmentManager());
loadFragmentObj.initializeFragment(new ManagemntPageFragment());
}
});
}
}
and my load fragment class is here.
public class LoadFragment {
FragmentManager fragmentManager;
FragmentManager fragmentManager1;
public LoadFragment(FragmentManager fragmentManager2) {
this.fragmentManager = fragmentManager2;
}
public void initializeFragment(Fragment resultFragment) {
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.content, resultFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
and my content.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
and my fragment class.
public class ManagemntPageFragment extends Fragment {
ImageView footervie;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = (RelativeLayout) inflater.inflate(R.layout.managmntpg,
container, false);
footervie = (ImageView) view.findViewById(R.id.footervie);
advtimagepath = Utility.getSharedKey("advertiseFooter_image",
getActivity());
if (!TextUtils.isEmpty(advtimagepath)) {
if (advtimagepath.endsWith(".jpeg")
|| advtimagepath.endsWith(".jpg")
|| advtimagepath.endsWith(".gif")
|| advtimagepath.endsWith(".png")) {
imageLoader = new ImageLoader(getActivity());
imageLoader.DisplayImage(advtimagepath, footervie);
} else {
}
}
return view ;
}
}
please reply if have solution
are you sure you have below code
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
in eventdetails layout file because you set content menu in activity to this layout
setContentView(R.layout.eventdetails);
and if you don't use support library for fragment you will have compatibility with older android version!

Losing fragment on rotation

I'm having a problem in my app where, even with some of the most basic code (see below), I'm losing a fragment view on rotation.
Everything starts off great:
Until you rotate:
The story is the same the the other way as well:
I'm not sure what is going on. I've tried to follow various tutorials and code examples, checked via logging to make sure that the fragments are being saved and restored, but to no avail. What am I doing wrong?
MainActivity.java
public class MainActivity extends FragmentActivity
{
private FragmentManager fragment_manager;
private CharacterMenuFragment characterMenuFragment;
private CharacterEditFragment characterEditFragment;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_frame);
fragment_manager = getSupportFragmentManager();
if (savedInstanceState != null) {
characterMenuFragment = (CharacterMenuFragment) fragment_manager.getFragment(savedInstanceState, "characterMenuFragment");
characterEditFragment = (CharacterEditFragment) fragment_manager.getFragment(savedInstanceState, "characterEditFragment");
}
if (characterMenuFragment == null) {
Log.i("characterMenuFragment", "making new fragment");
characterMenuFragment = new CharacterMenuFragment();
} else {
Log.i("characterMenuFragment", "re-using old fragment");
}
if (characterEditFragment == null) {
Log.i("characterEditFragment", "making new fragment");
characterEditFragment = new CharacterEditFragment();
} else {
Log.i("characterEditFragment", "re-using old fragment");
}
fragment_manager.beginTransaction().replace(R.id.menu_frame, characterMenuFragment).commit();
fragment_manager.beginTransaction().replace(R.id.content_frame, characterEditFragment).commit();
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
fragment_manager.putFragment(savedInstanceState, "characterMenuFragment", characterMenuFragment);
fragment_manager.putFragment(savedInstanceState, "characterEditFragment", characterEditFragment);
}
}
CharacterEditFragment.java
public class CharacterEditFragment extends Fragment {
private View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.hello_world, container, false);
return view;
}
}
CharacterMenuFragment.java
public class CharacterMenuFragment extends Fragment {
private View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.hello_world, container, false);
return view;
}
}
content_frame.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<FrameLayout
android:id="#+id/menu_frame"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" />
</LinearLayout>
hello_world.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</LinearLayout>
Try this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_frame);
fragment_manager = getSupportFragmentManager();
if (savedInstanceState != null) {
characterMenuFragment = (CharacterMenuFragment) fragment_manager.getFragment(savedInstanceState, "characterMenuFragment");
characterEditFragment = (CharacterEditFragment) fragment_manager.getFragment(savedInstanceState, "characterEditFragment");
}
if (characterMenuFragment == null) {
Log.i("characterMenuFragment", "making new fragment");
characterMenuFragment = new CharacterMenuFragment();
fragment_manager.beginTransaction().replace(R.id.menu_frame, characterMenuFragment).commit();
} else {
Log.i("characterMenuFragment", "re-using old fragment");
}
if (characterEditFragment == null) {
Log.i("characterEditFragment", "making new fragment");
characterEditFragment = new CharacterEditFragment();
fragment_manager.beginTransaction().replace(R.id.content_frame, characterEditFragment).commit();
} else {
Log.i("characterEditFragment", "re-using old fragment");
}
}
Move the fragment managers into the applicable if statements above. You're just making sure to reuse the existing fragment and only creating one if the fragment doesn't exist.

The way to put SupportMapFragment to another Fragment with overriding onCreateView()

I'll try to describe my situation:
I have MyFragment extends Fragment with overriding onCreateView().
In MyFragment.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_fragment, null);
//some manipulation with view
return view;
in my_fragment.xml
<LinearLayout
.....
>
<com.example.widgets.MyMapWidget
android:id="#+id/my_map"
android:layout_width="match_parent"
android:layout_height="300dp"
/>
</LinearLayout>
in MyMapWidget.java
public class MyMapWidget extends LinearLayout {
public MyMapWidget(final Context context, AttributeSet attrs) {
super(context, attrs);
((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.widget_map, this, true);
...
}
in widget_map.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- Some another views-->
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_map_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
/>
<!-- Some another views-->
</LinearLayout>
When I first time show MyFragment - all works great. But if then I show another fragment (with cleaning back stack) and then MyFragment again - I got Inflate Exception
Stacktrase
android.view.InflateException: Binary XML file line #151: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:606)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at `com.***.****.fragments.MyFragment.onCreateView(MyFragment.java:78)`
Line #78 in MyFragment.java (method onCreateView)
View view = inflater.inflate(R.layout.my_fragment, null);
Removing MyMapWidget from my_fragment.xml solved problem. But I have some questions:
Why it happens?
Can I show map like this?
May be you know another way how present map like part of the own Fragment?
NOTE: I checked answer on similar question, but can't solve my situation.
You can have the map in it's own fragment like this:
public class GoogleMapFragment extends MapFragment {
private static final String SUPPORT_MAP_BUNDLE_KEY = "MapOptions";
public GoogleMapFragment() {
mCallback = null;
}
public static interface OnGoogleMapFragmentListener {
void onMapReady(GoogleMap map);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
}
public static GoogleMapFragment newInstance() {
return new GoogleMapFragment();
}
public static GoogleMapFragment newInstance(GoogleMapOptions options) {
Bundle arguments = new Bundle();
arguments.putParcelable(SUPPORT_MAP_BUNDLE_KEY, options);
GoogleMapFragment fragment = new GoogleMapFragment();
fragment.setArguments(arguments);
return fragment;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallback = (OnGoogleMapFragmentListener) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException(getActivity().getClass().getName()
+ " must implement OnGoogleMapFragmentListener");
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
if (mCallback != null) {
mCallback.onMapReady(getMap());
}
return view;
}
private OnGoogleMapFragmentListener mCallback;
}
And add it to your activity like this:
// create a new map
mapsFragment = GoogleMapFragment.newInstance();
// Then we add it using a FragmentTransaction.
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(android.R.id.content, mapsFragment, FRAGMENT_MAP_TAG);
fragmentTransaction.commit();
Let your activity implement OnGoogleMapFragmentListener, then when the map is added and ready the following method will be called:
#Override
public void onMapReady(GoogleMap map) {
//add markers or whatever
}
Hope this is useful for you to get a bit more control over your mapfragment.
I remember facing a similar issue a time ago. What worked for me is programmatically creating the map inside the fragment onCreateView method. Something like.-
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState, R.layout.fragment_layout);
setupMap();
return view;
}
private void setupMap() {
mapFragment = new SupportMapFragment();
ParentActivity activity = getParentActivity();
if (activity != null) {
FragmentTransaction fragmentTransaction = activity.getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.wrapperMapPlaces, mapFragment);
fragmentTransaction.commit();
}
}
I found the way, how solve it:
Detect event where MyFragment destroyed their view (onDestroyView)
In this method get instance of the MyMapWidget and call destroyMap.
myMapWidget.destroyMap will be:
public void destroyMap(FragmentManager fm){
fm.beginTransaction().remove(mMapFragment).commit();
}
Just remove fragment with map, when parent fragment destroed their view.

achartengine: How to add chart to fragment in Tabs

I want to show a Button and a PieChart below in a tab.
Thats the code I worked with: https://github.com/JakeWharton/ActionBarSherlock/blob/master/samples/fragments/src/com/actionbarsherlock/sample/fragments/FragmentTabs.java
Instead of the CountingFragment etc. I implemented my own.
I'm not sure what to
My xml:
fragment_graph_categories.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_graph_categories"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button_filter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_filter" />
</LinearLayout>
And that's my "Tab" attached to the TabManger
public class GraphCategories extends SherlockFragmentActivity {
GraphCategoriesFragment mCategoriesFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_graph_categories);
if (savedInstanceState == null) {
// Do first time initialization -- add initial fragment.
mCategoriesFragment = GraphCategoriesFragment.newInstance(1);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragment_graph_categories, mCategoriesFragment).commit();
} else {
//mStackLevel = savedInstanceState.getInt("level");
}
}
#Override
protected void onResume() {
if(mCategoriesFragment != null) {
//mCategoriesFragment.onResumeRedraw();
}
}
//notice Inner class
public static class GraphCategoriesFragment extends SherlockFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//View mChartView = inflater.inflate(R.layout.hello_world, container, false);
View rlayout = inflater.inflate(R.layout.fragment_graph_categories, container, false);
//? rlayout = new LinearLayout(getActivity());
return rlayout;
}
}
}
But im not sure what to inflate at the fragments onCreateView or if I need a FrameLayout in my xml?
I tried some options but don't figured it out.

Categories

Resources