Activity in Android

Source: Internet
Author: User


For any Android Application, the Activity class is the core component. In many cases, you need to define and implement an Activity for each screen display. The Activity class needs to introduce the package import android. app. Activity.

Android applications can be multi-threaded. Android allows multiple programs to run at the same time. Applications can have background processes and can be interrupted by calls, text messages, and other events. However, at the same time, only one Activity program can be visible to users. That is to say, at any time, only one Activity of the Android application is in the foreground.

In the Android system, the system will track the running Activity objects and put these Activity objects into an Activity stack. The Activity of the current Activity is at the top of the stack. After the Activity is completed, it will be removed from the stack.

In Android, the following steps are required to switch the Activity:

1. register the Activity to AndroidMainfest. xml, for example:

<Activity
Android: name = "MessageActivity"/>

2. UI switching through Intent

Intent intent = new Intent (CurrentActivity. this, NextActivity. class );
StartActivity (intent );

Sometimes some data may need to be transferred from one Activity to another. The most common method is to use Bundle

When using the Bundle, first press the data in the current Activity into the Bundle

Intent intent = new Intent (CurrentActivity. this, NextActivity. class );

Bundle mBundle = new Bundle ();

MBundle. putString ("Data", "dataText"); // press Data

Intent. putExtras (mBundle );

StartActivity (intent );

You need to accept data after starting another Activity. The specific operations are as follows:

Bundle bundle = getIntent (). getExtras ();

String data = bundle. getString ("Data"); // read data


If you only perform interface switching, you can use another method.

You can set layout using the setContentView method. This interface is switched only in one Activity. All variables can be obtained without data being pushed and read.

Example:

SetContentView (R. layout. message );







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.