BackKey toggle between MainActivity and Activity-Intent

In MonoDroid I have added GoogleMap using Intent. When I use my backKey I go to my MainActivity. How do I go back to my GoogleMap Intent. My research shows solution using Navigation but that is not available in Mono.

How can I go to Main without executing OnResume and then GoogleMap and just cycle between these two using backKey.

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    Try to override the OnBackPressed method to achieve the navigation between Activity and Maps.It‘ll affect the exit of the application, you can add a bool parameter to detect the selection.

    public class MainActivity : AppCompatActivity
    {
        bool IsToLaunchMaps;
        ...
        public override void OnBackPressed()
        {
            if (IsToLaunchMaps)
            {
                var uri = Android.Net.Uri.Parse("geo:42.37426660,-71.12420824");
                StartActivity(new Intent(Intent.ActionView, uri));
            }
            else
            {
                base.OnBackPressed();
            }
        }
    }
    
  • GolfMan2000GolfMan2000 Member ✭✭

    I have that already.

    First backKey starts the Map.
    Second BackKey gives me back what I had

    so far so good

    Third BackKey brings me to Start Screen...no good

    I want to toggle between First and Second indefinitely. Also when Second BackKey brings me to MainActivity it triggers OnResume event... I don't want my code executed.

  • JarvanJarvan Member, Xamarin Team Xamurai

    @GolfMan2000

    when Second BackKey brings me to MainActivity it triggers OnResume event... I don't want my code executed.

    Add a bool in Activity parameter to detect if it's the first time to display the Activity.

    bool ISFirstTime;
    protected override void OnResume()
    {
        if (!ISFirstTime)
        {
            Console.WriteLine("==OnResume==");
            ISFirstTime = true;
        }
        base.OnResume();
    }
    
  • GolfMan2000GolfMan2000 Member ✭✭

    I already have that. In fact I use in OnBackPressed event

            bounds = MyMap.Projection.VisibleRegion.LatLngBounds;
            MyMap.MoveCamera(CameraUpdateFactory.NewLatLng(new LatLng(bounds.Center.Latitude,                          bounds.Center.Longitude)));
            MyMap.AnimateCamera(CameraUpdateFactory.ZoomTo(zoom));
            var geoUri = Android.Net.Uri.Parse("geo:" + bounds.Center.Latitude + "," + bounds.Center.Longitude + "?z=" + zoom);
            MapIntent = new Intent(Intent.ActionView, geoUri).SetFlags(ActivityFlags.BroughtToFront);
            StartActivity(MapIntent);
    

    this allows me to toggle between Intent and Mainactivity indefinitely. Ofcourse I can block OnResume in MainActivity but Intent is started each time.

    How can I go back to the intent that was displaying already before … rather then start new one each time

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited August 6

    How can I go back to the intent that was displaying already before … rather then start new one each time

    It'll be hardly to do that. Because when you click the backkey to go back to MainActivity, google map has been closed. Even if open the map manually now, you will find that the map application is restarted instead of being opened.

  • GolfMan2000GolfMan2000 Member ✭✭

    well if it was easy I would have done that. It can be done since when your look at all running app you can manually select one to view without restarting it. As if you have two photos (or state of being) and you toggle between the two.

Sign In or Register to comment.