Android之Activity

Activity一般用于和用户的交互,所以Activity类管理创建窗口,用户可以通过setContentView(View)来放置需要的UI控件。当Activity全屏显示时,可以用于其它用户,例如浮动窗口(通过windwosIsFloating设置)或内置activity(通过ActivityGroup设置)。

有两个方法大部分Activity子类都会实现:
OnCreate(Bundle)是用于初始化Activity,更为重要的是,在这里一般通过调用setContentView设置已经定义的layout资源和findViewById(int)初始化UI控件。
OnPause用于处理Avtivity将失去焦点动作,这时应该保存用户已经修改的数据。

所有的Activity必须在AndroidManifest.xml里面声明,否则在startActivity调用后,会crash.

实例:ActivityDemo
运行效果:

Android之Activity_第1张图片

>adb shell dumpsys activity top
TASK com.rainsong.activitydemo id=90
  ACTIVITY com.rainsong.activitydemo/.MainActivity 421bccb8 pid=7776
    Local Activity 41e26278 State:
      mResumed=true mStopped=false mFinished=false
      mLoadersStarted=true
      mChangingConfigurations=false
      mCurrentConfig={1.0 460mcc2mnc zh_CN ldltr sw360dp w360dp h615dp 320dpi nrml long port finger -keyb/v/h -nav/h s.20 themeChanged=0 themeChangedFlags=0}
    FragmentManager misc state:
      mActivity=com.rainsong.activitydemo.MainActivity@41e26278
      mContainer=android.app.Activity$1@41e263e8
      mCurState=5 mStateSaved=false mDestroyed=false
    ViewRoot:
      mAdded=true mRemoved=false
      mConsumeBatchedInputScheduled=false
      mPendingInputEventCount=0
      mProcessInputEventsScheduled=false
      mTraversalScheduled=false
      android.view.ViewRootImpl$NativePreImeInputStage: mQueueLength=0
      android.view.ViewRootImpl$ImeInputStage: mQueueLength=0
      android.view.ViewRootImpl$NativePostImeInputStage: mQueueLength=0
    Choreographer:
      mFrameScheduled=false
      mLastFrameTime=152388770 (16254 ms ago)
    View Hierarchy:
      com.android.internal.policy.impl.PhoneWindow$DecorView{41e28790 V.E..... R....... 0,0-720,1280}
        com.android.internal.widget.ActionBarOverlayLayout{41e2ab00 V.E..... ........ 0,0-720,1280 #102032b android:id/action_bar_overlay_layout}
          android.widget.FrameLayout{41e2d250 V.E..... ........ 0,146-720,1280 #1020002 android:id/content}
            android.widget.LinearLayout{41f5b7f8 V.E..... ........ 0,0-720,1134}
              android.widget.TextView{41f5bb20 V.ED.... ........ 0,0-720,38}

          com.android.internal.widget.ActionBarContainer{41e2d778 V.ED.... ........ 0,50-720,146 #102032c android:id/action_bar_container}
            com.android.internal.widget.ActionBarView{41e32ea0 V.E..... ........ 0,0-720,96 #102032d android:id/action_bar}
              android.widget.LinearLayout{41e33618 V.....C. ........ 32,0-342,96}
                com.android.internal.widget.ActionBarView$HomeView{41e35100 V.E..... ........ 0,0-80,96}
                  android.widget.ImageView{41e355d8 G.ED.... ......I. 0,0-0,0 #102025a android:id/up}
                  android.widget.ImageView{41e359e0 V.ED.... ........ 8,16-72,80 #102002c android:id/home}
                android.widget.LinearLayout{41e36db8 V.E..... ........ 80,23-310,72}
                  android.widget.TextView{41e37268 V.ED.... ........ 0,0-214,49 #1020265 android:id/action_bar_title}
                  android.widget.TextView{41e3fb68 G.ED.... ......I. 0,0-0,0 #1020266 android:id/action_bar_subtitle}
              com.android.internal.view.menu.ActionMenuView{41f74db8 V.ED.... ......ID 720,0-720,96}
            com.android.internal.widget.ActionBarContextView{41e40960 G.E..... ......ID 0,0-0,0 #102032e android:id/action_context_bar}
          com.android.internal.widget.ActionBarContainer{41e413c8 G.ED.... ......I. 0,0-0,0 #102032f android:id/split_action_bar}
        com.lbe.security.service.core.client.a.f{41f69ec8 V.E..... ........ 0,0-720,1280}
    Looper (main, tid 1) {41d93b00}
      Message 0: { when=+1s938ms what=1 obj=com.lbe.security.service.core.client.proxy.f@41debb08 target=com.lbe.security.service.core.client.proxy.bc }
      (Total messages: 1, idling=false, quitting=false)

代码清单:

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

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

    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
        <activity android:name="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>

布局文件:main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hello Android"
    />
</LinearLayout>

Java源代码文件:MainActivity.java

package com.rainsong.activitydemo;

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

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}



你可能感兴趣的:(android,Activity)