Launch new activity from PreferenceActivity Launch new activity from PreferenceActivity android android

Launch new activity from PreferenceActivity


Given you are using xml preferences you can add code right into the xml:

<Preference    android:title="Some Title"    android:summary="Some Description">    <intent         android:action="android.intent.action.VIEW"        android:targetPackage="com.package.name"        android:targetClass="com.package.name.ActivityName"    /></Preference>


After you add preferences using

addPreferencesFromResource(R.xml.preferences);

find your preference that you want to set onClick using

findPreference("foo_bar_pref");

and define it by casting like

Preference fooBarPref = (Preference) findPreference("foo_bar_pref");

Then you can easily set its onClick using

fooBarPref.setOnPreferenceClickListener (new OnPreferenceClickListener()){...}

You can start your new Activity (using an Intent) inside that listener.


Gradle Builders, Look Over Here!

If you are using gradle over ant as your build tool, and you declared an applicationId inside android.

[build.gradle]:android {    defaultConfig {        ...        applicationId "com.overriding.package.name"    }    ...}

This will overwrite whatever value you declared in AndroidManifest.xml's android:package as your app's unique identifier!

[AndroidManifest.xml]<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.my.package">    <activity android:name=".settings.MyActivity"/></manifest>

The <intent> would have to take both package names into account!

<Preference    android:title="Some Title"    android:summary="Some Description">    <intent         android:targetPackage="com.overriding.package.name"        android:targetClass="com.my.package.settings.MyActivity/></Preference>