"Android Notes" (1)--activity life cycle and communication

Source: Internet
Author: User

First, Activity
1.Activity Statusactivity: If activity is visible in front of the screen, the user's input can be accepted. paused state: Activity loses focus, but remains visible. Stop state: If an activity is obscured by another activity, it stops in its state.
2.Activity life cycle action MethodOnrestart (): Called when the activity was first created, used to initialize some data, such as view, at which time it is stopped. OnStart (): Called when activity is about to be visible to the user, and enters a stop state after the call. Onresume (): Started when user interaction is called and is activated (active) after the call. OnPause (): Called when the activity is resumed and is paused after the call. onStop (): Called when activity is no longer visible to the user, and then in a stopped state to close the activity. ondestory (): Destroying activity is called.
Note: When activity is in a paused state, it may be accidentally killed at the extreme of memory when it is stopped.

3.Activity three operating processes
1) Start activity for the first timeCall OnCreate () Create activity (stop state), call OnStart () (Paused state), call Onresume () to display activity (active)2) Activity A jumps to activity Ba call OnPause () into pause state, B call OnCreate () Enter stop status, b call OnStart () Enter suspend status, call Onresume () show B, call a overlay, and when a is being overwritten OnStop () enters the stop state. 3) from activity B back to acitivity Ab call OnPause () into the paused state, a call Onrestart () Enter the stop state, a call OnStart () into the paused state, a call Onresume () into the active state, overwriting B-B is overwritten Si cho Use OnStop () to enter stop state, B call Ondestory () Destroybecause the activity is created based on the stack, the top of the stack is returned to the stack, so B is destroyed.
ii. communication between activity
Passing data through intent, the official document says that intent is an abstract description that is about to be manipulated (abstract enough)
1. Call the Putextra () method directly into the intent object to deposit data in the form of a key-value pair. Take out the Getxxxextra (), XXX indicates the type of data stored, Getstringextra () takes the string type.

2. Passing values with bundle objectsIf you have too many values, you can put the data into the bundle object, and then pass in the bundle into the intent object. Bundle is the encapsulation of hashmap<string, object>, can only be deposited in the basic data type, such as Int,byte ....
Intent Intent = new Intent (Getapplicationcontext (), bactivity.class);  Bundle bundle = new bundle ();  Bundle.putstring ("name", "XXXX"); Bundle.putint ("Age");  Intent.putextra ("bundle", bundle);  StartActivity (Intent);

3. Retrieve data after jumping activity
First Call Getintent () to get the intent object, and then call Getextra ("Key") to take out the corresponding data. ① directly remove data from intent
String name = This.getintent (). Getstringextra ("name"). ToString ();
② removing data from bundles
Bundle bundle = This.getintent (). Getbundleextra ("bundle"); String name = bundle.getstring ("name"); Int = Bundle.getint ("Age");

Note: The bundle obtains the data needs to call the GetXXX (Key) method, XXX is the data type, for example GetString (), getInt ();
4.startActivity () differs from Startactivityforresultstartactivity () no longer calls back after initiating other activity, which means that the initiator and the initiator are not connected after the boot is complete. if we want to start an activity and fill in the data and return the data to the previous activity, we need to call the Startactivityforresult () method to start the activity. ① initiates another activity in activity A, overriding the Onactivityresult () method in activity A to process the data for the postback. ② needs to call the Setresult () method in activity B to set the returned intent.
Related parameters:Startactivityforresult (Intent Intent, int requestcode), and the second is the "request Code", which is used to distinguish different requests.
onactivityresult (int requestcode, int resultcode, Intent data), the first one is "request Code", the second is "result code", and the third is Intent, which is the data of the postbacksetresult (int resultcode, Intent data), the first is the "result code"for the identification of the Onactivityresult () in a, and the second is the return to a Intent

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.