16

In my preference file I go this

    <PreferenceCategory android:title="Title" >
        <Preference android:title="title" >
    <intent android:action="com.my.package.MainActivity" 
            />
</Preference>
    </PreferenceCategory>

The activity is created in the manifest file , but I still get

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.my.package.MainActivity }

How can I start activity from preferences screen ?

Lukap
  • 31,523
  • 64
  • 157
  • 244

6 Answers6

33

I had the same issue but none of the solutions i searched on stackoverflow solved my activitynotfound Exception.

Here is the working solution i found from here:

    <PreferenceScreen  
                android:title="@string/title_intent_preference"  
                android:summary="@string/summary_intent_preference">  

            <intent android:action="your.action.string"/>  

 </PreferenceScreen>  

set an intent filter in your activity inside manifest.xml

    <activity ...>  
            <intent-filter>  
                <action android:name="your.action.string"/>  
                <category android:name="android.intent.category.DEFAULT" />  
            </intent-filter>  
</activity>
Mehdi Fanai
  • 4,021
  • 13
  • 50
  • 75
6

This isn't the right way to start an intent from xml. The android:action field isn't for the name of the activity you are trying to start; it describes an action for an intent-filter (such as android.intent.action.VIEW or android.intent.action.EDIT) that another activity can supply.

See this answer for the correct use of <intent>, android:action, etc: https://stackoverflow.com/a/3751306/582004

Make sure that in your AndroidManifest.xml, your activity contains an <intent-filter> with the <action> that you are requesting in your PreferenceActivity (in the answer referenced, this is android.intent.action.VIEW).

dsh
  • 12,037
  • 3
  • 33
  • 51
liucheia
  • 364
  • 6
  • 16
6

try this

 <intent android:targetPackage="your.package"
 android:targetClass="your.package.yourMainClassName"/>
tarik203
  • 375
  • 3
  • 9
5

In your manifest :

This is the definition for your activity called [your package].MainActivity.

<activity android:name=".MainActivity">
 <intent-filter>
      <action android:name="example.action.ACTION_MAIN_ACTIVITY" />
      <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

Also, try using a PreferenceScreen:

<PreferenceScreen android:title="@string/my_location_settings">
    <intent android:action="example.action.ACTION_MAIN_ACTIVITY">
    </intent>
</PreferenceScreen>

for more details please see this link... starting an activity from preferences.xml

Community
  • 1
  • 1
Priyank Patel
  • 12,244
  • 8
  • 65
  • 85
3

You should do something like this in your intent declaration inside the preference xml:

<intent android:targetPackage="abc.def.efg"
 android:targetClass="abc.def.efg.hig.yourClassName"/>

Note: targetPackage should be same as the package property declared inside the manifest tag of AndroidManifest.xml. This is confusing sometimes, so read it again.

So equivalent AndroidManifest.xml would have declaration like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="abc.def.efg">
<application>
   ....
    <activity android:name=".hig.yourClassName"/>
</application>
</manifest>
MSS
  • 3,306
  • 1
  • 19
  • 50
0

To start an activity from preference screen, you can just modify your preference XML like

<Preference
    app:key="your_key"
    app:title="@string/title"
    app:summary="@string/summary">

    <intent
        android:targetPackage="your.package"
        android:targetClass="your.package.ActivityClass"/>
</Preference>

or, if you need passing action, just put your action string inside intent property like

<Preference
    app:key="your_key"
    app:title="@string/title"
    app:summary="@string/summary">

    <intent
        android:targetPackage="your.package"
        android:targetClass="your.package.ActivityClass"
        android:action="your.package.ACTION"/>
</Preference>
Gilbert Arafat
  • 441
  • 5
  • 10