Android Fragment生命周期和Google Maps

Android开发

我有一个应该显示Google地图的嵌套片段。首次创建活动时,将添加主片段,然后实例化嵌套的地图片段。这可以正常工作。

但是,如果用户离开该应用程序(将其放到后台),然后又返回到该应用程序,则不再显示嵌套的地图片段。好像有人在上面调用了map.setVisibility(View.INVISIBLE)(不是View.GONE)(除了未调用之外-我只是在声明它是为了将问题放在上下文中)。

这是第一个片段的相关代码(请注意,我遗漏了很多代码,因此请不要担心是否有未在此处看到的变量初始化的变量):

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

        Log.d("resume", "today onCreateView");
        view = inflater.inflate(R.layout.fragment_today, container, false);    
        fragmentManager = getFragmentManager();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        addMapFragment(new LatLng(location.getLatitude(), location.getLongitude()), location.getLocationName());
    }

    private void addMapFragment(LatLng latLng, String marker_name) {

        Log.d("resume", Double.toString(latLng.latitude));

        Bundle mapBundle = new Bundle();
        mapBundle.putDouble("lat", latLng.latitude);
        mapBundle.putDouble("lng", latLng.longitude);
        mapBundle.putString("marker_title", marker_name);

        // capture this for use in getting directions if requested
        destinationLatLng = new LatLng(latLng.latitude, latLng.longitude);

        mMapFragment = new MapFragment();
        mMapFragment.setArguments(mapBundle);

        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.map_today, mMapFragment);
        fragmentTransaction.commitAllowingStateLoss();

        Log.d("resume", "map added");
    }

fragment_today.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.mysite.myapp.Activities.ViewControllers.TodayFragment"
    android:id="@+id/today_background"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <FrameLayout
            android:id="@+id/title_frame_today"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <FrameLayout
            android:id="@+id/map_today"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:paddingLeft="15dp"
            android:paddingRight="15dp" />

       <!-- lots of other textviews follow. omitted for brevity-->

    </LinearLayout>

</RelativeLayout>

fragment_map.xml

<?xml version="1.0" encoding="utf-8"?>
<fragment 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fragment_map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    map:mapType="normal"
    map:uiCompass="true"
    map:uiRotateGestures="true"
    map:uiScrollGestures="true"
    map:uiTiltGestures="true"
    map:uiZoomControls="true"
    map:uiZoomGestures="true" />

然后,最后,在地图片段中,我有这段代码。我一直在研究放置代码以生成地图的位置(在onCreateView和onResume之间切换)。我在onResume中放置了尽可能多的代码。请注意,在用户将应用程序放置到后台与返回到应用程序之间,这些值都不会改变,因此这不是问题:

MapFragment.java

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

        Log.d("resume", "map oncreateview");

        view = inflater.inflate(R.layout.fragment_map, container, false);

        // set the title text (if given by parent fragment)
        Bundle bundle = getArguments();

        if (bundle != null) {

            if (bundle.containsKey("lat") && bundle.containsKey("lng")) {    
                myLocation = new LatLng(bundle.getDouble("lat"), bundle.getDouble("lng"));
            }

            if (bundle.containsKey("marker_title"))
                markerTitle = bundle.getString("marker_title");

        }

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d("resume", "map onResume");

        if (myLocation != null && markerTitle != null) {
            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(workoutLocation)
                    .zoom(12)
                    .tilt(30)
                    .build();

            GoogleMap map = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.fragment_map)).getMap();

            Marker marker = map.addMarker(new MarkerOptions()
                            .position(workoutLocation)
                            .title(markerTitle)
            );
            marker.showInfoWindow();

            map.setBuildingsEnabled(true);
            map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
        }
    }

在运行时,所有这些都会生成以下输出:

在初始实例化时

今天onResume
当前
44.915647
地图已添加
地图oncreateview
地图onResume

当应用程序进入后台时

今天
onPauseonStop

当应用今天在CreateView重新流行时



今天的地图oncreateview onCreateView
Activity.onPostResume()
今天调用今天onResume
当前
44.915647
地图添加了
地图onResume
今天onResume
当前
44.915647
地图添加了
地图oncreateview
地图onResume
地图oncreateview
地图onResume

我认为这一切看起来都不错,尽管很好奇“ map oncreateview”和“ map onResume”出现了两次(我不认为这是个问题)。

长篇文章,但一个有趣的困境。感谢任何能做到这一点的人!

托米斯拉夫·图尔契奇(Tomislav Turcic)

尝试使用新的API,该API可以异步获取地图。

引用您的Google Map对象。在onResume()中,检查它是否为null,然后获取您的地图。

另外,我建议您像添加常规片段一样添加子地图。也就是说,不要在布局中使用片段,而是使用FrameLayout并向其添加(Support)MapFragment。在片段中使用嵌套子片段时,我发现了很多问题。

因此,我建议这样的事情:

private GoogleMap googleMap;

private void setUpMapIfNeeded(){
    if(googleMap == null){
        SupportMapFragment mapFragment = SupportMapFragment.newInstance();
        // mapContainerLayout would be some FrameLayout to replace the <fragment> you use for map
        getChildFragmentManager.beginTransaction.replace(R.id.mapContainerLayout, mapFragment, "MapTag").commit;
        mapFragment.getMapAsync(this);
    }
}

@Override
public void onMapReady(GoogleMap map) {
    this.googleMap = map;
    // Do other stuff as needed
}

然后只需在onResume()中调用setUpMapIfNeeded()。

编辑:将您的fragmentManager更改为getChildFragmentManager();

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

android服务和活动生命周期?

来自分类Dev

Android生命周期和主页按钮

来自分类Dev

finish()和android活动生命周期

来自分类Dev

Android - 通知和生命周期

来自分类Dev

Android Fragment:用于Web服务调用的生命周期方法

来自分类Dev

Android Fragment生命周期变更方向方面的问题

来自分类Dev

Android Google Play服务生命周期

来自分类Dev

Android片段生命周期

来自分类Dev

活动生命周期Android

来自分类Dev

Android Activity 生命周期

来自分类Dev

Android Activity生命周期和锁定/解锁设备

来自分类Dev

服务和Android应用程序生命周期

来自分类Dev

当附加在活动的xml中时,Android Fragment生命周期

来自分类Dev

Android:应用程序的生命周期

来自分类Dev

Android-片段生命周期

来自分类Dev

活动崩溃生命周期方法-Android

来自分类Dev

Android Inter-Activity生命周期

来自分类Dev

Android服务生命周期

来自分类Dev

android生命周期方法不调用

来自分类Dev

Android 4.0 + /活动生命周期

来自分类Dev

onRestoreInstanceState-Android视图生命周期

来自分类Dev

Android Activity 生命周期方法

来自分类Dev

Android Google Maps LocationClient

来自分类Dev

Android Google Maps图标

来自分类Dev

Android-Google Maps

来自分类Dev

在Android的Fragment中使用Google Maps

来自分类Dev

Android蓝牙套接字数据传输和生命周期

来自分类Dev

android 4.4.X:taskAffinity和launchmode与Activity生命周期

来自分类Dev

如果多窗口生命周期会影响android N中的活动生命周期?