<span id="Label3"></p><p><p><span style="font-size: 18pt;"> <strong><span style="font-family: 宋体;">I. What IS activity</span></strong></span></p></p><p><p><span style="font-size: 18pt;"><strong><span style="font-family: 宋体;">an <span style="font-size: 18px;">activity is a component that can contain a user interface and is primarily used to interact with the User. An application can contain 0 or more Activities.</span></span></strong></span></p></p><p><p><span style="font-size: 18pt;"><strong><span style="font-family: 宋体;">II. basic usage of activities</span></strong></span></p></p><p><p><span style="font-size: 18px;"><strong><span style="font-family: 宋体;">The first thing you need to do is register your activity in Androidmanif:</span></strong></span></p></p><p><p><span style="font-size: 18px;"><strong><span style="font-family: 宋体;"><activity></span></strong></span></p></p><p><p><span style="font-size: 18px;"><strong><span style="font-family: 宋体;">......</span></strong></span></p></p><p><p><span style="font-size: 18px;"><strong><span style="font-family: 宋体;"></activity></span></strong></span></p></p><p><p><span style="font-size: 18px;"><strong><span style="font-family: 宋体;">You can also specify the active title bar in the OnCreate method in the Acitiviy class by using the Requestwindowfeature method</span></strong></span></p></p><p><p><span style="font-size: 18px;"><strong><span style="font-family: 宋体;">of course, You can use the Findviewbyid () method to use the resources contained in the resource File. Such as: button button= (button) Findviewbyid (r.id.button);</span></strong></span></p></p><p><p><span style="font-size: 18px;"><strong><span style="font-family: 宋体;">Usage of Toast: toast.maketext (activity.this, "", toas. key parameters). show ();</span></strong></span></p></p><p><p><span style="font-size: 18px;"><strong><span style="font-family: 宋体;">Use the menu in the activity, of course, you need to create the XML file of the menu in Res first, and then override the Oncreateoptionmenu () method in the Activity class</span></strong></span></p></p><p><p><span style="font-size: 18px;"><strong><span style="font-family: 宋体;">PUBILC Boolean Oncreateoptionmenu ()</span></strong></span></p></p><p><p><span style="font-size: 18px;"><strong><span style="font-family: 宋体;">{</span></strong></span></p></p><p><p><span style="font-size: 18px;"><strong><span style="font-family: 宋体;">Getmenuinflater (). inflate (r.menu. menu name);</span></strong></span></p></p><p><p><span style="font-size: 18px;"><strong><span style="font-family: 宋体;">Return true;</span></strong></span></p></p><p><p><span style="font-size: 18px;"><strong><span style="font-family: 宋体;">}</span></strong></span></p></p><p><p><span style="font-size: 18px;"><strong><span style="font-family: 宋体;">If you want to respond to a menu click event, you need to rewrite the onoptionsitemselected (MenuItem Item) method and use switch (item.getitemid ()) to make a Decision.</span></strong></span></p></p><p><p><span style="font-size: 18px;"><strong><span style="font-family: 宋体;"></span></strong></span></p></p><p><p><em><span style="font-size: 14pt;"><strong><span style="font-family: 宋体;">shuttle between different activities: using intent</span></strong></span> <span style="font-size: 18px;"> <strong> <span style="font-family: 宋体;"></span></strong></span></em></p></p><p><p> <strong>Explicitly called</strong><br><strong>Intent Intent = new Intent (firstactivity.this,secondactivity.class);</strong></p></p><p><p> <strong>StartActivity (intent);</strong><br><strong>implicit invocation</strong><br><strong>Intent Intent = new Intent ("com.example.activitytest.ACTION_START");</strong><br><strong>Intent.addcategory ("com.example.activitytest.MY_CATEGORY");</strong></p></p><p><p><strong><strong>StartActivity (intent);</strong></strong></p></p><p><p><strong>You can also use the Intent.setdata () method to show more Calls. For example, if you want to open baidu: intent.setdata (url.parse ("http://www.baidu.com"));</strong></p></p><p><p><strong>If you want to open the Phone: intent.setdata (url.parse ("tel:10086"));</strong></p></p><p><p><strong>Intent can also be used to pass data between activities, which provides the Putextra () method.</strong></p></p><p><p><strong>Activity1:intent = new Intent (activity1.this,activity2.class);</strong></p></p><p><p><strong>Intent.putextra ("extra_data", data);</strong></p></p><p><p><strong>Activity2:intent = Getintent ();</strong></p></p><p><p><strong>String data = Intent.getstringextra ("extra_data");</strong></p></p><p><p><strong>If you need to return data to the previous activity, you need to use Startactivityforresult () to start the child activity, and then use the Setresul method in the child activity to override the Onactivityresult in the main activity (), using switch (reuestcode) in Onactivityresult () to determine which child activity is specified when the child activity is Started.</strong></p></p><p><p><strong></strong></p></p><p><p><span style="font-size: 14pt;"><strong><em>life cycle of an activity:</em></strong></span></p></p><p><p><strong><span style="font-size: 14pt;"> <span style="font-size: 18px;">1.onCreate () 2.onStart () 3.onResume () 4.onPause () 5.onStop () 6.onDestroy ()</span></span></strong></p></p><p><p><strong><span style="font-size: 14pt;"><span style="font-size: 18px;">7.onRestart ()</span></span></strong></p></p><p><p><strong><span style="font-size: 14pt;"><span style="font-size: 18px;"></span></span></strong></p></p><p><p><strong>Full lifetime: between OnCreate and OnDestroy</strong></p></p><p><p><strong>Visible lifetime: between OnStart and OnStop</strong></p></p><p><p><strong>Foreground lifetime: between Onresum and OnPause</strong></p></p><p><p><strong> </strong></p></p><p><p><span style="font-size: 14pt;"><strong> <em>Active startup mode <span style="font-size: 18px;">:</span></em></strong></span></p></p><p><p><span style="font-size: 14pt;"><strong><em> </em><span style="font-size: 18px;">1.standard (default)</span></strong></span></p></p><p><p><span style="font-size: 14pt;"><strong><span style="font-size: 18px;">2.SingleTop: at startup, If it is found that the stack top of the return stack is already the activity, it is considered possible to use it, and no new activity instances are Created.</span></strong></span></p></p><p><p><span style="font-size: 14pt;"><strong><span style="font-size: 18px;">3.SingleTask: at startup, first check the return stack for the existence of the activity is an instance, if the discovery already exists then directly use the instance, and put the activity above this activity all out of the stack, if not found to create a new The Activity Instance.</span></strong></span></p></p><p><p><span style="font-size: 14pt;"><strong><span style="font-size: 18px;">4.SingleInstance: A new return stack is specified to manage the Activity. (resolve Shared Activity Instances)</span></strong></span></p></p><p><p></p></p><p><p><span style="font-size: 18px;"><strong><span style="font-family: 宋体;"> <em><span style="font-size: 14pt;">Activity practice:</span></em> </span></strong></span></p></p><p><p><span style="font-size: 18px;"><strong><span style="font-family: 宋体;">1. Know which activity is currently in: create a new baseactivity so that other activities inherit from it, and then in OnCreate () log.d ("baseactivity",</span></strong></span></p></p><p><p><span style="font-size: 18px;"><strong><span style="font-family: 宋体;">GetClass (). Getsimplename ());</span></strong></span></p></p><p><p><span style="font-size: 18px;"><strong><span style="font-family: 宋体;">2. Exit the program at any time: create a new Activitycollector class as an activity manager and use List<activity>activities to manage it.</span></strong></span></p></p><p><p><span style="font-size: 18px;"><strong><span style="font-family: 宋体;"></span></strong></span></p></p><p><p></p></p><p><p>Android Learning (a) Activity</p></p></span>
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