How do I navigate from an Android Activity to a Xamarin Forms ContentPage?

FrankDelangheFrankDelanghe BEUniversity ✭✭
edited February 2016 in Xamarin.Forms

I'm developing an application that uses an online payment. I open the link, for the payment, in a browser using: Device.OpenUri(Uri);. When the user has finished paying for his order he gets redirected to an Android Activity using a custom URL scheme:

[Activity(Label = "Urlentryclass", ScreenOrientation = ScreenOrientation.Portrait, Icon = "@drawable/icon",
        MainLauncher = false, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    [IntentFilter(new[] {Android.Content.Intent.ActionView},
        DataScheme = "test",
        Categories = new[] {Android.Content.Intent.CategoryDefault, Android.Content.Intent.CategoryBrowsable})]
 public class Urlentryclass : FormsAppCompatActivity
...

How do I navigate to a certain Forms content page from this activity?
I try to open a certain page using the following code example (It's done by using an Event Handler).

Navigation.PushAsync(new NavigationMenu(sisowStatus, modelStatus)).ConfigureAwait(false);

Followed by this.finish();

Hower, the view that's being showed is the last view that was opened before the browser shows.
It seems that Navigation.PushAsync doesn't affect the view that's being showed. I've try PopAsync, PopToRootAsync but nothing seems to be working.

For those that are interested in the Event Handler, here it is:

public static class PaymentNavigationHelper
{
    public delegate void PaymentExecutedHandler(string sisowStatus, string orderStatus);
    public static event PaymentExecutedHandler OnPaymentExecuted;

    public static void PaymentExecuted(string sisowStatus, string orderStatus)
    {
        OnPaymentExecuted?.Invoke(sisowStatus, orderStatus);
    }
}

I'm subsribing to the event on the Content Page where I'm starting the payment:

 PaymentNavigationHelper.OnPaymentExecuted += (sisowStatus, modelStatus) =>
 {
     Navigation.PushAsync(new NavigationMenu(sisowStatus, modelStatus)).ConfigureAwait(false);
 };

In the activity which handles the payment url, the following code is being used:

PaymentNavigationHelper.PaymentExecuted(status, test.Model.FirstOrDefault().Status);

Any advice is highly appreciated.

Best Answer

Answers

  • FrankDelangheFrankDelanghe BEUniversity ✭✭

    That solved the problem! Thanks Michael

  • ShahriatHossainShahriatHossain USMember ✭✭

    @MichaelRumpler said:
    Xamarin.Forms runs only in one Activity on Android. So if your url request comes out in a different Activity, you have to switch back to the MainActivity before you can use the normal XF navigation.

    I do this when a user opens a file associated with my app.

    [Activity(Label = "LaunchFileActivity")]
    public class LaunchFileActivity : Activity
    {
    protected override void OnCreate(Bundle bundle)
    {
    base.OnCreate(bundle);

          if (Intent.Data != null)
          {
              var uri = Intent.Data;
              if (uri != null)
              {
                  Intent i = new Intent(this, typeof(MainActivity));
                  i.AddFlags(ActivityFlags.ReorderToFront);
                  i.PutExtra("fileName", uri.Path);
                  this.StartActivity(i);
              }
          }
          this.FinishActivity(0);
      }
    

    }

    And in MainActivity:

      protected override void OnNewIntent(Intent intent)
      {
          base.OnNewIntent(intent);
          Intent = intent;
      }
    
      protected override void OnPostResume()
      {
          base.OnPostResume();
          if (Intent.Extras != null)
          {
              string fileName = Intent.Extras.GetString("fileName");
              if (!string.IsNullOrEmpty(fileName))
              {
                  // do something with fileName
              }
              Intent.RemoveExtra("fileName");
          }
      }
    

    Hi there, I have only one MainActivity on my droid project and several pages on my pcl project. I have confusion on your code whether fileName is the PageName of my pcl project or not?

Sign In or Register to comment.