Android的PreferenceActivity中的getPreferenceScreen().removeAll()只删除组件



Android的PreferenceActivity中的getPreferenceScreen().removeAll()只删除组件

如题,我们在Android中创建一个MyPreferenceActivity继承PreferenceActivity,然后在onCreate()方法中addPreferencesFromResource(R.xml.mypref);代码如下:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getApplicationContext();
        addPreferencesFromResource(R.xml.mypref);
        manager = getPreferenceManager();
//        listPreference = (ListPreference) manager.findPreference("mylist");
        listPreference = (ListPreference) manager.findPreference(context.getString(R.string.list_key));
        listPreference.setSummary(listPreference.getEntry() + ":" + listPreference.getValue());
        Toast.makeText(getApplicationContext(), listPreference.getEntry() + "的值为:" + listPreference.getValue(), Toast.LENGTH_SHORT).show();
    }

如果调用getPreferenceScreen().removeAll(),则只删除了当中的组件,而没有删除数据。代码如下:

 @Override
    protected void onStop() {
        super.onStop();
        getPreferenceScreen().removeAll();
        listPreference = (ListPreference) manager.findPreference(context.getString(R.string.list_key));
        Toast.makeText(getApplicationContext(), listPreference + "的值1为:" + listPreference, Toast.LENGTH_SHORT).show();
        Log.e("Test", "onStop1 " + listPreference);
        addPreferencesFromResource(R.xml.mypref);
        listPreference = (ListPreference) manager.findPreference(context.getString(R.string.list_key));
        Toast.makeText(getApplicationContext(), listPreference.getEntry() + "的值2为:" + listPreference.getValue(), Toast.LENGTH_SHORT).show();
        Log.e("Test", "onStop2 " + listPreference.getEntry());

        Log.e("Test", "onStop clear");
    }

打印log如下:
07-09 17:56:00.448  19857-19857/jehu.com.mypreference E/Test﹕ Main onPause
07-09 17:56:00.959  19857-19857/jehu.com.mypreference E/Test﹕ Main onStop
07-09 17:57:22.856  19857-19857/jehu.com.mypreference E/Test﹕ onStop 1null
07-09 17:57:22.866  19857-19857/jehu.com.mypreference E/Test﹕ onStop2 C#
07-09 17:57:22.866  19857-19857/jehu.com.mypreference E/Test﹕ onStop clear
07-09 17:57:22.866  19857-19857/jehu.com.mypreference E/Test﹕ onDestroy


我们发现

07-09 17:57:22.856  19857-19857/jehu.com.mypreference E/Test﹕ onStop1 null
07-09 17:57:22.866  19857-19857/jehu.com.mypreference E/Test﹕ onStop2 C#

原因是删除组件后,第一次打印null,但是数据还在,则再次加载组件后,第二次打印则为持久化到本地的数据。

完整代码:

package jehu.com.mypreference;

import android.content.Context;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by admin on 2015/7/9.
 */
public class MyPreferenceActivity extends PreferenceActivity {
    private PreferenceManager manager;
    private ListPreference listPreference;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getApplicationContext();
        addPreferencesFromResource(R.xml.mypref);
        manager = getPreferenceManager();
//        listPreference = (ListPreference) manager.findPreference("mylist");
        listPreference = (ListPreference) manager.findPreference(context.getString(R.string.list_key));
        listPreference.setSummary(listPreference.getEntry() + ":" + listPreference.getValue());
        Toast.makeText(getApplicationContext(), listPreference.getEntry() + "的值为:" + listPreference.getValue(), Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
        getPreferenceScreen().removeAll();
        listPreference = (ListPreference) manager.findPreference(context.getString(R.string.list_key));
        Toast.makeText(getApplicationContext(), listPreference + "的值1为:" + listPreference, Toast.LENGTH_SHORT).show();
        Log.e("Test", "onStop1 " + listPreference);
        addPreferencesFromResource(R.xml.mypref);
        listPreference = (ListPreference) manager.findPreference(context.getString(R.string.list_key));
        Toast.makeText(getApplicationContext(), listPreference.getEntry() + "的值2为:" + listPreference.getValue(), Toast.LENGTH_SHORT).show();
        Log.e("Test", "onStop2 " + listPreference.getEntry());

        Log.e("Test", "onStop clear");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e("Test", "onDestroy");
    }
}


<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    android:key="123"
    xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
    android:key="@string/list_key"
    android:title="请选择一个选项"
    android:summary="选项"
    android:entries = "@array/entries"
    android:entryValues="@array/values" />

</PreferenceScreen>

阅读更多
上一篇Android Studio 简单介绍和使用问题小结
下一篇Volley使用(1)
想对作者说点什么? 我来说一句

android 设置界面 PreferenceScreen

2017年04月19日 19.9MB 下载

没有更多推荐了,返回首页

关闭
关闭