Activity of Android four components

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-size:21px;font-family:‘宋体‘;">1.</span> <span style="font-size:21px;font-family:‘宋体‘;">what is activity?</span></p></p><p><p><span style="font-size:21px;font-family:‘宋体‘;color:#0000FF;">is a screen that can interact with the user, such as a phone call, a photo, a map, a calendar interface</span></p></p><p><p><br></p></p><p style="text-indent:0;"><p style="text-indent:0;"><span style="font-size:21px;font-family:‘宋体‘;">2. </span> <span style="font-size:21px;font-family:‘宋体‘;">Activity has 7 life-cycle methods ( <span style="font-size:21px;font-family:‘宋体‘;">The life cycle describes how an object will be executed from the creation (new) to the death process</span> ):</span></p></p><p><p><span style="font-size:21px;font-family:‘宋体‘;color:#0000FF;">onCreate</span>: Called when the Activity object is created for the first time</p></p><p><p><span style="font-size:21px;font-family:‘宋体‘;color:#0000FF;">OnStart</span>: Call this function when activity becomes visible</p></p><p><p><span style="font-size:21px;font-family:‘宋体‘;color:#0000FF;">Onresume</span>: This method is called when activity begins to prepare for user interaction</p></p><p><p><span style="font-size:21px;font-family:‘宋体‘;color:#0000FF;">OnPause</span>: This method is called before the system starts another activity</p></p><p><p><span style="font-size:21px;font-family:‘宋体‘;color:#0000FF;">OnStop</span>: This method is called when the current activity becomes invisible</p></p><p><p><span style="font-size:21px;font-family:‘宋体‘;color:#0000FF;">OnDestroy</span>: The method will be called before the current activity is destroyed</p></p><p><p><span style="font-size:21px;font-family:‘宋体‘;color:#0000FF;">Onrestart</span>: This method will be called before an activity is started again</p></p><p><p><br></p></p><p><p>1) When you enter the second activity from the first activity, call the OnPause of the first activity, and then call the second Activity's oncreate, onStart, onresume, Then call the first Activity's OnStop</p></p><p><p>2) when the first activi is returned from the second activity, the second Activity's OnPause is called, and the first Activity's onrestart, onStart, onresume are called, Then call the second Activity's onstop, OnDestroy</p></p><p><p><br></p></p><p><p>3. The life cycle can be divided into three states: regardless of the state, activity does not automatically release its own resources-by writing code in onpause, onStop, OnDestroy to release resources</p></p><p><p>1) Resumed:activity Object Running state</p></p><p><p>2) Paused: Another activity is in the front end, but this activity is also visible, such as a dialog box</p></p><p><p>3) Stopped: The other activity is in the front end, completely blocking the activity</p></p><p><p><br></p></p><p><p>4. Bind the Layout. Each new activity will inherit (extends) the activity class, the replication OnCreate (bundle) method, and then bind the Activity's layout file xml</p></p><pre class="brush:java;toolbar:false"><pre class="brush:java;toolbar:false">public class Baseswipebacek extends Activity {@Overrideprotected void OnCreate (Bundle Savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.main_layout);}}</pre></pre><p><p><br></p></p><p><p>5. The activity created must be registered in the androidmanifest.xml, otherwise the run will throw an exception</p></p><pre class="brush:java;toolbar:false"><pre class="brush:java;toolbar:false"><activity android:label= "second"//title android:name= ". secondactivity" >//package name. class name (package name can be omitted) </activity></pre></pre><p><p><br></p></p><p><p>6. Activate activity</p></p><pre class="brush:java;toolbar:false"><pre class="brush:java;toolbar:false">Intent Intent = new Intent (); intent.setclass (this, secondactivity.class); This is the context object, which is the current Activity object StartActivity (intent);</pre></pre><p><p><br></p></p><p><p>7. Transfer of data between two activity</p></p><p><p>Basic data types can be passed through intent<br></p></p><p><p>1) Passing Data</p></p><pre class="brush:java;toolbar:false"><pre class="brush:java;toolbar:false">Intent Intent = new Intent () intent.setclass (this, secondactivity.class); intent.putextra ("key", "data"); First parameter number key, the first parameter is the data to be passed, can be any basic data type startactivity (intent);</pre></pre><p><p>2) take out the data</p></p><pre class="brush:java;toolbar:false"><pre class="brush:java;toolbar:false">Intent Intent = getintent ();  String str = Intent.getstringextra ("key"); parameter is key and must be the same as the key Passed.</pre></pre><p><p>3) if the object is to be passed, it can be passed through serializable after serialization</p></p><p><p>Let the class implement the <span style="font-size:21px;font-family:‘Courier New‘;color:#0000FF;">Serializable</span> interface, and remove the object by Intent.getserializableextra</p></p><pre class="brush:java;toolbar:false"><pre class="brush:java;toolbar:false">public class Data implements serializable{private static final long Serialversionuid = -3445893843765959236l; Serialized serial version marked public string name;public int age;public Data (string name, int. Age) {this.name = Name;this.age = age;}}</pre></pre><p><p><br></p></p><pre class="brush:java;toolbar:false;"><pre class="brush:java;toolbar:false;">public class Firstactivity extends Activity {@Overrideprotected void OnCreate (Bundle Savedinstancestate) { Super.oncreate (savedinstancestate), setcontentview (r.layout.main_layout);D ata data = new data ("xiaoming", +); Intent Intent = new Intent (); intent.setclass (this, secondactivity.class); intent.putextra ("key", data); startactivity (Intent);}}</pre></pre><pre class="brush:java;toolbar:false"><pre class="brush:java;toolbar:false"><br></pre></pre><pre class="brush:java;toolbar:false"><pre class="brush:java;toolbar:false">public class Secondactivity extends Activity {@Overrideprotected void OnCreate (Bundle Savedinstancestate) { Super.oncreate (savedinstancestate); setcontentview (r.layout.second_layout); Intent Intent = getintent ();D ata Data = ( Data) Intent.getserializableextra ("key");}}</pre></pre><p><p><br></p></p><p><p>Activity of Android four components</p></p></span>

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.