Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6898439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:17:47+00:00 2026-05-27T07:17:47+00:00

import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.preference.PreferenceActivity; public class Preferences extends

  • 0
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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:17:48+00:00Added an answer on May 27, 2026 at 7:17 am

    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.

    Preference pref = new Preference(this);
    pref.setIntent(new Intent().setClass(this, <Activity>.class));
    

    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”.

    public class ListPrefTestActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);
    
    
            SharedPreferences SP = PreferenceManager
                    .getDefaultSharedPreferences(getBaseContext());
    
    
            // Register OnChangeListener
            SP.registerOnSharedPreferenceChangeListener(this);
        }
    
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
                String key) {
    
            //Make sure the item changed was the list_preference 
            if(key.equals("list_preference")) {
                //Get the value from the list_preference with default: "Nothing"
                String value = sharedPreferences.getString(key, "Nothing");
    
                // If the value not the default, then open google.com using intent.
                if(!value.equals("Nothing")) {
                    String url = "http://www.google.com";
                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(url));
                    startActivity(i);
                }       
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have this class here: package phil.droid.game; import android.content.Intent; import android.content.res.Resources; import android.os.Bundle;
package com.ebonybutler.cexample3; import android.app.Activity; import android.content.Intent; import android.content.pm.ActivityInfo; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener;
package com.example.Calc; import android.R.string; import android.app.ListActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter;
I have created following activity package com.ali.test; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import
This is what I have: import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import
intentTest.java: package intentTest.xyz.com; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public
VitaminTable.java: package vitaminTable.xyz.com; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import
My activity is below import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
package com.duncan.hello.world; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button;
Here's my code for DroidArmoryActivity package com.maxgenero.droidarmory; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.