Alex Rivera | Logout

Start Activity from preferences.xml and get the result in onActivityResult

Asked 2012-03-03T10:12:23.647
17

It's a complement to this question.

I can launch the Activity but I also need to be able to get the result. How do I do it?

I tried overriding onActivityResult on my PreferencesActivity to no avail.

Am I missing some extra properties in the preferences.xml?

Edit
Report

1 Answer

14

Try overriding startActivity() in your PreferencesActivity class and make it call startActivityForResult() instead after checking that the intent is the one we are interested in, similar to the following (with 1234 as the request code):

public class PreferencesActivity {
    // ...

    @Override
    public void startActivity(Intent intent) {
        if (thisIsTheExpected(intent)) {
            super.startActivityForResult(intent, 1234);
        } else {
            super.startActivity(intent);
        }
    }

    @Override
    protected void onActivityResult(int reqCode, int resCode, Intent data) {
        if (reqCode == 1234) {
            // should be getting called now
        }
    }

    // ...
}

Depending on your needs, this could be simpler compared to adding several OnPreferenceClickListener in your code :)

answered 2013-01-01T18:01:55.450

Your Answer