What the hell is activity? What's the life cycle going on? What is the relationship between XML?

Source: Internet
Author: User
Tags stub custom name

Read these days of books, finally a little harvest, began to read confused, now a kind of learning C + + when the understanding of a part of the feeling, but still have to learn C + + when the dazed, like the header file contains and namespace, always use can or a little touch the mind. A closer look at the life cycle of the activity and its connection with Main.xml and String.xml seems to be a bit understandable.

Heavy in the realization:

Here is my code in the book, it is important for one of them: Onrestart (), Onresume (), OnStart (), OnStop (), OnPause (), OnDestroy () have a understanding of the past, every time I see super all feel is a difficult point, In fact, in the inheritance of the parent class abstract method of rewriting, a bit like this function, but this point is this object. And then "@Override" every time I see the awkward, now also understand that is the way to rewrite the parent method of the declaration, tell me that is not a custom method, if not write is considered a custom method. As for the Log part, it is the "print" of the desired part, which involves different "print" forms, for example LOG.V is any message "print", LOG.E is "print" error message and so on. It is better to run the Watch program.

Code: mainactivity:

Package Com.himi;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.view.View;
Import Android.view.View.OnClickListener;

Import Android.widget.Button;

	public class Mainactivity extends Activity implements Onclicklistener {private Button btn;
		@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
		Setcontentview (R.layout.main);
		LOG.V ("Mainactivity", "onCreate");
		BTN = (Button) Findviewbyid (R.ID.MAIN_BTN);
		Btn.setonclicklistener (this); This.finish ();
			End Current mainactivity} @Override public void OnClick (View arg0) {if (arg0 = = btn) {Intent Intent = new Intent ();
			Intent.setclass (this, otheractivity.class);
		This.startactivity (Intent);
		}} @Override protected void OnDestroy () {//TODO auto-generated Method Stub Super.ondestroy ();
	LOG.V ("Mainactivity", "OnDestroy"); } @Override protected void OnPause () {//TODO Auto-generatEd method Stub Super.onpause ();
	LOG.V ("Mainactivity", "OnPause");
		} @Override protected void Onrestart () {//TODO auto-generated Method Stub Super.onrestart ();
	LOG.V ("Mainactivity", "Onrestart");
		} @Override protected void Onresume () {//TODO auto-generated Method Stub super.onresume ();
	LOG.V ("Mainactivity", "Onresume");
		} @Override protected void OnStart () {//TODO auto-generated Method Stub Super.onstart ();
	LOG.V ("Mainactivity", "OnStart");
		} @Override protected void OnStop () {//TODO auto-generated Method Stub super.onstop ();
	LOG.V ("Mainactivity", "onStop"); }

}

Otheractivity.java:

Package Com.himi;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.view.View;
Import Android.view.View.OnClickListener;

Import Android.widget.Button;

	public class Otheractivity extends Activity implements Onclicklistener {private Button btn;
		@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
		Setcontentview (R.layout.other);
		LOG.V ("Mainactivity", "onCreate");
		BTN = (Button) Findviewbyid (R.ID.OTHER_BTN);
	Btn.setonclicklistener (this);
		} @Override public void OnClick (View arg0) {if (arg0 = = btn) {this.finish ();
		}} @Override protected void OnDestroy () {//TODO auto-generated Method Stub Super.ondestroy ();
	LOG.V ("Mainactivity", "OnDestroy");
		} @Override protected void OnPause () {//TODO auto-generated Method Stub super.onpause ();
	LOG.V ("Mainactivity", "OnPause"); } @Override protected void Onrestart () {//TODO auto-generated method Stub Super. Onrestart ();
	LOG.V ("Mainactivity", "Onrestart");
		} @Override protected void Onresume () {//TODO auto-generated Method Stub super.onresume ();
	LOG.V ("Mainactivity", "Onresume");
		} @Override protected void OnStart () {//TODO auto-generated Method Stub Super.onstart ();
	LOG.V ("Mainactivity", "OnStart");
		} @Override protected void OnStop () {//TODO auto-generated Method Stub super.onstop ();
	LOG.V ("Mainactivity", "onStop"); }

}


Since the Mainactivity.java is the top layer, when the trigger button is set up, the interface of Otheractivity.java will pop up, specifically to understand what the activity declaration cycle is, how to run the end.

As for the XML part is actually the design interface properties, whether it is the main interface or a button set of properties, the basic attributes include width, height, custom name, id attribute, id attribute is a interface and Java code linked to the property, The specific will be assigned in R.java, as long as you make the declaration in the corresponding XML, r file does not need to worry about itself, assigned by the system.

As for the width height setting, only involves the layout problem, simply is defined as "fill type" or "Fill by content" Two (Fill_parent and wrap_content), but the layout problem is relatively thin, need to be discussed separately.

Code:

Main.xml:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout
xmlns:android= "http://schemas.android.com/apk/ Res/android "
    android:orientation=" vertical "
    android:layout_width=" fill_parent "
    android:layout_ height= "Fill_parent"
    >
<textview  
    android:layout_width= "fill_parent" 
    android:layout_ height= "Wrap_content" 
    android:text= "@string/hello"
    />
<button   
    android:layout_width= " Fill_parent " 
    android:layout_height=" wrap_content " 
    android:text=" @string/btn_open "
    android:id=" @ +id/main_btn "
    />
</LinearLayout>

Other.xml:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout
xmlns:android= "http://schemas.android.com/apk/ Res/android "
    android:orientation=" vertical "
    android:layout_width=" fill_parent "
    android:layout_ height= "Fill_parent"
    >
<textview  
    android:layout_width= "fill_parent" 
    android:layout_ height= "Wrap_content" 
    android:text= "@string/otheractiviy_hello"
    />
<button   
    android: Layout_width= "Fill_parent" 
    android:layout_height= "wrap_content" 
    android:text= "@string/otheractiviy_ Btnclose "
    android:id=" @+id/other_btn "
    />
</LinearLayout>
String.xml:

<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
	<string name= "Hello" >hello world, mainactivity!</string>
	<string name= "app_name" >MyFirstProject</string>
	<string Name = "Btn_open" > Open otheractivity. </string>
	<string name= "Otheractiviy_hello" >i ' m otheractivity!</string>
	<string name = "Otheractiviy_app_name" >OterActivity</string>
	<string name= "Otheractiviy_btnclose" > Close current activity!</string>
	
</resources>



The TextView and button are settings for different components,</> can be seen as a unique form of definition. From the structure of the "linearlayout" in the linear layout of the text and key building, each component is a unique form of "Android:" Layout location is Layout_width is the width set, as for "@string/" is to refer to the contents of the String.xml file in this form, the top is the version declaration <?xml version= "1.0" encoding= "Utf-8"?>

Screenshot experience:

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.