-4

We are facing issue with Android application.There is one class named SingleTon which Extends Application and we are using it for State manager.when Application running in background and if I open push notification I am not able to access the Singleton class due o context is null, and that's why application is crashed.Same thing happens if application is on stand by mode. Here, I have mentioned my SingleTon class Code:

public class StateManager extends Application {
public String FirstName;

public String LastName;

private static StateManager instance;
public static synchronized StateManager getInstance() {
    return instance;
}       
public String getFirstName() {
    return FirstName;
}
public void setFirstName(String firstName) {
    FirstName = firstName;
}
public String getLastName() {
    return LastName;
}
public void setLastName(String lastName) {
    LastName = lastName;
}   
@Override
public void onCreate() {
    super.onCreate();
    instance = this;       
    Parse.enableLocalDatastore(this);
    Parse.initialize(this, "xxxx", "xxxxx");       
    ParseInstallation.getCurrentInstallation().saveInBackground();       
}

@Override
public void onTerminate() {
    instance =this;
    super.onTerminate();
}      
  }
Csharp
  • 177
  • 4
  • 15

2 Answers2

1

just place this line in onCreate of Application class

instance= new StateManager();

Reprator
  • 2,859
  • 2
  • 32
  • 55
0

May be your instance is not correct please try below code

public static StateManager getInstance(){
        if(instance== null){
            instance= new StateManager();
        }
        return instance;
    }
Gopi Krishna
  • 172
  • 1
  • 8
  • If I create new instance statemanager will null at that time.So I want access of context in main activity So i can access this statemanegr class in my Main activity. – Csharp Sep 29 '15 at 06:58
  • If StateManager instance coming null then you cannot access those methods it will throw an exception. After this change also are you getting same..? – Gopi Krishna Sep 29 '15 at 08:27