Activity的生命周期
onCreate();创建
onStart();运行
onResume();获取焦点
onPasue();失去焦点
onStop();暂停
onDestroy();销毁
onRestart();
–活动状态(Active/Running) Activity处于界面最顶端,获取焦点
–暂停状态(Paused) Activity失去焦点,但对用户可见
–停止状态(Stopped) Activity被完全遮挡,但保留所有状态和成员信息
–非活动状态(Killed)Activity被停止
什么是Intent
Intent可以理解为信使(意图)
由Intent来协助完成Android各个组件之间的通讯
Intent 包括两种跳转方式
1.无返回结果的跳转方式:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class FirstActivity extends Activity{
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(FirstActivity.this,SActivity.class);
startActivity(intent);
}
});
}
}
2.有返回结果的页面跳转
第一个画面
public class FirstActivity extends Activity{
private Button btn;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
tv = (TextView) findViewById(R.id.mTextView);
btn = (Button) findViewById(R.id.button2);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(FirstActivity.this,SActivity.class);
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==1 && resultCode ==2) {
String context = data.getStringExtra("data");
tv.setText(context);
}
}
}
第二个跳转画面
public class SActivity extends Activity{
private Button btn;
private String datas = "hello word";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO 自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seconed);
btn = (Button) findViewById(R.id.button3);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent data = new Intent();
data.putExtra("data", datas);
setResult(2, data);
finish();
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="启动第一个页面" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="启动第二个页面" />
<TextView
android:id="@+id/mTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示返回结果"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击返回" />
</LinearLayout>