应用程序销毁时,SharedPreferences会丢失数据

时间:2016-06-02 09:56:30

标签: java android save

所以我试图用onPaus()方法在sharePreferences中保存数据,我在onCreate()尝试过同样的事情,但是没有用,这是代码:

public class Favorite extends Activity implements ListView.OnItemClickListener
{
    private static final String PREFS_FILE = "com.example.stewiesh.eduguide.preferences1";
    private static final String KEY_FAVORITE = "Key_favorite";
    static   ArrayList<String> uniID = new ArrayList<String>();
   /** SharedPreferences preferences;
    SharedPreferences.Editor editor; **/
    ArrayAdapter<String> adapter;
    ListView list;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.favorite);
        list = (ListView) findViewById(R.id.favoriteList);
        adapter = new ArrayAdapter<String>(this, R.layout.list_layout);
        list.setOnItemClickListener(this);
        list.setAdapter(adapter);
     //   preferences = getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE);
     //   editor = preferences.edit();
    //    setData();
        runOnUiThread(new Runnable() {
            public void run() {
                System.out.println("i am out");
                SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                adapter.add(settings.getString(KEY_FAVORITE,"none of them is here bitch"));
                adapter.notifyDataSetChanged();
            }
        });;
    }
/**
    public void setData()
    {
        for (int i = 0; i < Page2.responses.length; i++)
        {
            for(int j=0;j<uniID.size();j++)
            {
             if(Page2.responses[i].getPrograma().toString().equals(uniID.get(j)))
             {
                 editor.clear();
                 editor.putString(KEY_FAVORITE,Page2.responses[i].getFakulteti()+" -- "+Page2.responses[i].getUniversiteti()+" "+Page2.responses[i].getPrograma().toString());
             //    editor.commit();
               //  editor.apply();
             }
            }
        }
    }
**/
    @Override
    protected void onPause() {
        super.onPause();
     //   preferences = getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE);
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = preferences.edit();
        for (int i = 0; i < Page2.responses.length; i++)
        {
            for(int j=0;j<uniID.size();j++)
            {
                if(Page2.responses[i].getPrograma().toString().equals(uniID.get(j)))
                {
                    editor.clear();
                    editor.putString(KEY_FAVORITE,Page2.responses[i].getFakulteti()+" -- "+Page2.responses[i].getUniversiteti()+" "+Page2.responses[i].getPrograma().toString());
                    editor.commit();
                }
            }
        }
       //editor.apply();
    }

    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {

    }
}

当我销毁应用程序时,除了启动它之外,它什么都没给我,我无法理解它为什么不保存数据,我尝试了各种各样的东西但是......

1 个答案:

答案 0 :(得分:1)

首先你应该删除

runOnUiThread(new Runnable() 
{
    public void run() {
});

这部分来自你的代码:

runOnUiThread(new Runnable() {
    public void run() {
        System.out.println("i am out");
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        adapter.add(settings.getString(KEY_FAVORITE,"none of them is here bitch"));
        adapter.notifyDataSetChanged();
    }
});

因为你已经在UI线程中运行了那部分代码,所以再次添加runOnUiThread是没有意义的。

你无法保存任何东西的原因是因为你打电话 循环中editor.clear()

根据文件:

  

clear() - 在编辑器中标记以删除首选项中的所有值。

您需要做的只是put和commit(),无需调用editor.apply()

        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                SharedPreferences.Editor editor = preferences.edit();
                for (int i = 0; i < Page2.responses.length; i++)
                {
                    for(int j=0;j<uniID.size();j++)
                    {
                        if(Page2.responses[i].getPrograma().toString().equals(uniID.get(j)))
                        {
             editor.putString(KEY_FAVORITE,Page2.responses[i].getFakulteti()+" -- "+Page2.responses[i].getUniversiteti()+" "+Page2.responses[i].getPrograma().toString());
                        }
                    }
                }

                editor.commit();

您不需要继续提交,只有在完成数据传输后才会提交。