-2

I have developed an android app. It works fine but when the user has advanced app killer in his mobile and kills the app with it then when you open it again the app would crash. This is because the app killer is deleting all the contents of global variables of my app. Is there a way to stop this?

other work around could be like onResume() of my app i want to restart the app if it was killed by app killer. How to do this?

sundeep
  • 601
  • 1
  • 8
  • 21
  • 2
    This hardly makes any sense - when user kills the application, the whole process is killed, it's terminated. If it crashes in `onResume()` when restarted, it's simply a programming bug. Post your code, so we can see what is done wrong. – Code Painters Nov 30 '12 at 11:42
  • so this works fine on a tablet the problem comes with the mobiles. My app is in the list of programs which would not be killed by the same software in android4.0. Sorry couldn't post the code as it is very large. – sundeep Nov 30 '12 at 11:44
  • 2
    Your application can get killed anytime by the system, that's how Android works. If it fails to start afterwards, it's application's bug, sorry! – Code Painters Nov 30 '12 at 11:46
  • 2
    this is what task killers are supposed to do, you've to handle everything on your part. Also, see http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle if not already! – ThePCWizard Nov 30 '12 at 11:48
  • @CodePainters Yeah the application couldn't get started because it is coming back to onResume() and all the global variables are null. So my question is how do i handle it. – sundeep Nov 30 '12 at 11:50

2 Answers2

0

If your app is killed, when it starts next time it will start from scratch so onCreate will be called.

Now you claim your app is killed and then onResume is called. This doesn't make any sense. If onResume is called then probably your app was went to background so onPause must be called before. Check whether onPause is called and try to figure out what exactly happens.

Caner
  • 57,267
  • 35
  • 174
  • 180
0

You've to implement the callback onSaveInstanceState something like this -

protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);    
        outState.putInt("param1", yourGlobalIntVariableName);
        outState.putString("param2", yourGlobalStringVariableName);
    }

And in your onCreate you should see if your savedInstanceState variable is not null and in case it is not null, you've to take the saveInstanceState

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(savedInstanceState != null) {
            yourGlobalIntVariableName = savedInstanceState.getInt("param1");
            yourGlobalStringVariableName = savedInstanceState.getString("param2"));
        }
}

That should fix the problem.

If you have a Android 4.0 test device, go to Settings -> Developer Options -> "Do not keep activities"

Check it. And then run your app. See if it works fine. Ideally it should. OS can kill some of those activities in your back stack and your app should be able to handle it. This developer option will give you a way to test such cases. It removes the previous activity out of memory as soon as you go to the next activity.

Sudarshan Bhat
  • 3,772
  • 2
  • 26
  • 53