Android学习-认识Activity

来源:互联网 发布:淘宝上代购药品 编辑:程序博客网 时间:2024/06/08 00:32

重新认识Activity
1>什么是Activty
Activity是一个应用程序组件,提供用户与程序交互的界面

2>Android四大组件
–Activity
–Service
–BroadcastReceiver
–Content Provider

3>Activity如何创建使用
—继承Android的Activity类
—重写方法
—设置显示布局
—在AndroidManifest文件中,注册Activity

<activity android:name=".MainActivity"><!-有name这一行就ok-->  <intent-filter>     <action android:name="android.intent.action.MAIN" />     <category android:name="android.intent.category.LAUNCHER" />  </intent-filter> </activity>

Activity生命周期

1>概述
onCreat();创建
onStart();运行
onResume();获取焦点
onPause();失去焦点
onStop();暂停
onDestroy();销毁
onRestart();重启
这里写图片描述

2>Activity四种状态
–活动状态 :Activity处于界面最顶端,获取焦点
–暂停状态:Activity失去焦点,但是对用户可见
–停止状态:被完全遮挡,但保留所有状态和成员信息
–非活动状态:Activity被停止

3>实验
运行程序–>onCreate: onStart: onResume:
返回屏幕–>onCreate: onStart: onResume: onPause: onStop:
点击第一个活动的按钮跳转到第二个活动(第二个活动为Dialog,因为这样没有被完全遮挡)–> onCreate: onStart: onResume: onPause:

注册活动并且设置为Dialog(这样就可以未全部覆盖)

<activity android:name=".FirstActivity"            android:theme="@android:style/Theme.DeviceDefault.Dialog"/>

代码

MainActivity.javapublic class MainActivity extends AppCompatActivity {    private static final String TAG = "MainActivity";    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main_layout);        Log.i(TAG, "onCreate: ");        Button button = (Button)findViewById(R.id.button);        button.setOnClickListener(new View.OnClickListener() {            public void onClick(View view) {                Intent intent = new Intent(MainActivity.this,FirstActivity.class);                MainActivity.this.startActivity(intent);            }        });    }    protected void onStart() {        super.onStart();        Log.i(TAG, "onStart: ");    }    protected void onRestart() {        super.onRestart();        Log.i(TAG, "onRestart: ");    }    protected void onResume() {        super.onResume();        Log.i(TAG, "onResume: ");    }    protected void onDestroy() {        super.onDestroy();        Log.i(TAG, "onDestroy: ");    }    protected void onStop() {        super.onStop();        Log.i(TAG, "onStop: ");    }    protected void onPause() {        super.onPause();        Log.i(TAG, "onPause: ");    }}
FirstActivity.javapublic class FirstActivity extends AppCompatActivity {    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.first_layout);    }}
AndroidManifest.xml<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.angel.applacation">    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:roundIcon="@mipmap/ic_launcher_round"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity android:name=".FirstActivity"                   android:theme="@android:style/Theme.DeviceDefault.Dialog"/>    </application></manifest>
fisrst_layout.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:id="@+id/textView"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="第二个活动创建成功" /></LinearLayout>
main_layout.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <Button        android:id="@+id/button"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Button" /></LinearLayout>
原创粉丝点击