androidhere-api

AndroidXMapFragment returns null when initializing from fragment


SupportMapFragment is deprecated in here sdk and when i start the navigation, on screen rotation it crashing.

I updated to last version of sdk and followed instruction from official documentation: https://github.com/heremaps/here-android-sdk-examples/tree/master/turn-by-turn-navigation/app/src/main/java/com/here/android/example/guidance

I implemented on my project in activity but i need this to work on fragment as before.

i replace everything to AndroidX:

from Activity works

import androidx.appcompat.app.AppCompatActivity;
public MapFragmentView(AppCompatActivity activity) {
   m_activity = activity;
   initMapFragment();
   initNaviControlButton();
}
private AndroidXMapFragment getMapFragment() {
    return (AndroidXMapFragment) m_activity.getSupportFragmentManager().findFragmentById(R.id.mapfragment);
}

Calling this from Activity works

MapFragmentView mapFragmentView = new MapFragmentView (this);

When i call this from Fragment

MapFragmentView mapFragmentView = new MapFragmentView (getActivity ());

This does not accept getActivity() because is not compatible.
androidx.appcompat.app.AppCompatActivity in MapFragmentView can not be applied to androidx.fragment.app.ragmentActivity

I try to cast it and it accepts

AppCompatActivity appCompatActivity = (AppCompatActivity) getActivity ();
MapFragmentView mapFragmentView = new MapFragmentView (appCompatActivity);

But now the method getMapFragment() return null

private AndroidXMapFragment getMapFragment() {
    /* this returns null */ return (AndroidXMapFragment) m_activity.getSupportFragmentManager().findFragmentById(R.id.mapfragment);
}

Here is fragment class, i removed other code to just simplify

package com.example.Fragments;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;

public class FragmentHome extends Fragment {

    private final static int REQUEST_CODE_ASK_PERMISSIONS = 1;
    private static final String[] RUNTIME_PERMISSIONS = {
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.INTERNET,
            Manifest.permission.ACCESS_WIFI_STATE,
            Manifest.permission.ACCESS_NETWORK_STATE
    };
    private final String TAG = "FragmentHome";
    private View view;
    // Directions


    public FragmentHome() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);

    }

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

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

        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated (view, savedInstanceState);

        if (hasPermissions(getActivity (), RUNTIME_PERMISSIONS)) {
            setupMapFragmentView();
        } else {
            ActivityCompat
                    .requestPermissions(getActivity (), RUNTIME_PERMISSIONS, REQUEST_CODE_ASK_PERMISSIONS);
        }
    }

    /**
     * Only when the app's target SDK is 23 or higher, it requests each dangerous permissions it
     * needs when the app is running.
     */
    private static boolean hasPermissions(Context context, String... permissions) {
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && permissions != null) {
            for (String permission : permissions) {
                if (ActivityCompat.checkSelfPermission(context, permission)
                        != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CODE_ASK_PERMISSIONS: {
                for (int index = 0; index < permissions.length; index++) {
                    if (grantResults[index] != PackageManager.PERMISSION_GRANTED) {

                        /*
                         * If the user turned down the permission request in the past and chose the
                         * Don't ask again option in the permission request system dialog.
                         */
                        if (!ActivityCompat
                                .shouldShowRequestPermissionRationale(getActivity (), permissions[index])) {
//                            Toast.makeText(this, "Required permission " + permissions[index]
//                                            + " not granted. "
//                                            + "Please go to settings and turn on for sample app",
//                                    Toast.LENGTH_LONG).show();
                        } else {
//                            Toast.makeText(this, "Required permission " + permissions[index]
//                                    + " not granted", Toast.LENGTH_LONG).show();
                        }
                    }
                }

                setupMapFragmentView();
                break;
            }
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

    private void setupMapFragmentView() {
        // All permission requests are being handled. Create map fragment view. Please note
        // the HERE Mobile SDK requires all permissions defined above to operate properly.
        AppCompatActivity appCompatActivity = (AppCompatActivity) getActivity ();
        MapFragmentView mapFragmentView = new MapFragmentView (appCompatActivity);

    }

    public interface OnFragmentInteractionListener {
    }
}

Layout of Framgnet

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:mapbox="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/homeToolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/AppTheme"
        app:popupTheme="@style/AppTheme"
        android:visibility="gone">

        <RelativeLayout
            android:id="@+id/homeToolbarItems"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="visible">

            <TextView
                android:id="@+id/appTitle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Home"
                android:textAlignment="center"
                android:textColor="@color/color_white"
                android:textSize="20sp"
                android:textStyle="bold" />


        </RelativeLayout>

    </androidx.appcompat.widget.Toolbar>

    <!-- Map Fragment embedded with the map object -->
    <fragment
        class="com.here.android.mpa.mapping.AndroidXMapFragment"
        android:id="@+id/mapfragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</RelativeLayout>

Solution

  • I figured it out. Here is if anyone have troubles implementing it from Fragment.

    But this is with SupportMapFragment, also works with AndroidXMapFragment

    public class FragmentHome extends Fragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            view = inflater.inflate (R.layout.fragment_home, container, false);
    
            SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager ().findFragmentById (R.id.mapFragment);
            MapFragmentView fragmentMap = new MapFragmentView ((AppCompatActivity) getActivity (), mapFragment);
    
            return view;
        }
    }
    
    

    In MapFragmentView constructor i have added one more parameter for SupportMapFragment

    public class MapFragmentView {
    
        private AppCompatActivity m_activity;
        private SupportMapFragment mapFragment;
    
        public MapFragmentView(AppCompatActivity activity, SupportMapFragment mapFragment) {
            m_activity = activity;
            this.mapFragment = mapFragment;
            initialize (); // This is init map
        }
    }