0

I'm trying to start an activity from a PreferenceScreen. I can see, using the debugger, that the onCreate method of the Activity gets called but unfortunately nothing is shown. Here is my code.

<PreferenceScreen>
    <PreferenceScreen>
        <PreferenceCategory>
            ....
        </PreferenceCategory>    
    </PreferenceScreen>
    <PreferenceScreen android:title="title" android:key="key">
        <intent android:action="android.intent.action.VIEW"
        android:targetPackage="com.example.package" android:targetClass="com.example.package.MyActivity"/>
    </PreferenceScreen>

My manifest.xml:

<activity android:name="com.example.package.MyActivity" android:label="@string/title">
    <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
    </intent-filter>
</activity>

My Activity:

package com.example.package;
public class MyActivity extends Activity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myactivity_layout);

    }
}
ColdFire
  • 6,764
  • 6
  • 35
  • 51
Antonio La Marra
  • 5,949
  • 4
  • 15
  • 23
  • are you setting `setContentView()` in `onCreate()`? If you are already doing that, then update your question with code for `onCreate()` – Sagar Apr 10 '18 at 12:47
  • @Sagar I added the setContentVIew() it was already in the activity – Antonio La Marra Apr 10 '18 at 12:51
  • For PreferenceScreen you can use 'addPreferencesFromResource(R.xml.myactivity_layout); ' method in place of setContentView(R.layout.myactivity_layout); , though it is depricated for using directly in activity, still works. – Danger Apr 10 '18 at 12:55
  • you are missing `` in your intent filter – Sagar Apr 10 '18 at 12:59
  • I think this would help https://stackoverflow.com/questions/14810393/start-activity-from-preference-screen-intent-defined-in-xml-file/16943447 – Uday Ramjiyani Apr 10 '18 at 13:02
  • @Sagar added that modification but didn't help – Antonio La Marra Apr 10 '18 at 13:06
  • @AntonioLaMarra instead of `android.intent.action.VIEW` can you try to create your own action String: `com.example.package.StartMain`. update it in manifest as well as Preferences. – Sagar Apr 10 '18 at 13:08
  • @Sagar unfortunately it doesn't work – Antonio La Marra Apr 10 '18 at 13:12
  • As @UdayRamjiyani suggested, I've tried also to remove targetPackage and targetClass, but also this didn't help – Antonio La Marra Apr 10 '18 at 13:16
  • I've tried with this https://developer.android.com/guide/topics/ui/settings#Intents and I'm able to launch the browser. I really don't get why MyActivity is called but nothing is shown – Antonio La Marra Apr 26 '18 at 09:00

0 Answers0