Code:
Http://pan.baidu.com/s/1kTi0qhD
Effect:
These methods that are explained in eclipse
Note In addition to the general method, there are two methods,onsaveinstancestate(bundle outstate),onrestoreinstancestate(bundle Savedinstancestate) Take a look at the explanations of these two methods
onCreate (Bundle savedinstancestate)
@Override
Called when the activity is starting. This is where most initialization should go:calling Setcontentview (int.) to inflate the activity ' s UI, using Findviewbyid To programmatically interact with widgets in the UI, calling
Managedquery (Android.net.Uri, String [], String, string [], String) to retrieve cursors for data being displayed, etc.
You can call finish from within this function, in which case OnDestroy () 'll be immediately called without any of the Res T of the activity Lifecycle (OnStart, Onresume, onPause, etc) executing.
Derived classes must call through to the Super class ' s implementation of this method. If They does not, an exception would be thrown.
Overrides:oncreate (...) in Activity
Parameters:
Savedinstancestate If The activity is being re-initialized after previously being shut and this Bundle contains the Data it most recently supplied in Onsaveinstancestate. Note:otherwise it is null.
--------------------------------------------------------------------------------------------------------------- ----------------------------------------------
onsaveinstancestate (Bundle outstate)
Called to retrieve per-instance state from a activity before being killed so this state can is restored in onCreate O R Onrestoreinstancestate (the Bundle populated by this method is passed to both).
This method was called before an activity could be killed if it comes back some time in the future it can restore I TS State. For example, if activity B was launched in front of activity a, and at some point activity A is
Killed to reclaim resources, activity A would have an chance to save the current state of its user interface via this method So, then the user returns to activity A, the state of the user interface can be restored via onCreate
or Onrestoreinstancestate.
Do not confuse the This method with activity lifecycle callbacks such as OnPause, which are always called when an AC Tivity is being placed in the background or on its-to-destruction, or onStop which is called before
destruction . One example of when OnPause and OnStop are called and not the This method was when a user navigates back from activity B to ACTI Vity A:There is no need to call Onsaveinstancestate on B because that particular
instance WI ll never is restored, so the system avoids calling it . An example when OnPause was called and not onsaveinstancestate was when activity B was launched in front of activity a:the s Ystem Avoid calling
Onsaveinstancestate on activity A if it isn ' t killed during the lifetime of B since the state Of the user interface of A would stay intact.
The default implementation takes care of the most of the UI per-instance state for your by calling Android.view.View.onSaveInst Ancestate () on all view in the hierarchy that have an ID, and by saving the ID of the currently
focused view (all of which are restored by the default implementation of Onrestoreinstancestate). If you override this method to save additional information not captured by each individual view, you'll likely want to C All
Through to the default implementation, otherwise is prepared to save all of the state from each view yourself.
If called, this method would occur before onStop. There is no guarantees about whether it'll occur before or after onPause.
Parameters:
Outstate Bundle in which to place your saved state.
See Also:
OnCreate
Onrestoreinstancestate
OnPause
--------------------------------------------------------------------------------------------------------------- ----------------------------------------------
onrestoreinstancestate (Bundle savedinstancestate)
This method was called after OnStart when the activity was being re-initialized from a previously saved, given Savedinstancestate. Most implementations'll simply use onCreate-to-restore their state, but it's
Sometimes convenient to does it here after all of the initialization have been done or to allow subclasses to decide whether To use your default implementation. The default implementation of this method performs a restore of
Any view state is had previously been frozen by onsaveinstancestate.
This method is called between OnStart and Onpostcreate.
Parameters:
Savedinstancestate the data most recently supplied in Onsaveinstancestate.
See Also:
OnCreate
Onpostcreate
Onresume
Onsaveinstancestate
--------------------------------------------------------------------------------------------------------------- ----------------------------------------------
Setcontentview (int layoutresid)
Set the activity content from a layout resource. The resource would be inflated and adding all top-level the activity.
Parameters:
Layoutresid Resource ID to be inflated.
See Also:
Setcontentview (Android.view.View)
Setcontentview (Android.view.View, Android.view.ViewGroup.LayoutParams)
--------------------------------------------------------------------------------------------------------------- ----------------------------------------------
OnStart ()
@Override
Called after oncreate-or after Onrestart when the activity had been stopped, but was now again being displayed R. It is followed by Onresume.
Derived classes must call through to the Super class ' s implementation of this method. If They does not, an exception would be thrown.
Overrides:onstart () in Activity
--------------------------------------------------------------------------------------------------------------- ----------------------------------------------
Onresume ()
@Override
Called after Onrestoreinstancestate, Onrestart, or onPause, for your activity-to-start interacting with the user. This was a good place to begin animations, open exclusive-access devices (such as the camera), etc.
Keep in mind that Onresume are not the best indicator that your activity is visible to the user; A system window such as the Keyguard May is in front. Use onwindowfocuschanged to know for certain this your activity is
Visible to the user (for example, to resume a game).
Derived classes must call through to the Super class ' s implementation of this method. If They does not, an exception would be thrown.
Overrides:onresume () in Activity
--------------------------------------------------------------------------------------------------------------- ----------------------------------------------
OnPause ()
@Override
Called as part of the activity lifecycle if an activity was going into the background, but had not (yet) been killed. The counterpart to Onresume.
When activity B was launched in front of activity A, the this callback would be being invoked on a. B would not being created until A ' s on Pause returns, so is sure to does anything lengthy here.
This callback was mostly used for saving all persistent state the activity was editing, to present a "edit on place" Model T o the user and making sure nothing is lost if there be not enough resources to start the new activity
Without first killing this one. This is also a good place to does things like stop animations and other things that consume a noticeable amount of CPUs in or Der to do the switch to the next activity as fast as possible, or to
Close resources that is exclusive access such as the camera.
In situations where the system needs more memory it could kill paused processes to reclaim resources. Because of this, you should be sure, and all of the Your are saved by the time you return from this function. Inch
General Onsaveinstancestate are used to save per-instance state in the activity and this method are used to store global per Sistent data (in content providers, files, etc.)
After receiving this call, you'll usually receive a following call to OnStop (after the next activity have been resumed an D displayed), however in some cases there would be a direct call back to Onresume without going
Through the stopped state.
Derived classes must call through to the Super class ' s implementation of this method. If They does not, an exception would be thrown.
Overrides:onpause () in Activity
--------------------------------------------------------------------------------------------------------------- ----------------------------------------------
Onrestart ()
@Override
Called after OnStop, the current activity was being re-displayed to the user (the user had navigated back to it). It is followed by OnStart and then Onresume.
For activities that is using raw Cursor objects (instead of creating them through Managedquery (Android.net.Uri, String [] , string, string [], String), this is usually the place where the cursor should be requeried (because
You had deactivated it in OnStop.
Derived classes must call through to the Super class ' s implementation of this method. If They does not, an exception would be thrown.
Overrides:onrestart () in Activity
--------------------------------------------------------------------------------------------------------------- ----------------------------------------------
OnStop ()
@Override
Called when is no longer visible to the user. You'll next receive either Onrestart, OnDestroy, or nothing, and depending on later user activity.
Note that this method may never is called, in low memory situations where the system does not has enough memory to keep y Our activity's process running after it OnPause method is called.
Derived classes must call through to the Super class ' s implementation of this method. If They does not, an exception would be thrown.
Overrides:onstop () in Activity
--------------------------------------------------------------------------------------------------------------- ----------------------------------------------
OnDestroy ()
@Override
Perform any final cleanup before a activity is destroyed. This can happen either because the activity was finishing (someone called finish on it, or because the system is Temporaril Y destroying this instance of the activity to
Save space. You can distinguish between these-scenarios with the Isfinishing method.
Note:do not count on this method being called as a place for saving data! For example, if the activity is editing data in a content provider, those edits should being committed in either onPause or on Saveinstancestate, not
Here. This method was usually implemented to the free resources like threads that's associated with an activity, so that a Destroye D activity does not leave such things around while the rest of their application is still running.
There is situations where the system would simply kill the activity ' s hosting process without calling this method (or any Others) in it, so it should is used to does things that is intended to remain around after the process
Goes away.
Derived classes must call through to the Super class ' s implementation of this method. If They does not, an exception would be thrown.
Overrides:ondestroy () in Activity
--------------------------------------------------------------------------------------------------------------- ----------------------------------------------
When is OnPause () Onresume () OnStop () called?