Introduction to Android-Activity and androidactivity

Source: Internet
Author: User

Introduction to Android-Activity and androidactivity

In the previous article, we will continue to introduce the Android system. Previous Article: [translation] About Android

This article mainly introduces some main concepts for building Android applications:

Activity

Activity is a separate UI page in an application. For example, in an email application, an activity is used to display the email list, and activity is used to display the specific email. Generally, an application with user interaction contains at least one activity.

An application can contain multiple activities, and each activity can interact with each other through intent. intent will be introduced later in this article.

An activity is generally inherited from android. app. Activity. All the activities are managed by the onXX () method:

  • OnCreate()-Set the activity Initialization Method
  • OnStart()-This method is called when the activity becomes visible. When the activity is stopped and restarted, the method is called again;
  • OnResume()-This method is called when the activity is visible and you are ready to receive and process user input;
  • OnPause()-This method is called when the activity is about to lose focus. This method is saved as submitted data quickly, stops CPU intensive computing, and prepares the activity to run in the background;
  • OnStop()-Activity loses focus and this method is called. Used to release resources used by applications;
  • OnRestart()-This method is called when the activity is stopped and needed again. This method restores the previous activity status;
  • OnDestroy()-This method is called when the activity is canceled, indicating that the lifecycle of the activity is over;

Activity lifecycle:

Example of activity Lifecycle

Add in strings. xml

<string name="CodeProjectWebURL">http://www.codeproject.com</string>

Rewrite these methods of activity:

public class FirstActivity extends Activity {    private WebView oWebView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_first);        oWebView = (WebView)findViewById(R.id.webView);        oWebView.loadUrl(getString(R.string.CodeProjectWebURL));    }    /** Called when the activity is about to become visible. */    @Override    protected void onStart() {        super.onStart();        Toast.makeText(getApplicationContext(), "Inside onStart", Toast.LENGTH_LONG).show();    }    /** Called when the activity has become visible. */    @Override    protected void onResume() {        super.onResume();        Toast.makeText(getApplicationContext(), "Inside onResume", Toast.LENGTH_LONG).show();    }    /** Called when another activity is taking focus. */    @Override    protected void onPause() {        super.onPause();        Toast.makeText(getApplicationContext(), "Inside onPause", Toast.LENGTH_LONG).show();    }    /** Called when the activity is no longer visible. */    @Override    protected void onStop() {        super.onStop();        Toast.makeText(getApplicationContext(), "Inside onStop", Toast.LENGTH_LONG).show();    }    /** Called just before the activity is destroyed. */    @Override    public void onDestroy() {        super.onDestroy();        Toast.makeText(getApplicationContext(), "Inside onDestroy", Toast.LENGTH_LONG).show();    }}

Add the WebView control in Layout:

<WebView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/webView"        android:layout_alignParentTop="true"        android:layout_alignParentStart="true"        android:layout_alignParentBottom="true"        android:layout_alignParentEnd="true" />

Running effect:

After the onCreate () method is called, The onStart () method is called. We can see it through the Toast message;

When other activities are activated (such as pressing the home Key), the current activity will call the onPause () method, and the onStop () method will be called immediately;

Scenario where onResume () is called: When an application is running and a call is made, the current application will lose the focus (onPause, onStop) and the phone application will get the focus; the onResume () method is called when the application obtains the focus again after the call;

When the activity ends (the onFinish method is called) or the system temporarily destroys the activity instance for memory space, the onDestroy () method is called.

 

PS: You still need to split activity, service, content provider, intent, and fragement into different articles, which is a little long.

 

Note:

1. This Article has been deleted during the translation process;

2. Key words are not translated to avoid confusion;

3. http://www.codeproject.com/Articles/802449/Article

Version history:

Initial version:


Which of the following statements is best for the Activity class in Android?

Activity is a container of the image. Various controls (buttons, text, check boxes, etc.) are placed in the container to form the software interface ~
Activity is visible. If no control is added, it is like a blank form in Windows.
Use Public Class activity01 extends Activity {} to create an activity01 Activity Class, that is, an Activity




How to identify activity A and activity B in android and how the system identifies each activity

If you want to identify it, you can customize an application, Singleton

This instanceof activity class name is used to manage all activities.

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.