0

I have an issue with singleton in an app created in android studio.

Class MyApplication is created :


    public class MyApplication extends Application {  // code - freeCodeCamp.org 
    
        private static MyApplication singleton;
        private List<Location> myLocations;
    
    
        public List<Location> getMyLocations() {
            return myLocations;
        }
    
        public void setMyLocations(List<Location> myLocations) {
            this.myLocations = myLocations;
        }
    
        public MyApplication getInstance(){
            return singleton;
        }
    
        public void OnCreate() {
            super.onCreate();
            singleton = this;
            myLocations = new ArrayList<>();
        }
    }

And when i call MyApplication in Main aactivity it gives an error , specific null:

    MyApplication myApplication = (MyApplication)getApplicationContext();
    savedLocations = myApplication.getMyLocations();
    savedLocations.add(currentLocation);

currentLocation has a valid Location value. Any hints? thanks

CanonSAS
  • 3
  • 3
  • 1
    Does this answer your question? [Singleton in Android](https://stackoverflow.com/questions/16517702/singleton-in-android) – BroscR Oct 15 '21 at 07:31
  • this is most likely a horrible pattern to be following regardless, you very rarely need to be saving and using data like this in modern android development – a_local_nobody Oct 15 '21 at 07:32
  • thanks for the feedback, how do you propose to tackle this? this class was created to have a unique list of locations. – CanonSAS Oct 15 '21 at 07:38
  • using a single activity with fragments means that you can save this in the activity and have access to it in all your fragments, you could save it in a shared view model, you could even consider passing the data around to where you need it, quite a variety of options available. to burden your application class with this just becomes messy, it's an easy escape to just save everything you need there, without thinking of alternatives – a_local_nobody Oct 15 '21 at 07:51
  • thanks, i was wondering the same why it was used this solution. i m following a tutorial on how to build a gps app and the code is same as in tutorial but i have the error above. The only thing that is working is to add when list is declared private List myLocations = new ArrayList<>(); because onCreate is never used. I'm pretty sure i'm missing something and is not @override oncreate. – CanonSAS Oct 15 '21 at 09:43

2 Answers2

2

myLocations is null cause you misspelled the onCreate method signature and it's actually never called.

You should change this:

public void OnCreate() {

to this:

@Override
public void onCreate() {

That's the solution to your problem but honestly you shouldn't do it that way. Please read into application architectures and learn how to define scope of your data. Having a singleton array in your android application class is in 99.99% cases the wrong answer.

Ernest Zamelczyk
  • 2,562
  • 2
  • 18
  • 32
0

You must use @Override public void onCreate() like Ernest said, and write this code to get the application instance and using it.

import MyApplication;

public OtherClass {

    public void otherMethod() {
        MyApplication myApp = MyApplication.getInstance();
        savedLocations = myApp.getMyLocations();
        savedLocations.add(currentLocation);
    }

}
hardyx
  • 31
  • 4