Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4570164
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:19:22+00:00 2026-05-21T19:19:22+00:00

I have an ArrayList of string in my activity and i want to store

  • 0

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);
                    }                                               
                }
            });
     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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T19:19:23+00:00Added an answer on May 21, 2026 at 7:19 pm

    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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have arraylist myorderdata . I want to retrieve this arraylist to one String
I have a method that returns an IEnumerable<KeyValuePair<string, ArrayList>> , but some of the
I have an ArrayList of strings, and I want to randomly change a string's
I have this code: FVDTO.setStatus(fail); List<String[]> invalidFields = new ArrayList<String[]>(); Iterator<ConstraintViolation<HazardSubmission>> iterator = cv.iterator();
I want to pass a ArrayList from one activity to other. I have found
My main activity code: // here you put all your data. String[] dataArray =
How can I retrieve one activity arraylist into another activity and store,update and clear?
I want to save images in android using SharedPreference. I have two activity classes,
I have an activity which has an array list ArrayList<String> array = new ArrayList<String>();
i have my listview activity that works pretty fine but i have an annoying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.