0

One customer has sent me feedback about my app force closing on startup after he has killed the app with a Task Killer. I've told him task killers are dangerous and do not do anything good to the phone performance since Android does not manage RAM as Windows, but he won't uninstall the Task Killer.

The Force Close goes like this:

A) User uses my app normally

B) User task kills my app

C) User launches my app again

D) Force close on startup

E) User launches my app yet again

F) Everything works as intended

I've found the problem is that my static objects become null because they are cleared from RAM, leading to NullPointerExceptions that Force Close the app. However, I have a lot of places where I'm accessing those static objects (if this wasn't the case I wouldn't even use static objects) so the NullPointerExceptions can happen in a lot of places.

This behaviour only happens in Android 2.3.7 or lower. Android 4.0's own task killer (the one you can access by long-pressing Home) kills the app correctly and then launches it without any force closings.

How can I circumvent this behaviour?

Charlie-Blake
  • 10,832
  • 13
  • 55
  • 90
  • put your app log and startup code. – Dhaval Parmar Feb 01 '13 at 10:50
  • He hasn't submitted the log, but basically the app tries to launch where it was suspended, and whenever it tries to get a static object it NPE's and force closes. The major problem is that the exception is never caught at the same place: It throws in the last Activity you visited, making exception handling a lot more troublesome. – Charlie-Blake Feb 01 '13 at 10:59
  • you have your app code? try to do same step at your place get logs by Log.e or Log.d then post it here. also put some code otherwise no one can help you. – Dhaval Parmar Feb 01 '13 at 12:07

1 Answers1

0

I solved it.

The solution is not optimal but it works:

I created an interface like this:

public interface StaticAccessTo<T>{
    public void returnStatic(T object);
}

Instead of having a getter for my static objects, I have this:

public void getWhatever(StaticAccessTo<Whatever> callback){
    if (whatever == null){
        loadWhateverFromServer(callback);
    } else {
        callback.returnStatic(whatever);
    }
}

private void loadWhateverFromServer(StaticAccessTo<Whatever> callback){
    new AsyncTask<Void,Void,Void>(){

    public Void doInBackground(Void... params){
        whatever = loadWhatever();
        return null;
    }

    public void onPostExecute(Void result){
        callback.returnStatic(whatever);
    }

    }.execute();
}

So, if whatever is not null, it will call returnStatic(whatever) immediately and will have access to whatever in the main thread. Else, it will automatically load it from network and call returnStatic(whatever) when it's loaded, from the main thread.

Charlie-Blake
  • 10,832
  • 13
  • 55
  • 90