0

I need to launch new Activity from PreferenceActivity on button click or somehow else. Is it possible? How to make it?

BArtWell
  • 4,176
  • 10
  • 63
  • 106

3 Answers3

6

You can start another Activity from your PreferenceActivity like the standard way of doing that. For example:

Intent testIntent = new Intent(getApplicationContext(), Activity2.class);
startActivity(testIntent);

First, define a Preference in your XML:

<Preference
    android:key="test_pref"
    android:summary="@string/someDescription"
    android:title="Some Random Title" >
</Preference>

In your PreferenceActivity:

Preference pref = findPreference("test_pref");
shareSociallyYou.setOnPreferenceClickListener(new OnPreferenceClickListener() {

    @Override
    public boolean onPreferenceClick(Preference preference) {

        Intent testIntent = new Intent(getApplicationContext(), Activity2.class);
        startActivity(testIntent);

        return true;
    }
});
SSL
  • 278
  • 2
  • 17
3

This should work

Preference preference = findPreference("Your Preference Key");
    preference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            Intent intent = new Intent(getApplicationContext(), YourActivity.class);
            startActivity(intent);
            return true;
        }
    });

This should be in the oncreate or similar.

Nicklas Gnejs Eriksson
  • 3,395
  • 2
  • 21
  • 19
1

You can set the intent as preference action in XML as well. Just add to your preference XML:

<PreferenceScreen android:key="KEY" android:title="DOYOURWORK">
    <intent android:targetClass="com.yourcompany.app.youractivity"
        android: targetPackage="com.yourcompany.app">
        <extra android:name="EXTRA_KEY" android:value="yourValue" />
    </intent>
</PreferenceScreen>

and now the tricky part i wanted to share: The xml attribute android:targetPackage in the <intent> node refers to your application package, NOT THE JAVA PACKAGE! So as long as you work within your app and not calling external intents, you just have to state your application package no matter in which JAVA package the activity class is located in your application project.

I hope this helps, i couldn't find any documentation on this stuff, just user postings in the web.

Bondax
  • 3,143
  • 28
  • 35