Here is the scene what i am facing..when i press home key it saves the current state of the current activity..so when my app comes in foreground, onResume() of that particular activity gets called who's state was saved. So if an app with say 10 activities are there then, we will need to write reload of app data in each activity's onResume()..is there a way to specify a reload of app data in one activity only..?
Jitesh,
Yes, this is possible. You will probably want to perform the reloading of application data in your Activity's onResume() function. See Android Activity Lifecycle.
Related
The detailed question is :
1.Whether the activity clicking order will get restore.In other words,if i click the back button again and again, whether the app's activity order will be normal.
2.what life cycle in activity will be triggered in last activity?Is it the same with the normal activity jump?
3.Was the state stored in physical memory(like sdcard)?How can i judge whether the important state was lost.
Taken from here:
Once an activity starts it goes to onCreate -> onStart -> onResume. If you minimize an activity (usually with the Home key) it will run onPause -> onStop and when android runs out of memory (because new app needs memory) then android will call onSaveInstance (where you save all the data you want) on your activity, then it will destroy it, calling onDestroy.
The next time the activity runs it will start again from onCreate and the rest, but it will pass a Bundle on the onCreate with the data you saved previously, for you to restore them. It will also run onRestoreState which gets the Bundle as well (you can use either one).
Basically, if onRestoreState runs, it means your app is restoring, or inside your onCreate check if the given Bundle is null, if it isn't then your activity is resuming.
I'm not sure if data are saved on sdcard, but I guess they are saved somewhere on physical memory, so android can free some RAM for another app.
I want following behavior in my app:
1) Show notification on its launch: done
2) Do not show notification while switching through activities of the app
3) If another app is launched over this app, while coming back to the app again show notification.
I am planning to implement it via shared prefs(by storing a boolean value in it),
But cannot figure out the place to reset this value
i.e. how to know that app is exiting or switching to another app to reset this value(so it can show notification when app is launched again).
When an Activity goes to the background, onPause() is called, when it comes back to the foreground, onResume() is called. You can use that states to hide/show your notifications or even store your SharedPreferences.
Have a look at the Activity Lifecylce:
If you want to detect if all application's activities gone to background you need to extend Application class and register activity lifecycle listeners. Then just count calls to onPause() and onCreate() method.
http://developer.android.com/reference/android/app/Application.html#registerActivityLifecycleCallbacks(android.app.Application.ActivityLifecycleCallbacks)
Try to understand Android application life cycle.
Go through the following methods documentation.
onCreate() , onStart(), onPause() , onResume() , onDestroy() , onRestart()
Link
3) If another app is launched over this app, while coming back to the
app again show notification again.
To do this you need to overwrite back button and check activity stack. If it is 0 - show notification.
In an app I am developing, I have a custom view in which I need to save the instance state when either the screen configuration changes or the app is moved to the background. I implement onSaveInstanceState() and onRestoreInstanceState(Parcelable savedInstanceState) and in the case of screen configuration changes, the instance state is saved and restored perfectly. However, when the app is moved to the background, onSaveInstanceState() is not called. This means I have no saved instance state when the activity is brought to the foreground again. My question is then:
Why is onSaveInstanceState() not called when the app is moved to the background, and is it possible to make my view call onSaveInstanceState() when the app is moved to the background?
It occurs when you press the back button to leave the activity. –
Zero
When using the Back button to leave the application the onSaveInstanceState method is not called by the Android system, this is expected system behavior. The back button is meant to stop your app (Read: activity).
The Back button finishes your Activity and there is no need to save app state. But if there is reason to to this then you should override the onStop method for example and save data to the file system there.
Sources:
http://developer.android.com/training/basics/activity-lifecycle/recreating.html
(Recomended read)
There are a few scenarios in which your activity is destroyed due to
normal app behavior, such as when the user presses the Back button or
your activity signals its own destruction by calling finish().
As per my knowledge, when app enter into background, on Activity it calls onPause() method. So please try by moving your code to onPause() method.
I have a problem with my application when it is brought back to the foreground in the case the phone went low on memory while the application was hidden:
The class inheriting from Application is re-created (onCreate is called again), thus losing data it held before. The Activity which is restarted is not the one tagged as main action in the Manifest, but the last one that was active. This is a problem as the main activity, from which the user logs in, is the one responsible for filling in the Application subclass' data and I can't fill it in later.
Is there any way to tell the application to restart at the main activity instead of the latest one in this case ?
You could check in onResume() if the user is logged in. Means that you check if your Application data is filled. If this is not the case, finish the activity and start your first Application.
The user expect to return to the latest activity, so a general "always start first activity" would upset the user...
you could keep the data by implementing methods such as onPause() and onResume()
Check this link out: http://developer.android.com/reference/android/app/Activity.html
this answer might also be helpful: onSaveInstanceState () and onRestoreInstanceState ()
I have an application that navigates to the same activity but each time the activity loads with different parameters. In my application it's a parsed data content retrieved from url. First thing I want to ask: When I push the backbutton of my device I get my earlier activity without being recreated. Is the objects in this activities alive and can I reference them?
Second question is if my first question doesn't make sense, what do you advice me to do?
If you look at the Activity life cycle, you'll notice that as long as your phone has enough memory, your first activity is kept in memory, and with it any member with the data it contains.
But if your phone needs to have some memory, it can kill any activity kept in background (any activity but the one being shown to the user), which means that you'll loose any data that was in your first activity.
To know which happened, keep in mind that the onResume() method will always be called when your activity is brought to foreground (either on creation, or when you navigate back to it), but onCreate() will be called only when your application is created from scratch (meaning you don't have any saved data).
You should use the bundle mechanism to save data when your activity is paused, and load it when you come back to it. Read the paragraph about Saving Activity state in Android doc to see how to use this.
You are not guaranteed that in memory data will be around once you leave an Activity. Read through this part of the dev guide thoroughly to understand the lifecycle of an Activity: http://developer.android.com/guide/topics/fundamentals/activities.html
If you need to persist information, you should override the onPause, onStop, and/or onDestroy methods of your Activity. You can then save your state using SharedPreferences, the SQLite database, or even a flat file.
In the manifest file add the following to the activity:
android:launchMode="singleTop"
In your example what is happening is when you navigate back to the activity using the back button you are bringing up the activity from the stack. When you navigate to the activity inside of the app what is happening is a NEW activity is being created, while the original is still on the stack. The singleTop launch mode will pop the activity out of the stack if it is there when you navigate to it in the app, and only create a new activity if it is not on the stack.
Without singleTop each time you launch the activity in the app it will create a new instance and you will find there are times you have to hit the back button on the same activity more than once due to multiple instances.