ANDROID83 activity life cycle, startup mode, passing data on return

Source: Internet
Author: User

#Android四大组件*Activity*Broadcastreceiver*Service*contentprovider#activity life cycle*Oncreate:activity object created, but not visible at this time*onstart:activity is visible on the screen, but there is no focus at this time (not able to point, cannot interact)*the onresume:activity is visible on the screen and gets the focus*onpause:activity is still visible on the screen at this time, but there is no focus*onstop:activity is not visible, but the object of activity is still in memory*Ondestroy:activity object is destroyed # # #内存不足*when the memory is low, the system will first kill the process of the background activity, all killed, if the memory is still insufficient, then it will kill the suspended state activity in the process, if not enough, it is possible to kill the foreground process*If there are multiple background processes, the least recently used algorithm (LRU) is used when selecting the target to kill---# # #Activity任务栈 (when you press the back key, the stack is displayed sequentially)*While the application is running, multiple activity may be open in memory, and all open activity will be saved in the activity task stack*stack: LIFO, the most advanced stack, will be the last stack # # #Activity的启动模式4种:1. Standard mode: The default is this mode2. Singletop: If the target activity is not at the top of the stack, a new activity is started, and if it is already at the top of the stack, it will not start again (the front page is the top of the stack acticity)3. Singletask: If there is no activity in the stack, the activity is created when it is started (when the return key is displayed, and the activity is removed from the stack), and if an instance of the activity already exists in the stack, at startup, Kills all the activity that is on top of the activity in the stack, then the activity becomes the top activity of the stack. When viewing multiple pages, it appears that there are multiple pages open, but only one activity is switching. *Singletask: To ensure that there is only one instance of the activity in the entire stack, the first start is to create new ones and will not be created later but returned. 4. SingleInstance: The activity that is set up for this mode will have a separate task stack, and the instance of the activity will only be created and saved in a separate task stack, which is different from the task stack of the activity that is not a singleton pattern. is not a singleton mode, all activity is saved on the same task stack, and not in the singleton mode, the activity starts multiple times in the task stack. *SingleInstance's role: to ensure that there is only one instance of the activity in the entire system's memory, and that 10 application launches are the same activity, such as when the phone is activated on the same call activity. ---#横竖屏的切换*activity will destroy the rebuild when switching between the two screens, in order to read the new layout file (there are 2 layout files on the screen)*Write dead direction, do not allow switching android:screenorientation="Portrait"android:screenorientation="Landscape"*When you configure activity, the following properties are added, and the rebuild android:configchanges is not destroyed when switching between screens="orientation|keyboardhidden|screensize" //sets the direction of the current activity, using the code to control the vertical screen (written in the Oncreat method)setrequestedorientation (activityinfo.screen_orientation_portrait);---#Activity返回时传递数据*Request code: Used to distinguish which activity the data came from*result code: Used to differentiate, what type of data is returned # Master open activity Get return value # # #从A界面打开B界面, B when the interface is closed, return a data to the A interface step:1. Turn on activity and get return value Startactivityforresult (Intent,0);2to implement the logic of setting data in the newly opened interface Intent=NewIntent (); Data.putextra ("Phone", phone); //sets a result data that is returned to the callerSetresult (0, data); Finish ();//turns off the current activity before returning the data3in the opener activity, implement method Onactivityresult (intRequestcode,intResultCode, Intent data) to retrieve the returned information via data4. Determining business logic based on request code and result code

Mainactivity:

 PackageCom.itheima.getresult;ImportAndroid.os.Bundle;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.widget.EditText; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); }     Public voidClick (View c) {//jump to select contact ActivityIntent Intent =NewIntent ( This, Contactactivity.class);//startactivity (Intent); //The activity initiated with this API tells the system to return data when the contactactivity is destroyed, and the system will callback OnactivityresultStartactivityforresult (Intent, 10); }         Public voidClick2 (View v) {//jump to activity to select a quick replyIntent Intent =NewIntent ( This, Callbackactivity.class); Startactivityforresult (Intent,20); }    //If an activity returns data when it is destroyed, this method is called to receive the data//Requestcode: Used to differentiate which activity the data came from//ResultCode: Used to differentiate what type of data is returned@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {        Super. Onactivityresult (Requestcode, ResultCode, data); String name= Data.getstringextra ("name"); if(Requestcode = = 10) {EditText et=(EditText) Findviewbyid (r.id.et);        Et.settext (name); }        Else if(Requestcode = = 20) {EditText et_content=(EditText) Findviewbyid (r.id.et_content);        Et_content.settext (name); }}} Configure Activity<activity android:name= "com.itheima.getresult.ContactActivity" ></activity><activity android:name= "Com.itheima.getresult.CallbackActivity" ></activity>

Callbackactivity:

 PackageCom.itheima.getresult;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.AdapterView;ImportAndroid.widget.AdapterView.OnItemClickListener;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.ListView; Public classCallbackactivityextendsActivity {//show a list of LV@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.activity_contact); ListView LV=(ListView) Findviewbyid (r.id.lv); FinalString[] Objects =Newstring[]{"No, no, no, roll Duzi.",                "Daughter-in-law I was wrong, beg forgiveness",                "Lao Tzu is the head of the household        }; Lv.setadapter (NewArrayadapter<string> ( This, R.layout.item_listview, R.id.tv, objects)); Lv.setonitemclicklistener (NewOnitemclicklistener () {@Override Public voidOnitemclick (adapterview<?>Parent, view view,intPositionLongID) {Intent data=NewIntent (); Data.putextra ("Name", objects[position]); Setresult (2, data);            Finish ();    }        }); }}

Contactactivity:

 PackageCom.itheima.getresult;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.AdapterView;ImportAndroid.widget.AdapterView.OnItemClickListener;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.ListView; Public classContactactivityextendsActivity {//Show list activity of LV@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.activity_contact); ListView LV=(ListView) Findviewbyid (r.id.lv); FinalString[] Objects =Newstring[]{"Very small",                "Brother forced",                "World class XXX",                "National Service First"        }; Lv.setadapter (NewArrayadapter<string> ( This, R.layout.item_listview, R.id.tv, objects)); //Click to listen to the ListView settings entryLv.setonitemclicklistener (NewOnitemclicklistener () {//when an entry is clicked, this method calls@Override Public voidOnitemclick (adapterview<?>Parent, view view,intPositionLongID) {//when the activity returns, the data is passed through the intent objectIntent data =NewIntent (); //encapsulates the data to be passed into the intent objectData.putextra ("name", objects[position]); //when the current activity is destroyed, the intent of data is passed to the mainactivity that initiates the current activity.Setresult (1, data); //destroy the current activity, go back to the last mainactivity,finish ();    }        }); } @Override//Click the Back button to perform the operation called Finish (),     Public voidonbackpressed () {Super. onbackpressed (); }}

In some special applications, such as the game, do not want the screen switch activity is destroyed re-create
Requirements: Disable the life cycle of the screen switch
1. Write dead on the screen
android:screenorientation= "Landscape"
android:screenorientation= "Portrait"

2. Let the system's environment no longer be sensitive to the screen switch.

Android:configchanges= "Orientation|screensize|keyboardhidden"

---

ANDROID83 activity life cycle, startup mode, passing data on return

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.