import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.preference.PreferenceActivity;
public class Preferences extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
SharedPreferences SP = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
String listPref = SP.getString("listPref", "No City Selected");
SP.getString("listPref", "No City Selected");
SP.contains(listPref);
if (listPref == "Aplpharetta") {
Intent myIntent = new Intent(Preferences.this, Contents.class);
Preferences.this.startActivity(myIntent);
} else if ("Greenville".equals("Greenvile")) {
Intent myIntent = new Intent(Preferences.this, Contents.class);
Preferences.this.startActivity(myIntent);
} else if ("Houston".equals("Houston")) {
Intent myIntent = new Intent(Preferences.this, Contents.class);
Preferences.this.startActivity(myIntent);
}}}
Here is the Arrays file
<string-array name="listArray">
<item>Alpharetta</item>
<item>Greenville</item>
<item>Houston</item>
<item>Tampa</item>
</string-array>
<string-array name="listValues">
<item>Alpharetta</item>
<item>Greenville</item>
<item>Houston</item>
<item>Tampa</item>
</string-array>
</resources>
The problem I am having is making the activity start when a listpreference is selected. I have tried the two ways listed above, the list lets you select the item, but not trigger the other activity to start. The arrays list is set up without integers due to the information being populated in another part of the app. I have tried setOnPreferenceChangeListener() and setOnPreferenceClickListener() . With using those, the preference screen would flash up for a send and return right back to the previous screen like it is starting the activity, but doesn’t stay open long enough if you need to make a change. This is my first attempt at making a preference screen, so I am not sure if I am missing something or going about this the entirely wrong way.
Have a look in the APIDemos for the API version that you a re using. There is a Preference Activity example with good explanations. 2 files you want to have a look at are: PreferencesFromCode.java and PreferencesFromXML.java in com.example.android.apis.app.
In short, you need to register intent for the particular preference you want.
This way when preference is clicked it will automatically fire the Activity you have registered for it.
UPDATE:
I have quickly created the activity and tested code below. You have said that you have tried the OnChangeListener, but for some reason it didn’t work out. This works using OnSharedPreferenceChangeListener and it launches the intent when you change the value of the “list_preference”.