Android Learning Note (first article) writing the first program Hello World+activity

Source: Internet
Author: User

PS: In the laboratory on my one to do Android, like others learn the Java EE, still someone with to learn, I so no one can only own the work, and finally is the majority of the foundation is finished, finally began to formally engaged in Android ... No one to bring a novice, I still work silently ...

Learning content:

1. Write the first Hello World program.

Learn Android, then you need to have a compiler to integrate this environment, and then build a good environment of the compiler to develop on it, I was introduced in Eclipse to do Android development ... The environment is set up here I will not introduce. Here directly to develop ...

First create a new project, presumably new project this thing everybody understand ... After the Android project was built, it is now generally 4. X later version of ... Therefore in 4. An fragment.xml file will be added to layout layouts in a later version of the project ... This file is called the fragment function, because oneself is also a novice, this thing is also just contact, to this thing also not very understanding, so I first directly deleted the Fragment.xml file, just with Activity_main.xml file ... After you delete the Fragment.xml file, you need to modify the Mainactivity.java file, or the error will appear directly. The changes are as follows ...//commented out sections that represent the parts we need to modify ... Delete directly can also, comment out also line ...

 PackageCom.example.android_hello;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.support.v7.app.ActionBar;Importandroid.support.v4.app.Fragment;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.os.Build;Importandroid.app.Activity; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_main);//if (savedinstancestate = = null) {//Getsupportfragmentmanager (). BeginTransaction ()//. Add (R.id.container, New Placeholderfragment ()). commit ();//        }} @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml.        intID =Item.getitemid (); if(id = =r.id.action_settings) {            return true; }        return Super. onoptionsitemselected (item); }    /*** A placeholder fragment containing a simple view. *///Public Static Class Placeholderfragment extends Fragment {////Public placeholderfragment () {//        }////@Override//Public View Oncreateview (layoutinflater inflater, ViewGroup container,//Bundle savedinstancestate) {//View Rootview = inflater.inflate (R.layout.fragment_main, container,//false);//return rootview;//        }//    }}

At the same time, we also need to modify the Activity.xml file, the contents of all deleted. Then the following code:

put an absolute layout and a text display component ...<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" >    <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/hello_world">            </TextView></Relativelayout>

Then you can run it directly ... In fact, the direct establishment of the Android project can also be run directly, the results will be displayed on the simulator Hello world ... Because of the novice reason, so the use of the regular mode ... In the future, we will conduct in-depth research on fragment ...
I've established this in 4. After x version, there will be one more AppCompat file, which contains some of the classes to be called, which is a new version after 22.1, the provision of this class can bring more convenience. This file is not allowed to be deleted ... Deleted after the program will error ...

2.Activity of knowledge ...

Activity is one of the four components of the Android program ... Activity is the application layer of the program ... Each display screen of the program is an activity ... Activity can be interpreted as a JSP file ... Activity is not directly displayed, it is an abstraction layer, a shell, the content is displayed is view or ViewGroup, is the user interface components. This next chapter is to say ... First I have an impression ...

Activity is a life-cycle ... The most important and basic function of the phone is to call, it means that the phone may be at any time to suspend the current program, if the battery is not enough time may be at any time to close the existing program, so the Android program and the computer program is different, specific to the activity, The activity's life cycle is not controlled by itself, but by the Android system.

Four basic states of activity ...

I.running Running state ...

This status indicates that the activity is running, displayed at the top of the screen, and can interact with the user ...

Ii. Paused paused state ...

The current activity represented by the paused state is covered by a transparent layer or another non-full-screen activity ... Although visible, it is not possible to implement user interaction ...

Iii. Stop Stop State ...

When an activity is completely covered by another activity, the user is not able to see ...

iv. Killed Kill state

Indicates that the activity was not started or was killed directly by the system.

Life cycle diagram of activity ...

Activity stack ....


The active activity is at the top of the stack, it is running, and when new activity enters the top of the screen, the original activity is pressed into the second layer, and if his screen is not completely obscured, he is in pause State, If he is covered then he is in the stop state. Of course, regardless of whether you are from any layer, you may be forced to shut down when the system feels inadequate resources, of course, the program at the bottom of the stack is closed. For example, when you call the Activity.finish () method in a program, the result is the same as when the user presses the back key: He tells the activity manager that the activity instance can be "recycled." Activity Manager then activates the activity on the second level of the stack and re-enter the stack, pressing the original activity into the second layer of the stack, from the Running state to the Paused state. three. Example: Observing the change in activity state caused by the flip screen
Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.support.v7.app.ActionBar;Importandroid.support.v4.app.Fragment;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.os.Build;ImportAndroid.widget.TextView;Importandroid.app.Activity;ImportAndroid.widget.ImageView;Importandroid.app.Activity;/**Called when the activity is first created.*/ Public classMainactivitylifeextendsActivity {@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.main);    LOG.I (Tag, "onCreate"); } @Override Public voidOnStart () {Super. OnStart ();    LOG.I (Tag, "OnStart"); } @Override Public voidOnPause () {Super. OnPause ();    LOG.I (Tag, "OnPause"); } @Override Public voidOnresume () {Super. Onresume ();    LOG.I (Tag, "Onresume"); } @Override Public voidOnStop () {Super. OnStop ();    LOG.I (Tag, "onStop"); } @Override Public voidOnDestroy () {Super. OnDestroy ();    LOG.I (Tag, "OnDestroy"); }}

By running, we will see the output results under the Logcat console ... Similar to the following results ...

Android Learning Note (first article) writing the first program Hello World+activity

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.