Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pressing home button and access app again from app icon call onCreate?

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?

like image 828
Irfan Ahmed Avatar asked Dec 05 '13 08:12

Irfan Ahmed


People also ask

Which method gets called when Home button pressed?

When you press the Home button on the device, the onPause method will be called. If the device thinks that it needs more memory it might call onStop to let your application shut down. The device can restart your application later from a stopped state. If your process killed, onDestroy will be called.

What happens when we press home button?

For example, if you pressed the Home key now, it'd scroll to the top of this page. Some users may assume that this key returns them to the home page, which it does not. If the text cursor is in a text box like the one above, pressing Home key moves to the beginning of the line.

Which callback is called when a button is tapped?

The callback method for the android:onClick attribute must be public , return void , and define a View as its only parameter (this is the View that was tapped). Use the method to perform a task or call other methods as a response to the Button tap.

Which method is not called when we press the Home button from an activity?

Note: onDestroy() method not call after press Home Button.


4 Answers

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.

like image 64
Martin Marconcini Avatar answered Nov 08 '22 09:11

Martin Marconcini


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.

like image 25
Talha Mir Avatar answered Nov 08 '22 09:11

Talha Mir


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.

like image 43
Baschi Avatar answered Nov 08 '22 10:11

Baschi


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

like image 1
Shashika Avatar answered Nov 08 '22 11:11

Shashika