Android 0 Basics Section 73rd: Getting Started with activity, creating and configuring it so easy

Source: Internet
Author: User

Activity is one of the most important components of Android apps and one of the most common parts of Android apps. The examples seen earlier typically contain only one activity or one appcompatactivity, but in practice this is unlikely and often involves multiple activity, with different activity presenting different interfaces to the user. Learn the relevant knowledge of activity from the beginning of this issue.

I. Introduction of activity

Activity is the most basic and most commonly used component of Android components, and in an Android app, an activity is usually a separate screen. Each activity is implemented as a separate class and is inherited from the activity's base class.

Of course, in different application scenarios, it is sometimes required to inherit the subclass of activity. If the application interface includes only a list, you can have the application inherit listactivity, and if the application interface needs to implement the tab effect, you can have the application inherit tabactivity.

The activity class provided in Android, as shown in the class diagram of its subclasses.

As you can see, the activity class indirectly or directly inherits the base classes of Context, Contextwrapper, Contextthemewrapper, and so the activity and its subclasses can call their methods directly. The functions of its subclasses are:

    • Accountauthenticatoractivity: Account authentication activity, which is an activity to implement account authentication.

    • Tabactivity: The activity that implements the tab interface has been learned in the previous issue.

    • Aliasactivity: Stub activity, which initiates another specified activity based on the information meta-data in the component's own manifest file, and the finish itself.

    • Expandablelistactivity: Implements an activity that expands the list interface.

    • Listactivity: Activity that contains a ListView component has been learned before.

    • Launcheractivity: An activity that implements a list interface, when a single list item is selected, the corresponding activity is initiated.

    • Preferenceactivity: Implement a program parameter setting, storage function activity.

    • Fragmentactivity: To solve the interface without fragment before Android 3.0, it is easy to embed fragment in the activity to achieve the layout effect you want.

Some students also found that we in the front of most of the study is the contact of the appcompatactivity, and its activity and what is the relationship?

When you use Eclipse to develop Android apps, Mainactivity automatically inherits activity when you create a project, and when you use Android Studio to develop Android apps, The appcompatactivity is automatically inherited when the project is created. Of course, we can change the appcompatactivity to activity, we will find that there are some differences in the interface after running the program, respectively, as shown in the two effects.

As you can see from the above run, when you inherit Appcompatactivity, a actionbar appears at the top of the interface, which appears by default as the program name, and when you inherit activity, you find that the top Actionbar is missing. This is a visual difference in the interface, the content about Actionbar will be learned in the following content, do not introduce too much here. There is little difference between appcompatactivity and activity for learning about activity-related knowledge.

In an application, activity is like an interface administrator, and the user's actions on the interface are managed through activity.

Ii. Activity creation and configuration

In the previous study has already created a lot of activity interface, very simple, generally create activity of the steps summarized as follows:

    1. Defines a class that inherits from a android.app.Activity or its subclasses.

    2. Create an XML file in the Res/layout directory that is used to create the layout of the activity.

    3. Register the Activity created in the Androidmanifest.xml file.

    4. Rewrite the activity's OnCreate () method and use Setcontentview () in the method to load the specified layout file.

It is important to note that the Setcontentview () method can either receive the View object as a parameter or receive the resource ID for the layout file as a parameter.

For everyone to learn how to create the activity well, let's go through it in turn.

As with the previous creation of the Widgetsample project, first create an Android project, named Activitysample, follow the same steps as before, here no longer repeat, if not clear can look back "development IDE big upgrade, finally ushered in Android Studio ".

1. Create Activity Class

Right-click the package name where Mainactivity is located and select New→java Class, as shown in.

The Up action pops up the Create New class window and then loses the name myactivity in the Name text box in that window, and set superclass to android.support.v7.app.AppCompatActivity as shown in. Click "OK" button to complete the Activity class creation.

Of course, the activity created here is empty and there is nothing in it.

2. Create an activity-corresponding layout

Next, create an XML file in the Res/layout directory, right-click the Layout folder, and select New→layout resource file, as shown in.

The step-up action pops up the new Resource file window, entering the layout file name my_layout in file name, as shown in. Click the "OK" button to complete the layout resource file creation.

Next, add a TextView component in the My_layout.xml file, with the following code:

3. Registered Activity

Android apps require that all application components (Activity, Service, ContentProvider, broadcastreceiver) must be in the Androidmanifest.xml file < The application.../> element is configured inside.

To register the myactivity, the code is as follows:

If you want to set myactivity to the interface that the application starts by default, you need to configure the <intent-filter> node in the <activity> node.

The <action android:name= "Android.intent.action.MAIN"/> In the node indicates that the current activity is set to the activity that the program started first.

<category android:name= "Android.intent.category. LAUNCHER "/> means to have the current activity create an icon on the desktop.

4. Rewrite the OnCreate () method

Next, rewrite the OnCreate () method in MyActivity and set the layout file to be loaded, with the following code:

After a few steps above, the creation and configuration of myactivity is basically complete, then you can run the program, you can see the effect shown.

As you can see, the app displays the MyActivity interface as soon as it starts, stating that MyActivity is configured in the Androidmanifest manifest file and created successfully.

Of course, we do not use all the activity of the program as described above as the application launch interface, after all, in the actual development of an Android program will only start using an activity as a startup page, there are some differences in the configuration of the activity. If the Android project Mainactivity as the starting interface, myactivity as the other interface, then the MyActivity configuration code is as follows:

With this configuration, the myactivity is started differently and will be learned in the next section.

5. Quickly create

Android Studio development tool is very convenient, in fact, the above steps to create myactivity have been encapsulated, only a few simple operations to complete.

Right-click the package name where Mainactivity is located and select New→activity→empty Activity, as shown in.

Enter the appropriate configuration in the popup New Android Activity dialog box, as shown in, click the Finish button to complete the activity creation.

Has been discovered, after the above steps, Android Studio directly completed the creation of the activity of a few steps, it is very convenient, which is often used by developers to the method. Then we can change the code we need.

Come here today, if you have any questions welcome message to discuss together, also welcome to join the Android 0 Basic introductory Technical discussion group, grow together!

This article copyright for the public Share talent show (Shareexpert)--Xin 鱻 all, if necessary reprint please contact the author authorized, hereby declare!

Past period Summary share:

Android 0 Basics Introduction 1th: Android's past life

Android 0 Basics Section 2nd: Android system Architecture and application components those things

Android 0 Basics Section 3rd: Bring you up to talk about Android development environment

Android 0 Basics 4th: Installing and configuring the JDK correctly Ko fu the first trick

Android 0 Basics 5th: Use ADT bundles to easily meet the goddess

Android 0 Basics 6th: Configuration Optimization SDK Manager, official dating goddess

Android 0 Basics 7th: Take care of Android simulator and start the Sweet journey

Android 0 Basics 8th: HelloWorld, the starting point for my first trip

Android 0 Basics 9th: Android app, no code can be developed

Android 0 Basics Section 10th: Development IDE Big upgrade, finally ushered in Android Studio

Android 0 Basics Introductory Section 11th: Simple steps to take you to fly, run Android Studio project

Android 0 Basics 12th: Get familiar with the Android studio interface and start selling

Android 0 Basics 13th: Android Studio Personalized configuration to create a tool for development

Android 0 Basics 14th: Using high-speed genymotion, stepping into the rocket era

Android 0 Basics Section 15th: Mastering the Android Studio project structure, sailing

Android 0 Basics Section 16th: Android User Interface Development overview

Android 0 Basics Section 17th: text box TextView

Android 0 Basics Section 18th: Input box EditText

Android 0 Basics Get started section 19th: Buttons button

Android 0 Basics 20th: check box checkbox and radio button radiobutton

Android 0 Basics Section 21st: Switch Components ToggleButton and switch

Android 0 Basics Section 22nd: Image View ImageView

Android 0 Basics Section 23rd: Image button ImageButton and zoom button Zoombutton

Android 0 Basics Section 24th: Customize view simple to use to create your own controls

Android 0 Basics Section 25th: Simple and most commonly used linearlayout linear layouts

Android 0 Basics Section 26th: Two alignments, layout_gravity and gravity differ greatly

Android 0 Basics section 27th: Using padding and margin correctly

Android 0 Basics Section 28th: Easy to master relativelayout relative layout

Android 0 Basics 29th: Use tablelayout table layouts

Android 0 Basics 30th: Two minutes master Framelayout frame layout

Android 0 Basics Section 31st: absolutelayout absolute Layout with less

Android 0 Basics Section 32nd: New GridLayout Grid layout

Android 0 Basics section 33rd: Android Event Handling overview

Android 0 Basics Section 34th: Listening-based event handling in Android

Android 0 Basics Section 35th: Callback-based event handling in Android

Android 0 Basics Section 36th: Handling of Android system events

Android 0 Basics 37th: First Knowledge ListView

Android 0 Basics 38th: First Knowledge Adapter

Android 0 Basics section 39th: listactivity and custom list items

Android 0 Basics Section 40th: Customizing Arrayadapter

Android 0 Basics Section 41st: Using Simpleadapter

Android 0 Basics Section 42nd: Customizing Baseadapter

Android 0 Basics section 43rd: ListView Optimization and List End-to-end use

Android 0 Basics Section 44th: ListView Data Dynamic Update

Android 0 Basic Getting Started section 45th: Grid view GridView

Android 0 Basics section 46th: List Options Box spinner

Android 0 Basic Getting Started section 47th: AutoComplete text Box Autocompletetextview

Android 0 Basics Section 48th: Collapsible list Expandablelistview

Android 0 Basics section 49th: Adapterviewflipper picture Carousel

Android 0 Basics Section 50th: StackView card Stacking

Android 0 Basics Section 51st: progress bar ProgressBar

Android 0 Basics section 52nd: Customizing ProgressBar Cool progress bar

Android 0 Basics 53rd: Drag bar Seekbar and star rating bar Ratingbar

Android 0 Basics section 54th: View switch Components Viewswitcher

Android 0 Basics Section 55th: Imageswitcher and Textswitcher

Android 0 Basics Section 56th: Flip View Viewflipper

Android 0 Basics Section 57th: DatePicker and Timepicker selectors

Android 0 Basics Section 58th: Value selector numberpicker

Android 0 Basics Section 59th: Common Three clock clocks components

Android 0 Basics Section 60th: Calendar view CalendarView and timer chronometer

Android 0 Basics Section 61st: Scrolling view ScrollView

Android 0 Basics section 62nd: Search box Component Searchview

Android 0 Basics Introductory Section 63rd: Learn from the tab tabhost

Android 0 Basics 64th: Uncover Recyclerview

Android 0 Basics Section 65th: Recyclerview Split Line Development tips

Android 0 Basics Section 66th: Recyclerview Click event Handling

Android 0 Basics Section 67th: Recyclerview Data Dynamic Update

Android 0 Basics 68th: Recyclerview adding end-to-end views

Android 0 Basics Section 69th: Viewpager Quick Implementation Guide page

Android 0 Basics 70th: Viewpager build Tabhost effect

Android 0 Basics 71st: CardView Simple card-based layout

Android 0 Basics Section 72nd: Swiperefreshlayout drop-down refresh

Android 0 Basics Section 73rd: Getting Started with activity, creating and configuring it so easy

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.