Search code examples
androidxamarinandroid-intentandroid-activitypreference

Xamarin Android Preference Activity Intent "Unable to find explicit activity class"


I use the latest Xamarin and Visual Studio 2017. I have a test activity "Sample" that I want to run from the preference. I tried in different ways and still nothing. Any ideas?

Sample class to run:

  namespace TestApp
    {
        [Activity(Label = "Sample", Name = "pl.test.TestApp.Sample")]
        [IntentFilter(new[] { Intent.ActionView })]
        public class Sample : Android.Preferences.PreferenceActivity
        {
            protected override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
                SetContentView(Resource.Layout.login);           
            }
        }
    }

Using this I try run Sample activity:

<SwitchPreferenceCompat
        android:key="pref_pow"
        android:title="Pow pow"
        android:summary="Pow summary" />
     <PreferenceScreen
            android:key="rename"
            android:title="test"
            android:persistent="false"
            android:summary="test test test">
          <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="pl.test.TestApp"
            android:targetClass="Sample"/>
        </PreferenceScreen>

I'm getting an error:

Android.Content.ActivityNotFoundException: Unable to find explicit activity class {pl.test.testapp/Sample}; have you declared this activity in your AndroidManifest.xml?

But in Xamarin I shouldn't edit AndroidManifest.xml


Solution

  • The targetClass should be the fully qualified class name and since you are using the ActivityAttribute with a param of Name = "pl.test.TestApp.Sample", then your intent should be:

    ~~~
    android:targetPackage="pl.test.TestApp"
    android:targetClass="pl.test.TestApp.Sample"/>
    ~~~