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 ?
Asked
Active
Viewed 697 times
0
-
To be honest NO! – FreakyAli Feb 26 '18 at 13:03
-
@G.hakim, why no? – Robbit Feb 26 '18 at 13:11
-
You want an Android code to work in xamarin forms for ios right? or maybe I'm not getting you, your question looks ambiguous – FreakyAli Feb 26 '18 at 13:14
-
To clarify I have xamarin.android module and xamarin.ios module as well. And am trying to access these in the specific platform from the xamarin.forms module. – Midhun Kumar Feb 26 '18 at 13:16
-
@JoeLvMSFT Trying out the solution seen in the picture. What happened to me was that I had already created modules in Xamarin.Android and Xamarin.iOS (ie. native projects). Later I decided to add a Xamarin forms project as the launch module. Hence ending up with App,App.Droid and App.iOS and additionally App2.Droid and App2.iOS modules. – Midhun Kumar Feb 26 '18 at 14:22
-
@JoeLvMSFT right now am trying to replace the App.Droid and App.iOS modules with App2.Droid and App2.iOS. Any suggestions. – Midhun Kumar Feb 26 '18 at 14:26
-
@MidhunKumar, I am not sure what about `App.Droid and App.iOS modules`, why not create a Xamarin.forms project directly? Or can you give me a picture about your project? – Robbit Feb 26 '18 at 14:31
-
If I am right, you don't have the PCL which I mentioned in my answer, and now you want to add it? You can create a `Cross-Platform`->`Class Library`, in your solution, and then reference it in your Android and Ios project. – Robbit Feb 26 '18 at 14:35
1 Answers
4
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:

Robbit
- 4,300
- 1
- 13
- 29
-
So I need to define the interface in the android module which is part of the xamarin.forms project . And implement the interface inside the xamarin.android module? – Midhun Kumar Feb 26 '18 at 13:14
-
By `android module`, what do you mean? Your `xamarin.forms module` is my PCL. – Robbit Feb 26 '18 at 13:18
-
No, you need define the interface in xamarin.forms module. And inplement the interface inside the xamarin.android module. – Robbit Feb 26 '18 at 13:25
-
So it will be implemented in the Xamarin.Android Project, the native one, which was not generated as part of the Xamarin Forms project? – Midhun Kumar Feb 26 '18 at 14:01
-
-
But Forms.Context is obsolete now. Is there any workaround for that. – Midhun Kumar Feb 27 '18 at 07:29
-
Try this `Android.App.Application.Context.StartActivity(new Intent(Android.App.Application.Context, typeof(TextActivity)));` – Robbit Feb 27 '18 at 07:38
-
I was able to implement the delegate and open the forms page in iOS as well but not sure about how to open another view controller from this page. ie how we will use this IOpenPage interface here. – Midhun Kumar Feb 27 '18 at 10:13
-
Any idea about how to achieve the same in iOS, ie how to push a view controller into the scene from Xamarin.Forms? – Midhun Kumar Mar 19 '18 at 19:18
-
Also, the android implementation works sometimes and crashes sometimes. Observed the error message: Android.Util.AndroidRuntimeException Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? xamarin forms – Midhun Kumar Mar 20 '18 at 06:42
-
@MidhunKumar `FLAG_ACTIVITY_NEW_TASK `, you need add a flag, [here](https://stackoverflow.com/questions/3918517/calling-startactivity-from-outside-of-an-activity-context), because you are using Application Context, so there is no stack for it. – Robbit Mar 20 '18 at 06:45
-
@MidhunKumar About Ios, sorry, I am not familiar with Ios, but I think the idea is the same. You can open an new thread with Xamarin.Ios tag. – Robbit Mar 20 '18 at 06:48
-
so while navigating from forms to native through dependency service do we have to add this flag? – Midhun Kumar Mar 20 '18 at 06:50
-
If you can get MainActivity's Context, you don't need it, I haven't tried it, and I don't think I can get it, so I usually use the Application's Context. Every Activity must have [taskAffinity](https://developer.android.com/guide/topics/manifest/activity-element.html?#aff). – Robbit Mar 20 '18 at 06:52
-
Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/167143/discussion-between-midhun-kumar-and-joe-lv-msft). – Midhun Kumar Mar 20 '18 at 07:11