PreferenceActivity not displayed (Blank Screen) - android

I am following these instructions to create a PreferenceActivity but the PereferenceActivity is not displayed. Activity starts but the Layout is blank.
I am also using material design NavDrawer in my Application by referring to developer.android.com but i hope it doesn't affect
I am testing application on device running Kitkat
Here is what happens when i launch PreferenceActivity "screenshot"
Logcat:
Could not find method android.preference.PreferenceActivity.onCreate
VFY: unable to resolve virtual method 950: Landroid/preference/PreferenceActivity;.onCreate(Landroid/os/Bundle;Landroid/os/PersistableBundle;)V
Code:
app_preference.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Settings">
<ListPreference
android:title="Timer"
android:summary="#string/timer_summary"
android:key="prefTime"
android:defaultValue="30"
android:entries="#array/prefTime"
android:entryValues="#array/prefTimeValue"/>
<SwitchPreference
android:title="Shuffle"
android:summary="#string/shuffle_summary"
android:defaultValue="false"
android:key="prefToggleShuffle"/>
<SwitchPreference
android:title="Allow over Wifi-only"
android:summary="#string/wifi_only_summary"
android:defaultValue="false"
android:key="prefWifiOnly"/>
<Preference
android:title="Restore Defaults"
android:summary="#string/restore_summ"/>
<Preference
android:title="Clear Data"
android:summary="#string/shuffle_summary"/>
</PreferenceCategory>
Settings.java
public class Settings extends PreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
getActionBar().setDisplayHomeAsUpEnabled(true);
getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();
}
public static class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.app_preference);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
break;
}
return super.onOptionsItemSelected(item);
}
}
Mainfest.xml
.....
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21"/>
<activity android:name=".preferences.Settings>
<intent-filter>
<category android:name="android.intent.category.PREFERENCE"/>
</intent-filter>
<meta-data android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.MainActivity"/>
</activity>
.....
Calling Intent
case R.id.action_setting:
Intent intent = new Intent(this, Settings.class);
startActivityForResult(intent, RESULT_SETTINGS);//RESULT_SETTINGS = 1
break;

Delete the second parameter in onCreate method:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Update for Aviel Fedida:
According with this, void onCreate (Bundle savedInstanceState,
PersistableBundle persistentState) is called when the activity is being re-initialized.
So when you start a new Activity with startActivityForResult or startActivity that method is not called, and the code inside is not executed.
Black screen appears becouse addPreferencesFromResource(R.xml.app_preference); is "unreachable code"

Use protected void onCreate(Bundle savedInstanceState)instead of public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState)

I had the same problem and I solved it by adding the line:
setContentView(R.layout.activity_settings);
Here I mean to add the layout file associated to your SettingsActivity,
inside the OnCreate method of the Settings class:

[RE-EDITED]
The main issue seems to be that you are using a PreferenceActivity with a PreferenceFragment, and you are assuming that a PreferenceActivity has things like action bars that you can directly access. This is probably a bad assumption.
A PreferenceFragment is actually meant to be used with a "regular" Activity, not a PreferenceActivity.
So, for instance, if you really want to access the action bar, you should use an ActionBarActivity instead. See also the documentation in the API Guide.

Related

android.preference.Preference.setOnPreferenceClickListener() does not work

I want to set an intent from my SettingsFragment (extends PreferenceFragment) to another activity within my app. I tried using the following code at first: (I'm using android.preference.Preference)
preferences.xml (the root preferences)
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
...
<PreferenceCategory android:title="About">
<Preference
android:key="about_here"
android:title="About us"
android:enabled="true" />
</PreferenceCategory>
</PreferenceScreen>
Then, in my SettingsFragment.java:
public class SettingsFragment extends PreferenceFragment {
public static final String goToAbout = "about_here";
private ViewGroup parent;
private View currView;
#Override
public void onCreate(#Nullable Bundle SavedInstanceState) {
super.onCreate(SavedInstanceState);
addPreferencesFromResource(R.xml.preferences);
Preference preferencex = findPreference(goToAbout);
preferencex.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
Intent i = new Intent(getActivity(), AboutActivity.class);
i.putExtra("Class", "Settings");
preference.setIntent(i);
Log.e("Preference", "Clicked");
getActivity().startActivity(i);
return true;
}
});
}
...
}
However, the onPreferenceClick code is not invoked at all (I know from the Log.e() that it does not happen). I know that many have asked this question before, but even implementing their solutions does not help. Is there anything I'm missing?
Also, I don't mind implementing an Intent from within preferences.xml, but I tried that and it returns an error.

Apply style setting in Activity immediately

I have an Activity containing a PreferenceFragment. The user can change the app's style (light theme or dark theme) in this Activity. Now I want the change being visible immediately. That means, I want the style of the Activity changing when the user changes the style setting in the PreferenceFragment.
My idea was to listen for the preference change with OnSharedPreferenceChangeListener in the PreferenceFragment and recreate the Activity.
But I do not think this is the right way. So, what is best practice here? Thanks in advance.
UPDATE
Activity:
public class SettingsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loadSettings();
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}
private void loadSettings() {
SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(this);
boolean darkTheme =
sharedPreferences.getBoolean(getText(R.string.pref_theme_key).toString(), false);
if (darkTheme) {
setTheme(R.style.DarkTheme);
}
}
}
PreferenceFragment:
public class SettingsFragment extends PreferenceFragment
implements SharedPreferences.OnSharedPreferenceChangeListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals(getText(R.string.pref_theme_key))) {
// recreate Activity
}
}
}
Preferences:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:defaultValue="false"
android:key="#string/pref_theme_key"
android:title="#string/pref_theme" />
</PreferenceScreen>
I think your approach is correct. Listen for that preference change and then recreate your activity.
Editing for more context:
You should be saving this as a light/dark variant in your preferences and then when your activity is created call setTheme based on this before calling setContentView.

Add preferences deprecated

I just noticed that addPreferencesFromResource(); is deprecated in Android now. I tried looking for what I should use now and the developers site still has it this way. Does anyone know what I should use instead?
As I pointed out in my comment, just use a Preference fragment
PreferenceActivity is still fine, but you have to use a PreferenceFragment subclass to do it now. It looks something like this:
public class SetupActivity extends PreferenceActivity {
#Override
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
}
public static class MyPreferenceFragment extends PreferenceFragment
{
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
PreferenceManager.setDefaultValues(getActivity(), R.xml.preferences, false);
addPreferencesFromResource(R.xml.preferences);
}
}
You then store the preferences in preferences.xml like this:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference
android:key="rest_server"
android:defaultValue="#string/default_rest_server"
android:summary="#string/rest_url_desc"
android:title="#string/rest_url_title" />
<EditTextPreference
android:key="base_url"
android:defaultValue="#string/default_base_url"
android:summary="#string/base_url_desc"
android:title="#string/base_url_title" />
</PreferenceScreen>

Preference Screen option is not showing

My Preference Screen option is not showing, it shows-app. has stopped unexpectedly..
This is my Preference.java-
public class Prefs extends PreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.id.settings);
}
}
This is the settings for Preference-
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference
android:key="music"
android:title="#string/music_title"
android:summary="#string/music_summary"
android:defaultValue="true"/>
<CheckBoxPreference
android:defaultValue="true"
android:summary="#string/hints_summary"
android:title="#string/hints_title"
android:key="hints"/>
</PreferenceScreen>
This is the Item Select event-
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.settings)
{
startActivity(new Intent(this,Prefs.class));
return true;
}
return false;
}
And the activity is registered well in manifest file.
<activity
android:name=".Prefs"
android:label="#string/settings_title" >
</activity>
addPreferencesFromResource()
method should load XML file containing the preferences.
Therefore, in you code replace
addPreferencesFromResource(R.id.settings)
with
addPreferencesFromResource(R.xml.yourPreferenceSettingsFileHere)
That will solve your problem.
please replace addPreferencesFromResource(R.id.settings); with addPreferencesFromResource(R.layout.settings);
where settings is the preference xml.
and instead of using onOptionsItemSelected()
use
onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
final String key = preference.getKey();
if(key.equal("music"){
/ur implementaion
}
}**

PreferenceActivity causes Force close

I have a bit of an issue that I am not sure how to fix. I have an options menu which has code like this
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.main_menu_settings:
startActivity(new Intent(MainMenuActivity.this, BackofficePreferencesActivity.class));
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
No error happens if I comment out the line
startActivity(new Intent(MainMenuActivity.this, BackofficePreferencesActivity.class));
My preferences activity looks like this
public class BackofficePreferencesActivity extends PreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.preferences);
}
}
and my layout
<PreferenceCategory android:title="System Configuration">
<ListPreference
android:title="Environment"
android:summary="Select the environment"
android:key="#string/pref_current_environment"
android:defaultValue="Production"
android:entries="#array/environment_list"
android:entryValues="#array/environment_list"
android:dialogTitle="Select Environment" />
</PreferenceCategory>
I even tried removing the list preference to see if the screen would load empty, but it still errors. In eclipse, usually I can see what went wrong by looking in the LogCat tab, but for some reason nothing is being logged there anymore. I tried rebooting my AVD and that hasn't helped.
You need to declare the Activty in your manifest file.
Sample XML code:
<activity android:label="#string/sample"
android:name=".Sample"
android:icon="#drawable/sample">
</activity>

Categories

Resources