2

Hi i am using navigation drawer in my project and multiple fragments. in 1 of my fragment i have mapfragment which is added dynamically.

Now when that fragment loads it hides every other views in parent fragment.

and when i click on other fragment and comes back to fragment which contains map.. Map is not visible.

Following is my Fragment xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MapActivity" 
    android:id="@+id/mapContainer">


    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <Button android:id="@+id/btnSearch"
        android:text="Search"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        />
    <EditText android:id="@+id/etSearch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Search"/>
    </LinearLayout>
</FrameLayout>

and following is my java file for fragment.

public class MyRouteFragment extends Fragment {

    public MyRouteFragment(){}
    EditText etSearchValue;
    private LatLng myPos;
    UserData user;
    View rootView;
    GoogleMap mapView;
    MapFragment mapFragment;
    List<Marker> markers=new ArrayList<Marker>();
    LocationsDataSource lDataSource;
    public ProgressDialog pDialog=null;
    FragmentManager mFragmentManager;
    GoogleMapOptions options;
    String address,TAG="MyMapTag";
    Button btnSearch;

    public static MyRouteFragment newInstance(LatLng position){
        MyRouteFragment frag=new MyRouteFragment();
        frag.myPos=position;
        return frag;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.fragment_my_route, container, false);
        btnSearch=(Button) rootView.findViewById(R.id.btnSearch);
        etSearchValue=(EditText) rootView.findViewById(R.id.etSearch);
        mFragmentManager=getFragmentManager();

        mapFragment=(MapFragment) mFragmentManager.findFragmentByTag(TAG);
        if(mapFragment==null){
            mapFragment=MapFragment.newInstance();

            FragmentTransaction fragmentTransaction=mFragmentManager.beginTransaction();

            fragmentTransaction.add(R.id.mapContainer,mapFragment,TAG);

            fragmentTransaction.commit();
            }
            //btnSearch.getParent().bringChildToFront(btnSearch);
            /*btnSearch.invalidate();
            etSearchValue.invalidate();
            btnSearch.bringToFront();
            etSearchValue.invalidate();
            btnSearch.invalidate();
            */
            //container.removeView(btnSearch);
            //container.addView(btnSearch);
            //container.addView(etSearchValue);

        //mapFragment=(MapFragment) mFragmentManager.findFragmentById(R.id.map);

        mapView=mapFragment.getMap();
        if(mapView!=null){
            mapView.setPadding(0, 20, 0, 0);
            mapView.setMyLocationEnabled(true);

        }

        btnSearch.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new getFormattedAddress().execute();
            }
        });

        //btnSearch.bringToFront();


        //etSearchValue.bringToFront();
        return rootView;
    }
Paresh Dudhat
  • 1,166
  • 1
  • 14
  • 28

1 Answers1

3

For a start, you do not need to call container.removeAllViews() and fragmentTransaction.remove(mapFragment) before adding the MapFragment to the container.

You are inserting your MapFragment dynamically on the same container as all the other views. Your MapFragment is front of all the other views, inside the container,that is why you cannot see them.

You can have a child FrameLayout just to insert your MapFragment, inside your parent FrameLayout. Do not forget to add the android:id="@+id/mapContainer" to the child FrameLayout and remove it from the parent FrameLayout. This way, the other components are going to be visible.

I should warn you that you should not load a Fragment inside another Fragment, it is not recommended.

joao2fast4u
  • 6,868
  • 5
  • 28
  • 42
  • But having another issue. i am getting null value in mapView. So i made AsyncTask new AsyncTask(){ ProgressDialog pd; protected void onPreExecute() { pd= new ProgressDialog(getActivity()); pd.setTitle("Loading Map"); pd.setMessage("Please wait while we load map"); pd.setCancelable(false); pd.show(); }; @Override protected Void doInBackground(Void... params) { return null; } protected void onPostExecute(Void result) { while(mapView==null){ mapView=mapFragment.getMap(); mapView.setMyLocationEnabled(true); } if(pd.isShowing()){ pd.cancel(); } }; }.execute(); – Paresh Dudhat Jul 27 '14 at 18:29