6

I have an offline-online application, i found a strange issue in it, may be it is not, but i did'nt understand about it..
App requirement is that, if internet is available, even from starting app or from resuming, i call webservices and store data in sqlite, otherwise app stays in offline mode,
I have 2 activities, second activity contains an id, that i passes through intent (that point is important),
My Problem:
if i am in second activity, and internet is running, and i press home button , then this 2nd activity pauses, then stop which is a default behavior in android, i goto settings, turn wifi off, then press app icon again to get back in my app, here i got confused, i expect that my app now will be in onResume, but when i see in logcat its onCreated called and app crashes, nullPointerException occurs, because this 2nd activity does not have that id, i passed through intent..

Note:
If i use recent app button to go to "settings", then come back again after turing wifi off, and repeat all this behavior, then working fine, its onResumes called not oncreate..

My Question
Why it is going in onCreate while i my expectation is to be onResume while i came back from app icon?

Irfan Ahmed
  • 9,136
  • 8
  • 33
  • 54

4 Answers4

2

The NPE reason is clear, your second activity doesn't have the value and it crashes.

Why do you get different behavior then!?

It's because the launching intents are different. When you "task switch" Android is merely stopping your app but leaving it there (no guarantee) in case you want to switch back.

Going home (with home) is a clear indication that you want to leave the app, and although it will remain in memory and cached (as long as there is available memory), going back through the launcher (or App Icon as you call it) fires the LAUNCHER category (which goes to your Activity 1 first).

Take a look at this StackOverflow question (and answer) to better understand the consequences.

In any case, your problem is that your app must always be designed to resume in an inconsistent state and be able to recover. Android will kill your references, will destroy your variables and most likely send your app to hell overnight even if you have it running… if the phone goes on standby, chances are processes that aren't doing anything will be paused/stopped and likely killed.

Re-design your app so this is not a problem.

You say:

"I have 2 activities, second activity contains an id, that i passes through intent (that point is important),"

Well, why not make it easier and have ONE activity and TWO fragments? Then use Fragment Arguments to pass the value?

Or why not store the value in the preferences and/or a local database and recover it during onCreate?

And also why not make it so that if Activity 2 doesn't have a value, it calls Activity 1 and closes itself (better than a crash, huh?).

Etc.

As you can see there are multiple things you should consider. All in all, never trust that your app will be alive, because it won't.

Community
  • 1
  • 1
Martin Marconcini
  • 26,875
  • 19
  • 106
  • 144
  • wow..great answer and great solutions, i understand now, and used savedInstanceState to save and restore my id. Happy to see this elaboration, many thanx:) – Irfan Ahmed Dec 05 '13 at 09:32
  • Glad to see it helped. Please be careful with using savedInstanceState. That bundle is only intended to be used for **temporarily** saving the state during the lifecycle of an Activity or Fragment and not as a permanent storage solution. Please read this Stack Overflow thread to get an idea: http://stackoverflow.com/questions/151777/saving-activity-state-in-android you can see a very good explanation near the end written by Martin Belcher - Eigo – Martin Marconcini Dec 05 '13 at 19:56
1

You stated that you can see that the activitiy is stopped (onStop) if you go to the settings. That is the behaviour shown in the Android activity lifecycle. The counterpart for onStop is onCreate. So it does what the documentation tells us. Btw activities are paused if they are visible in some way and get stopped if they are not visible anymore. This would explain why your activity get paused. For further information read Managing the Activity Lifecycle. You can find a whole picture of the lifecycle here.

Baschi
  • 1,128
  • 11
  • 14
  • wait wait, i also stated that, if i switch between apps using recent app button from device, then also its on pause, onstop called, and when i switch back from same recent app button, this time my onResume called..it should be onCreate in case of your answer?isnt it? – Irfan Ahmed Dec 05 '13 at 08:11
  • The counterpart of ```onStop()``` is ```onStart()``` not ```onCreate()```. – Martin Marconcini Dec 05 '13 at 09:02
  • @MartínMarconcini for the described behaviour (the App process gets killed) the counterpart is the `onCreate()` method. Otherwise if the activity only stops you are right. Take a look at the lifecycle picture, there is a path to the left and to the right. ;) – Baschi Dec 05 '13 at 09:42
  • @Baschi well you're right that it can go either way, but only if it gets destroyed (which is happening and will eventually happen). – Martin Marconcini Dec 05 '13 at 20:00
1

This type of behaviour can be seen when you change some system configurations like font type,font size or language. But turning wifi on/off won't destroy the app and recreate it again. Check http://developer.android.com/guide/topics/manifest/activity-element.html#config for more information

Shashika
  • 1,151
  • 1
  • 9
  • 21
1

Once your activity's onStop gets called it's susceptible to be killed by the android system to collect resources for other apps which is what i think happened in your case.If it is killed, android will obviously call OnCreate when you get back to the activity.Check this for clarification. For experimenting you can try opening more than one apps from your recent apps and then return to your app. It may crash there too now.

Talha Mir
  • 1,228
  • 11
  • 19