Android之Activity

一.初步认识

src

package cn.android.lyj;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class TestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button button = (Button)findViewById(R.id.bt1);
        button.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				TextView textView = (TextView)findViewById(R.id.tv1);
				textView.setText("修改后");
			}
		});
    }
}


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
	<Button 
	    android:id="@+id/bt1"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="@string/bt_text" />
</LinearLayout>

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">修改前text</string>
    <string name="app_name">Test</string>
    <string name="bt_text">修改text</string>

</resources>

AndroidManifest.xml

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

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".TestActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

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

</manifest>


二.生命周期

注:缺少OnRestart(), 在OnStop()执行后当重新打开这个Activity时会被调用,OnRestart()——>onStart()——>onResume()

src

package cn.android.lyj;

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

public class TestActivity extends Activity {
	private static final String TAG = "LYJ";
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Log.i(TAG, "onCreate()");
    }
    
    @Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		
		Log.i(TAG, "onStart()");
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		Log.i(TAG, "onResume()");
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		
		Log.i(TAG, "onPause()");
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		
		Log.i(TAG, "onStop()");
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		
		Log.i(TAG, "onDestroy()");
	}
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
</LinearLayout>

string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">测试生命周期</string>
    <string name="app_name">Test</string>
    
</resources>

AndroidManifest.xml同上


二.运行结果:

启动运行:


分两种情况:

1.点击模拟器返回按钮——>会destory这个Activity


2.点击模拟器主页按钮——>用栈保存这个Activity,下次重新打开时不会再create


下次再打开这个Activity



三.细节

1. it is popped from the stack (and destroyed) and the previous activity resumes.

2. The two most important callback methods are.

a. onCreate() :  You must implement this method. The system calls this when creating your activity.

b. onPause() :   The system calls this method as the first indication that the user is leaving your activity (though it does not always    mean the activity is being destroyed). 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).

3. ViewGroup继承自View.

4. An intent can also carry small amounts of data to be used by the activity that is started.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值