<NME> NavigateAnyPointActivity.java
<BEF> package cgeo.geocaching;

import cgeo.geocaching.activity.AbstractActionBarActivity;
import cgeo.geocaching.connector.internal.InternalConnector;
import cgeo.geocaching.enumerations.LoadFlags;
import cgeo.geocaching.enumerations.WaypointType;
import cgeo.geocaching.list.StoredList;
import cgeo.geocaching.location.Geopoint;
import cgeo.geocaching.models.Geocache;
import cgeo.geocaching.models.Waypoint;
import cgeo.geocaching.storage.DataStore;
import cgeo.geocaching.ui.GeoItemSelectorUtils;
import cgeo.geocaching.ui.dialog.Dialogs;
import cgeo.geocaching.utils.Log;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import cgeo.geocaching.utils.Log;

import org.apache.commons.lang3.StringUtils;

import android.app.Activity;
import android.content.Context;

import java.util.ArrayList;

import de.k3b.geo.api.GeoPointDto;
import de.k3b.geo.api.IGeoPointInfo;
import de.k3b.geo.io.GeoUri;

public class NavigateAnyPointActivity extends AbstractActionBarActivity {
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.cgeo);

        InternalConnector.assertHistoryCacheExists(this);

        // check if "geo" action is requested
        boolean geoActionRequested = false;
        if (getIntent() != null) {
            final GeoUri parser = new GeoUri(GeoUri.OPT_DEFAULT);
            final IGeoPointInfo geo = parser.fromUri(getIntent().getDataString());
            if (geo != null && !GeoPointDto.isEmpty(geo)) {
                Log.i("Received a geo intent: lat=" + geo.getLatitude()
                        + ", lon=" + geo.getLongitude() + ", name=" + geo.getName()
                        + " form " + getIntent().getDataString());
                selectTargetType(this, geo.getLatitude(), geo.getLongitude(), geo.getName());
                geoActionRequested = true;
            }
        }
        if (!geoActionRequested) {
            CacheDetailActivity.startActivity(this, InternalConnector.GEOCODE_HISTORY_CACHE, true);
            finish();
        }
    }

    private static void selectTargetType(final Activity context, final double latitude, final double longitude, @Nullable final String name) {

        final ArrayList<Geocache> items = new ArrayList<>();
        items.add(null);
        items.add(DataStore.loadCache(InternalConnector.GEOCODE_HISTORY_CACHE, LoadFlags.LOAD_CACHE_OR_DB));
        items.addAll(DataStore.loadUDCSorted());

        final ListAdapter adapter = new ArrayAdapter<Geocache>(context, R.layout.cacheslist_item_select, items) {
            @SuppressLint("SetTextI18n")
            @NonNull
            @Override
            public View getView(final int position, final View convertView, @NonNull final ViewGroup parent) {
                final Geocache cache = getItem(position);

                if (cache == null) { // special case: we want to display a "<New cache>" item on top
                    final View view = GeoItemSelectorUtils.getOrCreateView(context, convertView, parent);

                    final TextView title = (TextView) view.findViewById(R.id.text);
                    title.setText("<" + context.getString(R.string.create_internal_cache_short) + ">");

                    final TextView info = (TextView) view.findViewById(R.id.info);
                    info.setText(context.getString(R.string.create_internal_cache));

                    return view;
                }

                return GeoItemSelectorUtils.createGeocacheItemView(context, cache,
                        GeoItemSelectorUtils.getOrCreateView(context, convertView, parent));
            }
        };

        final AlertDialog dialog = Dialogs.newBuilder(context)
                .setTitle(R.string.add_target_to)
                .setAdapter(adapter, (dialog1, which) -> {
        public View getView(final int position, final View convertView, final ViewGroup parent) {
            View rowView = convertView;

            ViewHolder viewHolder;
            if (rowView == null) {
                rowView = getInflater().inflate(R.layout.simple_way_point, parent, false);
                viewHolder = new ViewHolder(rowView);
                        final Geocache cache = DataStore.loadCache(geocode, LoadFlags.LOAD_CACHE_OR_DB);
                        final Waypoint newWaypoint = new Waypoint(null != name ? name : Waypoint.getDefaultWaypointName(cache, WaypointType.WAYPOINT), WaypointType.WAYPOINT, true);
                        newWaypoint.setCoords(new Geopoint(latitude, longitude));
                        newWaypoint.setGeocode(geocode);
                        cache.addOrChangeWaypoint(newWaypoint, true);
                    }
                    CacheDetailActivity.startActivity(context, geocode, which != 0);
                    context.finish();
                })
                .create();
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
    }

}
    @Override
    public boolean onOptionsItemSelected(final MenuItem item) {
        final int menuItem = item.getItemId();

        final Geopoint coords = getDestination();

        if (coords != null) {
            addToHistory(coords);
        }

        switch (menuItem) {
            case R.id.menu_default_navigation:
                navigateTo();
                return true;

            case R.id.menu_caches_around:
                cachesAround();
                return true;

            case R.id.menu_clear_history:
        return super.onOptionsItemSelected(item);
    }

    private void addToHistory(final Geopoint coords) {
        // Add locations to history
        final Destination loc = new Destination(coords);

        }
    }

    private void navigateTo() {
        navigateTo(getDestination());
    }

    private void navigateTo(final Geopoint geopoint) {
        NavigationAppFactory.startDefaultNavigationApplication(1, this, geopoint);
    }

    private void cachesAround() {
        final Geopoint coords = getDestination();

        if (coords == null) {
            showToast(res.getString(R.string.err_location_unknown));
            return;
        // apply projection
        if (StringUtils.isNotBlank(bearingText) && StringUtils.isNotBlank(distanceText)) {
            // bearing & distance
            double bearing;
            try {
                bearing = Double.parseDouble(bearingText);
            } catch (final NumberFormatException ignored) {
                return null;
            }

            double distance;
            try {
                distance = DistanceParser.parseDistance(distanceText,
                        !Settings.useImperialUnits());

    @Override
    public void startDefaultNavigation() {
        navigateTo();
    }

    @Override
    public void startDefaultNavigation2() {
        NavigationAppFactory.startDefaultNavigationApplication(2, this, getDestination());
    }
}

<MSG> fix #4332: no history when navigate via compass

<DFF> @@ -21,6 +21,7 @@ import cgeo.geocaching.utils.Formatter;
 import cgeo.geocaching.utils.Log;
 
 import org.apache.commons.lang3.StringUtils;
+import org.eclipse.jdt.annotation.Nullable;
 
 import android.app.Activity;
 import android.content.Context;
@@ -95,7 +96,7 @@ public class NavigateAnyPointActivity extends AbstractActionBarActivity implemen
         public View getView(final int position, final View convertView, final ViewGroup parent) {
             View rowView = convertView;
 
-            ViewHolder viewHolder;
+            final ViewHolder viewHolder;
             if (rowView == null) {
                 rowView = getInflater().inflate(R.layout.simple_way_point, parent, false);
                 viewHolder = new ViewHolder(rowView);
@@ -347,20 +348,14 @@ public class NavigateAnyPointActivity extends AbstractActionBarActivity implemen
     @Override
     public boolean onOptionsItemSelected(final MenuItem item) {
         final int menuItem = item.getItemId();
-
-        final Geopoint coords = getDestination();
-
-        if (coords != null) {
-            addToHistory(coords);
-        }
-
+        final Geopoint coords = getDestinationAndAddToHistory();
         switch (menuItem) {
             case R.id.menu_default_navigation:
-                navigateTo();
+                navigateTo(coords);
                 return true;
 
             case R.id.menu_caches_around:
-                cachesAround();
+                cachesAround(coords);
                 return true;
 
             case R.id.menu_clear_history:
@@ -374,7 +369,17 @@ public class NavigateAnyPointActivity extends AbstractActionBarActivity implemen
         return super.onOptionsItemSelected(item);
     }
 
-    private void addToHistory(final Geopoint coords) {
+    private Geopoint getDestinationAndAddToHistory() {
+        final Geopoint coords = getDestination();
+        addToHistory(coords);
+        return coords;
+    }
+
+    private void addToHistory(@Nullable final Geopoint coords) {
+        if (coords == null) {
+            return;
+        }
+
         // Add locations to history
         final Destination loc = new Destination(coords);
 
@@ -435,17 +440,16 @@ public class NavigateAnyPointActivity extends AbstractActionBarActivity implemen
         }
     }
 
-    private void navigateTo() {
-        navigateTo(getDestination());
-    }
+    private void navigateTo(final Geopoint coords) {
+        if (coords == null) {
+            showToast(res.getString(R.string.err_location_unknown));
+            return;
+        }
 
-    private void navigateTo(final Geopoint geopoint) {
-        NavigationAppFactory.startDefaultNavigationApplication(1, this, geopoint);
+        NavigationAppFactory.startDefaultNavigationApplication(1, this, coords);
     }
 
-    private void cachesAround() {
-        final Geopoint coords = getDestination();
-
+    private void cachesAround(final Geopoint coords) {
         if (coords == null) {
             showToast(res.getString(R.string.err_location_unknown));
             return;
@@ -508,7 +512,7 @@ public class NavigateAnyPointActivity extends AbstractActionBarActivity implemen
         // apply projection
         if (StringUtils.isNotBlank(bearingText) && StringUtils.isNotBlank(distanceText)) {
             // bearing & distance
-            double bearing;
+            final double bearing;
             try {
                 bearing = Double.parseDouble(bearingText);
             } catch (final NumberFormatException ignored) {
@@ -516,7 +520,7 @@ public class NavigateAnyPointActivity extends AbstractActionBarActivity implemen
                 return null;
             }
 
-            double distance;
+            final double distance;
             try {
                 distance = DistanceParser.parseDistance(distanceText,
                         !Settings.useImperialUnits());
@@ -542,11 +546,11 @@ public class NavigateAnyPointActivity extends AbstractActionBarActivity implemen
 
     @Override
     public void startDefaultNavigation() {
-        navigateTo();
+        navigateTo(getDestinationAndAddToHistory());
     }
 
     @Override
     public void startDefaultNavigation2() {
-        NavigationAppFactory.startDefaultNavigationApplication(2, this, getDestination());
+        NavigationAppFactory.startDefaultNavigationApplication(2, this, getDestinationAndAddToHistory());
     }
 }

fix #4332: no history when navigate via compass