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 4615866
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:54:03+00:00 2026-05-22T01:54:03+00:00

In my app, I’m trying to save an ArrayList of Strings. I have an

  • 0

In my app, I’m trying to save an ArrayList of Strings. I have an Activity Favourites, which has an add() method. This method is called in another Activity to add something to the ArrayList. I want that each time something is added, it first gets all the values that I’ve stored in SharedPreferences, checks if the value isn’t already there and if it’s not, adds it to the ArrayList. Finally I want it to store all the values again in SharedPreferences. When the Activity Favourites is called, it shows a list with all the favourites (Gets each value in database and all that).

So I’ve made a method, but as onCreate() is never called when an item is added to the ArrayList, I can’t seem to instantiate my SharedPreferences. Is there anyone who can give me some hints to get this to work?

Note: I don’t want to work with SQLite database, because it would be for only one class in my app, and it would contain very little values.

Also, the code I used:

public void add(String v, String k){
    prefs = PreferenceManager.getDefaultSharedPreferences(this);
    int countvaca = prefs.getInt("vacacount", 0);
    int countkantoor = prefs.getInt("kantoorcount", 0);

    for(int i = 0; i <countvaca; i++){
        vaca.add(prefs.getString("Vaca"+i, null));
    }
    for(int j = 0; j <countkantoor; j++){
        kantoor.add(prefs.getString("Kantoor"+j, null));
    }

    boolean blnFound = vaca.contains(v);
    System.out.println("Does arrayList contain vacature ? " + blnFound);
    if(!blnFound){
        vaca.add(v);
        kantoor.add(k);
    }

    Editor edit = prefs.edit();
    countvaca = vaca.size();
    countkantoor = kantoor.size();
    edit.putInt("Vacaturelijstgrootte", countvaca);
    edit.putInt("Kantoorlijstgrootte", countkantoor);
    for(int a = 0; a<countvaca; a++){
         edit.putString("Vaca"+a, vaca.get(a));
    }
    for(int b = 0; b< countkantoor; b++){
        edit.putString("Kantoor"+b, kantoor.get(b));
    }
    edit.commit();


}
  • 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-22T01:54:04+00:00Added an answer on May 22, 2026 at 1:54 am

    If onCreate never gets called then you activity is never started, so the “this” in

    prefs = PreferenceManager.getDefaultSharedPreferences(this);
    

    doesn’t refer to a properly initialized activity.

    Either you should start your activity that contains your “add” method (which automatically calls the onCreate method), or you can pass a reference to the currently active Activity to your add method, i.e. something like this:

    public void add(String v, String k, Activity a){
         prefs = PreferenceManager.getDefaultSharedPreferences(a);
    

    However, it is not considered good programming style to directly call methods from one activity to another.
    Best is to create a separate class that contains the code that the two activities have in common.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

App A has this BroadcastReceiver in its manifest (within <application>): And this receiver: public
Cocoa app, SnowLeopard I have a WebView in to which I'm loading HTML (essentially
My app has many controls on its surface, and more are added dynamically at
My app uses a WebRequest at certain points to get pages from itself. This
My app has a DataGridView object and a List of type MousePos. MousePos is
App_1 has a view and a template for this view. It would like to
App Description: I have a UIWebview and a Toolbar beneath it. A button on
App.config: <add key=SaveDraftPath value=C:\Drafts\/> C#: var saveDraftPath = ConfigurationManager.AppSettings[SaveDraftPath]; var sfDialog = new SaveFileDialog();
App has 4 view controllers; Menu, A, B and C, and a singleton class
App.config: <?xml version=1.0 encoding=utf-8 ?> <configuration> <appSettings> <add key=1 value=An error occured.\r\nPlease try again

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.