0

I have an app up and running in Play store,i have an issue where android OS kills my app.

Scenario/Steps :

1) Open app, move to any screen.
2) Minimise the app by clicking HOME button.
3) Open several other apps.(say 14 to 15 apps).
4) And now launch my app it CRASHES!!!!.

What i have noticed is instead of app starting from Splash screen it starts from where it was left of and since all my data is lost it gives me NULL POINTER EXCEPTIONS.

Ideally my app should start from the Splash Screen since i am loading all my data there and passing it other activities.

How do i check if my app is killed and load from splash screen ?

I have Application class extended as well but i am not sure how to use that.

user2056563
  • 600
  • 2
  • 12
  • 37
  • The OS can destroy your app anytime it needs memory, your saying after it kills your app when you click the launcher again, it does not go to your splash screen ? – j2emanue Dec 02 '15 at 16:29
  • @j2emanue thats correct but in that case i want my app to start from splash screen. – user2056563 Dec 02 '15 at 16:30
  • that's how ends using static variables as store on android – Selvin Dec 02 '15 at 16:31
  • @Selvin so how can i solve this ? can you help me ? – user2056563 Dec 02 '15 at 16:31
  • *i want my app to start from splash screen.* obvious solution: do a null check and start splash screen by yourself – Selvin Dec 02 '15 at 16:32
  • @Selvin i have several activities and i cant do it for each and every activity – user2056563 Dec 02 '15 at 16:32
  • Does your splash screen start on every launch or just the first time ? – j2emanue Dec 02 '15 at 16:33
  • @j2emanue first time only, example if the app is installed from store it launches with splash screen and if minimized it just goes back where he was left off, Splash screen is my LAUNCHER activity. – user2056563 Dec 02 '15 at 16:35
  • ok so you have a shared preference so that the splash screen is only EVER shown once ? lets say i open your app for the first time and then i reboot the phone and open your app again, will the splash screen be shown a second time ? – j2emanue Dec 02 '15 at 16:36
  • @j2emanue NO as i said Splash Screen is my Launcher – user2056563 Dec 02 '15 at 16:37
  • 2
    he has somthing like `class IShouldReadTheGuidesBecauseImNoobStorage { public static Object value; }` then in splash he has: `class Splash extends Activity { onCreate() { IShouldReadTheGuidesBecauseImNoobStorage.value = loadValueAndStartNewActivity();}}` then in NewActivity he is using `IShouldReadTheGuidesBecauseImNoobStorage.value` without null check ... – Selvin Dec 02 '15 at 16:41
  • of course the solution is `if(IShouldReadTheGuidesBecauseImNoobStorage.value == null) { startSplashActivity(); finish();} else { use(IShouldReadTheGuidesBecauseImNoobStorage.value)}` in NewActivty – Selvin Dec 02 '15 at 16:43
  • @Selvin it not about adding NULL check, i hope you have not understood the issue. – user2056563 Dec 02 '15 at 16:49
  • lol ... I hope that there will come such day that you will understand android's application lifecycle ... and yes, it is all about null check ... obviously if process is killed(because OS need memory for other apps) static variables are wiped out ... if you return to the last activity, it is recreated from some state ... but android system doesn't recreate those static values – Selvin Dec 02 '15 at 16:52
  • @Selvin thanks can we have a chat so that i can explain you the scenario please? – user2056563 Dec 02 '15 at 17:59
  • http://developer.android.com/guide/components/tasks-and-back-stack.html – Selvin Dec 02 '15 at 18:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/96824/discussion-between-user2056563-and-selvin). – user2056563 Dec 02 '15 at 18:08
  • Have no time for chat, also I'm on mobile... Check the very last paragraph of this guide... It should help – Selvin Dec 02 '15 at 18:11
  • @Selvin ok thanks, i guess you are pointing to finishOnTaskLaunch, but if thats set to all my remaining activities,then will it launch my Splash screen every time ? if session is killed – user2056563 Dec 02 '15 at 18:13
  • How did you fixed it? @user2056563 – Sudheesh Mohan Aug 24 '17 at 04:34

1 Answers1

0

I think you might be using static variable(s) , just a hunch. Anyway can you try this in your mainActivity onCreate or onResume:

if (isTaskRoot()) {
    // This activity is at root of task, so launch main splash screen 
} else { 
    // This activity isn't at root of task, so continue
} 

But thats getting away from the real problem i think: instead i'd look into onSaveInstanceState in Activity class:

@Override 
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current  state 
    savedInstanceState.putString("myStaticVariable", sStaticVariableAreBad);
    // Always call the superclass so it can save the view hierarchy state 
    super.onSaveInstanceState(savedInstanceState);
} 

then in onCreate check if the activity was previously destroyed:

// Check whether we're recreating a previously destroyed instance 
if (savedInstanceState != null) {
    // Restore value of members from saved state 
    sStaticVariableAreBad = savedInstanceState.getString("myStaticVariable");

}
j2emanue
  • 60,549
  • 65
  • 286
  • 456