3

I'm trying to launch an activity from preference-header.

I searched a lot for a solution but didn't find except this question :Start activity from preference-headers.

but when applying its answer I got a new Exception :

android.content.ActivityNotFoundException: Unable to find explicit activity class {com.package.myapp/com.package.myapp.MyActivity}; have you declared this activity in your AndroidManifest.xml?

This is my code:

<header
    android:icon="@drawable/ic_contacts"
    android:title="@string/menuOptionContact">
    <intent android:targetPackage="com.package.myapp"
        android:targetClass="com.package.myapp.MyActivity" />
</header>

I tried to change the code to many forms like this: android:targetClass="MyActivity"

Or android:targetClass=".MyActivity"

all gave me same Exception

I tried to start it from inside the corresponding fragment like this :

<header
    android:fragment="com.package.myapp.SettingsActivity$ContactPreferenceFragment"
    android:icon="@drawable/ic_contacts"
    android:title="@string/menuOptionContact">
</header>

and inside my fragments onCreate I put :

  startActivity(new Intent(getActivity(), MyActivity.class));

it launches the activity but when I tap back from MyActivity it takes me to a blank fragment then I have to tap back again to go back to SettingsActivity

How can I launch MyActivity directly from preference-header or get rid of the blank fragments in back tap?

Edit

My AndroidManifest.xml :

   <activity
        android:name=".MyActivity"
        android:label="@string/app_name">
    </activity>

any help will be very appreciated

Community
  • 1
  • 1
Mohammad
  • 1,185
  • 1
  • 14
  • 26

1 Answers1

2

It been a long time but in case someone is still trying to deal with this issue, I solved it like so In your preference header file add this

<header
android:title="@string/menuOptionContact">
    <intent android:action="your custom string"
      android:targetClass="com.package.myapp.MyActivity"/>
</header>

in your manifest file include the following:

<activity android:name=".activity.MyActivity" 
              android:label="@string/my_activity_title"
              android:theme="@android:style/Theme.Black.NoTitleBar">
        <intent-filter>
           <action android:name="your custom string" />
           <category android:name="android.intent.category.DEFAULT" />
       </intent-filter>
    </activity>

In my case the error is caused when I include targetPackage alongside targetClass the system end up concatenating both together According to android developer guide both targetClass and targetPackage is synonymous to intent.setComponent method.

lyrics
  • 61
  • 6
  • Thank you very much, if I had the time to test it I'll do and if it works I'll accept your answer, for now +1 for your effort. Have a nice day :) – Mohammad Jul 28 '18 at 09:40