BArtWell
BArtWell

Reputation: 4044

How to call Activity from PreferenceActivity?

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

Upvotes: 0

Views: 3861

Answers (3)

Bondax
Bondax

Reputation: 3163

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.

Upvotes: 1

SSL
SSL

Reputation: 278

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;
    }
});

Upvotes: 6

Nicklas Gnejs Eriksson
Nicklas Gnejs Eriksson

Reputation: 3415

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.

Upvotes: 3

Related Questions