Midhun Kumar
Midhun Kumar

Reputation: 577

How to navigate to a Xamarin.Android module activity from a Xamarin.forms Page? How to achieve the same in Xamarin.iOS as well?

am relatively new to Xamarin. I have a solution with Xamarin.Forms project and a Xamarin.Android(Native) project. Is it possible to launch the Activity in the Native module from a 'Page' in Xamarin.forms, say when we click on a button. Also is the same applicable in Xamarin.iOS ?

Upvotes: 0

Views: 767

Answers (1)

Robbit
Robbit

Reputation: 4358

Is it possible to launch the Activity in the Native module from a 'Page' in Xamarin.forms, say when we click on a button

The answer is yes.


You can use DependencyService to achieve it.

In Android:

1) Define a interface in PCL:

public interface IOpenPage
{
    void OpenPage();
}

2) Implement the interface in Android:

[assembly: Dependency(typeof(OpenPageImpl))]
namespace App2.Droid
{
    class OpenPageImpl : IOpenPage
    {   

        public void OpenPage()
        {
            Forms.Context.StartActivity(new Intent(Forms.Context, typeof(TextActivity)));
        }
    }
}

3) In your button's click event:

private void ToPage(object sender, EventArgs e)
{
    DependencyService.Get<IOpenPage>().OpenPage();
}

Update:

Xamarin.forms project:

enter image description here

Upvotes: 4

Related Questions