您的位置:首页 > 移动开发 > Android开发

android之activity生命周期示例

2010-09-21 21:56 615 查看


FirstActivity.java

package archie.android.life;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class FirstActivity extends Activity {

private static final String TAG="activity";
private Button myButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.v(TAG, "onCreate");

myButton=(Button) findViewById(R.id.myButton);
myButton.setText(R.string.startSec);
myButton.setOnClickListener(new MyButtonListener());
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.v(TAG, "onDestroy");
}
@Override
protected void onPause() {
super.onPause();
Log.v(TAG, "onPause");
}
@Override
protected void onRestart() {
super.onRestart();
Log.v(TAG, "onRestart");
}
@Override
protected void onResume() {
super.onResume();
Log.v(TAG, "onResume");
}
@Override
protected void onStart() {
super.onStart();
Log.v(TAG, "onStart");
}
@Override
protected void onStop() {
super.onStop();
Log.v(TAG, "onStop");
}

class MyButtonListener implements OnClickListener{

@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setClass(FirstActivity.this, SecondActivity.class);
FirstActivity.this.startActivity(intent);

}

}

}


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="@string/hello"
/>
<Button
android:id="@+id/myButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: