Home > Net >  Android/Java: How to inherit from a class into an activity that extends AppCompatActivity?
Android/Java: How to inherit from a class into an activity that extends AppCompatActivity?

Time:11-08

I am quite new to Android, and I don't know how to inherit from the class 'HereMapClass', in my case, into an activity 'HereMap' that extends AppCompatActivity?

I think there is code in double, the goal is to have the main class that inherit all the functions of the HereMapClass. I would like to get the class HereMapClass to manage the download of map etc... and the main class just to manage the map visually on the phone in a fragment. I did it that way because I need to use the class separate as I use it in multiple place in the code but without needing to display the map visually.

Here is the code of the Main class :

HereMap Class activity:

import androidx.appcompat.app.AppCompatActivity;

public class HereMap extends AppCompatActivity {

    private MapView view;
    private AndroidXMapFragment mapFragment = null;
    private MapRoute m_mapRoute;
    JSONArray jsonPoints;
    JSONArray jsArray;
    ArrayList<String> poiArr;
    String gpsFolder;
    String poiFolder;
    String loopName = "";
    String loopLang = "";
    String loopEvent = "";

    Context mContext;
    Intent intent;
    Bundle bundle2;


    private final static String TAG = HereMap.class.getSimpleName();
    private MapView mapView;
    private Map map;
    private static Image m_marker_image;
    //MapScreenMarker m_tap_marker;
    MapMarker m_tap_marker2;
    private ArrayList<MapPackage> currentInstalledMaps;
    private String currentInstalledMapsString;
    private ProgressBar downloadProgressBar;
    private PositioningManager positioningManager = null;
    private PositioningManager.OnPositionChangedListener positionListener;
    private GeoCoordinate currentPosition;


    // listener for MapLoader
    private MapLoader.Listener mapLoaderHandler = new MapLoader.Listener() {

        @Override
        public void onProgress(int progress) {
            Log.i(TAG, "Progress "   progress   "%");
            downloadProgressBar.setProgress(progress);
        }

        @Override
        public void onInstallationSize(long diskSize, long networkSize) {
            Log.i(TAG, "Map data require "   diskSize);
        }

        @Override
        public void onGetMapPackagesComplete(MapPackage rootMapPackage,
                                             MapLoader.ResultCode resultCode) {
            if (resultCode == MapLoader.ResultCode.OPERATION_SUCCESSFUL) {
                Log.i(TAG, "Map packages received successful: "   rootMapPackage.getTitle());

                currentInstalledMaps = new ArrayList<>(1);
                populateInstalledMaps(rootMapPackage);
            } else {
                Log.e(TAG, "Can't retrieve map packages: "   resultCode.name());
                Toast.makeText(HereMap.this,
                        "Error: "   resultCode.name(), Toast.LENGTH_SHORT).show();
                return;
            }

            StringBuilder sb = new StringBuilder();
            for (MapPackage pac : currentInstalledMaps) {
                sb.append(pac.getTitle());
                sb.append("\n");
            }

            currentInstalledMapsString = sb.toString();
        }


        private void populateInstalledMaps(MapPackage pac) {
            // only take installed root package, so if e.g. Germany is installed,
            // don't check for children states
            if (pac.getInstallationState() == MapPackage.InstallationState.INSTALLED) {
                Log.i(TAG, "Installed package found: "   pac.getTitle()   " id "   pac.getId());
                currentInstalledMaps.add(pac);
            } else if (pac.getChildren() != null && pac.getChildren().size() > 0) {
                for (MapPackage p : pac.getChildren()) {
                    populateInstalledMaps(p);
                }
            }
        }
...

Here is the class to inherit into the main one:

HereMapClass:

public class HereMapClass {

    private MapView view;
    private AndroidXMapFragment mapFragment = null;
    private MapRoute m_mapRoute;
    private  MapEngine mapEngine;
    private MapLoader mapLoader;

    JSONArray jsonPoints;
    JSONArray jsArray;
    ArrayList<String> poiArr;
    String gpsFolder;
    String poiFolder;
    String loopName = "";
    String loopLang = "";
    String loopEvent = "";

    Context mContext;
    Intent intent;
    Bundle bundle2;


    private final static String TAG = HereMap.class.getSimpleName();
    private MapView mapView;
    private Map map;
    private static Image m_marker_image;
    //MapScreenMarker m_tap_marker;
    MapMarker m_tap_marker2;
    private ArrayList<MapPackage> currentInstalledMaps;
    private String currentInstalledMapsString;
    private ProgressBar downloadProgressBar;
    private PositioningManager positioningManager = null;
    private PositioningManager.OnPositionChangedListener positionListener;
    private GeoCoordinate currentPosition;

    private Context context;

    public HereMapClass(Context context){
        this.context=context;
    }

    // listener for MapLoader
    private MapLoader.Listener mapLoaderHandler = new MapLoader.Listener() {
            @Override
            public void onProgress(int progress) {
                Log.i(TAG, "Progress "   progress   "%");
                downloadProgressBar.setProgress(progress);
            }

            @Override
            public void onInstallationSize(long diskSize, long networkSize) {
                Log.i(TAG, "Map data require "   diskSize);
            }

            @Override
            public void onGetMapPackagesComplete(MapPackage rootMapPackage,
                                                 MapLoader.ResultCode resultCode) {
                if (resultCode == MapLoader.ResultCode.OPERATION_SUCCESSFUL) {
                    Log.i(TAG, "Map packages received successful: "   rootMapPackage.getTitle());

                    currentInstalledMaps = new ArrayList<>(1);

                    populateInstalledMaps(rootMapPackage);


                } else {
                    Log.e(TAG, "Can't retrieve map packages: "   resultCode.name());
                    Toast.makeText(context,
                            "Error: "   resultCode.name(), Toast.LENGTH_SHORT).show();
                    return;
                }

                StringBuilder sb = new StringBuilder();
                for (MapPackage pac : currentInstalledMaps) {
                    sb.append(pac.getTitle());
                    sb.append("\n");
                }

                currentInstalledMapsString = sb.toString();
            }


            private void populateInstalledMaps(MapPackage pac) {
                // only take installed root package, so if e.g. Germany is installed,
                // don't check for children states
                if (pac.getInstallationState() == MapPackage.InstallationState.INSTALLED) {
                    Log.i(TAG, "Installed package found: "   pac.getTitle()   " id "   pac.getId());
                    currentInstalledMaps.add(pac);
                } else if (pac.getChildren() != null && pac.getChildren().size() > 0) {
                    for (MapPackage p : pac.getChildren()) {
                        populateInstalledMaps(p);
                    }
                }

                //continuez à installer
                //lancer notification pour login
                
            }
...

Best regards

CodePudding user response:

What you are asking for is multiple inheritance. You want a class to inherit from both HereMapClass and AppCompatActivity. That isn't possible in Java as Java language does not support multiple inheritance.

Since you are building an Activity, you must inherit from AppCompatActivity. Therefore, to use HereMapClass you will need to use delegation instead of inheritance.

Create an instance of HereMapClass and store a reference in a member variable in your Activity. When you want to use HereMapClass, simply call methods on your instance of the class.

Basically, you are "delegating" some responsibility to the instance of HereMapClass.

CodePudding user response:

Use delegation instead of inheritance. Unlike some other object oriented programming languages, Java doesn’t provide support for multiple inheritance in classes. More info on that: https://www.digitalocean.com/community/tutorials/multiple-inheritance-in-java

For your use case, you could create an interface (ignore the naming :))

    public interface HereMap {
    void onProgress();
    void populateInstalledMaps();
}

Create an implementation of that interface:

public class IHereMapImpl implements IHereMap {
    @Override
    public void onProgress() {
        //add logic
    }

    @Override
    public void populateInstalledMaps() {
        ////add logic
    }
}

use this interface around where you need:

public class HereMap extends AppCompatActivity {

    private IHereMap iHereMap;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        iHereMap = new IHereMapImpl();
        iHereMap.onProgress();
        iHereMap.populateInstalledMaps();
    }
}

Recommendation: Android Architecture Guide

Hope it helps :)

  • Related