Activity1.1

An Activity is an application component that provides a screen with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map.

 

An application usually consists of multiple activities

 

Each time a new activity starts, the previous activity is stopped, but the system preserves the activity in a stack (the "back stack").

 

when the user is done with the current activity and presses the Back button, it is popped from the stack (and destroyed) and the previous activity resumes

 

activity's lifecycle callback methods

 

There are several callback methods that an activity might receive, due to a change in its state—whether the system is creating it, stopping it, resuming it, or destroying it—and each callback provides you the opportunity to perform specific work that's appropriate to that state change

 

To create an activity, you must create a subclass of Activity (or an existing subclass of it).

 

 

package com.cmge;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

/**
 * @desc	一个简单的Activity
 * @author	ljt
 * @time	2014年8月24日 下午10:42:19
 */
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}

 

 

The two most important callback methods are:

onCreate()

You must implement this method.

The system calls this when creating your activity

this is where you must call setContentView() to define the layout for the activity's user interface.

 

public void setContentView (View view)

Set the activity content to an explicit view. This view is placed directly into the activity's view hierarchy

 

onPause()

The system calls this method as the first indication that the user is leaving your activity 

This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back).

 

 

Declaring the activity in the manifest

You must declare your activity in the manifest file in order for it to be accessible to the system

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cmge"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.cmge.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 Theandroid:name attribute is the only required attribute—it specifies the class name of the activity

 

Using intent filters

using the <intent-filter> element—in order to declare how other application components may activate it.

 

The <action> element specifies that this is the "main" entry point to the application. 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值