开发者

Android - sharedPreference are not working as it should?

开发者 https://www.devze.com 2023-03-02 08:49 出处:网络
I have an ArrayList of string in my activity andi want to store it as onPause in SharedPreference and retrieve it in my onCreate() but i am unable to do that.

I have an ArrayList of string in my activity and i want to store it as onPause in SharedPreference and retrieve it in my onCreate() but i am unable to do that.

here is my code -:

public static ArrayList<String> ignoredTasks;
public static final String PREF_FILE_NAME = "savedPreferences";

@Override
public void onCreate(Bundle savedInstanceState)
{
    ignoredTasks =  loadBackStates();
}
@Override
public void onPause()
{
    super.onPause();
    Log.e(TAG, "I am pause");
    saveState();
}
@Override
public void onStop()
{
    super.onStop();
    Log.e(TAG, "I am stop");
    saveState();
}
@Override
public void onDestroy()
{
    super.onDestroy();
    Log.e(TAG, "i am destroy");
    saveState();
}

public void saveState()
{
    SharedPreferences spr = getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = spr.edit();
    int size = ignoredTasks.size();
    editor.putInt("size", size);
    for(int k = 0; k < size-1; k++)
    {
        editor.putString(k+"4", ignoredTasks.get(k));
        editor.commit();
    }

    //editor.commit();
}
public ArrayList<String> loadBackStates()
{
     ArrayList<String> ignoredTask = new ArrayList<String>();
    SharedPreferences sp = getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
    int size = sp.getInt("size", 0);
    for(int k = 0; k < size-1; k++)
    {
        ignoredTask.add(k, sp.getString(k + "4", " "));
    }
    return ignoredTask;

}


public void createMenuDialog(final int i, Drawable icon, final String taskName, final String pkgName)
{
    final AlertDialog.Builder menuDialog = new AlertDialog.Builder(this);
    menuDialog.setIcon(icon);
     menuDialog.setTitle(taskName);
menuDialog.setItems(menuItems, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    /* User clicked so do some stuff */
                    String[] items = menuItems;

                    if(items[which].equals("Ignore"))
                    {
                        HashMap<String,String> newItem = new HashMap<String,String>();
                        newItem.put("Process", pkgName);
                        newItem.put("Name", taskName);
                        ignored.add(newItem);
                        ignoredTasks.add(pkgName);
                    }   开发者_Go百科                                            
                }
            });
     menuDialog.show();
}

The problem is when i exit my app using back key and then if i restart it again, i could see the sharedpreference but when i force close my app then its gone. what's the issue?

Please Help!!

Thanks


Try with getSharedPreferences instead of getPreferences

Edit: I have read the logcat lines you have posted in a comment. Android is sending to your app a SIGKILL signal when you force close so I think you can't do anything. Read this. http://en.wikipedia.org/wiki/SIGKILL


try this one..

SharedPreferences spr =  activity.getSharedPreferences("SPR", Context.MODE_PRIVATE);

try this - maybe it will help..

for(int k = 0; k < size-1; k++)
{
    editor.putString(k+"4", ignoredTasks.get(k));

}
editor.commit();


Force close terminates your app instantly, without any other code being called. This is not weird, this is how things work.

It sounds like you've logged that onPause() gets called when you press Back. You should also check that onPause() is called when moving into another activity or app. For instance, long-press the Home button and select another app, you should see your onPause() log.

That should be enough for your purposes. Why do you think you need onPause() to get called after a force close (which is impossible)?

0

精彩评论

暂无评论...
验证码 换一张
取 消