关闭

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

823人阅读 评论(0) 收藏 举报


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>

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2121次
    • 积分:67
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:5篇
    • 译文:0篇
    • 评论:0条
    文章分类