简体   繁体   中英

Map fragment in main activity not visible when returning from another fragment

I am using a navigational drawer in the main activity.The main activity holds the map fragment and on selecting SettingsFragment on the navigational drawer the fragment should be displayed.The fragment gets displayed but when I select another item from the drawer the map fragment is not displayed,Only the layout of the settings fragment is shown.

main_activity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

}

private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        selectItem(position);
    }
}

private void selectItem(int position) {

    Fragment fragment = null;

    switch (position) {
        case 0:
            Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
            if (location == null)
              LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
            else
              handleNewLocation(location);
            break;
        case 1:
              output_locations(dataFromAsyncTask);
            break;
        case 2:
            /*fragment is called here */
            fragment = new SettingsFragment();
            break;
       default:
           break;
       }

    if (fragment != null) {
        Bundle data = new Bundle();
        fragment.setArguments(data);
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(osArray[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
        }
    else{
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(osArray[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    }

}

activity_main.xml

<android.support.v4.widget.DrawerLayout 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"
android:id="@+id/drawer_layout"
tools:context=".MainActivity" >

<!-- The main content view -->
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/content_frame">
    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment"
        tools:layout="@layout/activity_main" />
</FrameLayout>

<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#fff"/>

SettingsFragment.java class

public class SettingsFragment extends Fragment {

public SettingsFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View Settings_view = inflater.inflate(R.layout.settings_fragment, container, false);

    return Settings_view;
} 
}

settings_fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@color/background_material_light">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="40px"
    android:textColor="#ffffff"
    android:layout_gravity="center"
    android:id="@+id/detail"/>

Trying to get around this for days...

You are replacing the MapFragment with your SettingsFragment and (as of now) there is no way it can come back.

fragmentManager.beginTransaction().replace(R.id.contend_frame, fragment).commit();

Btw. is conten d _frame just a typo? In your layout file it says conten t _frame.

My advice would be to put the MapFragment (use MapView) in its own layout and Fragment-class not in activity_main and load that fragment on startup. Therefore you can show it again when you need it. (ex. by popBackStack or replacing SettingsFragment with it).

Check this answer on how to use MapView.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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