Activity基础小结
Activity:与用户交互的基本呈现单元
3种状态:运行、停止(Home键)、暂停(长按Home键半透明,后面遮住前面)
生命周期:7个方法
基本操作:
1.启动一个Activity
由MainActivity通过按钮跳到Aty1,按钮监听器代码如下:
Intent i = new Intent(MainActivity.this,Aty1.class);
startActivity(i);
2.关闭一个Activity
在Aty1点击按钮关闭,按钮监听器代码如下:
finish();
3.传递数据
从MainActivity把数据传到Aty1,两种处理方式:
(1):简单数据:
MainActivity.java核心代码:
Intent i = new Intent(MainActivity.this,Aty1.class);
i.putExtra("txt1", "Hello!");
startActivity(i);
Aty1.java核心代码:
tvOut.setText(getIntent().getStringExtra("txt1"));
(2):复杂数据
MainActivity.java核心代码:
Intent i = new Intent(MainActivity.this,Aty1.class);
Bundle data = new Bundle();
data.putString("Hello");
i.putExtras(data);
startActivity(i);
Aty.java核心代码:
Bundle data = getIntent().getExtras();
String txt1 = data.getString("txt1");
tvOut.setText(txt1);
4.取回第二个Activity返回的数据,不能用startActivity(),而应该用startActivityForResult()
MainActivity.java核心代码:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
String result = data.getStringExtra("result");
tvOut = (TextView) findViewById(R.id.tvOut);
tvOut.setText(result);
super.onActivityResult(requestCode, resultCode, data);
}
Aty1.java核心代码:
btnClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent();
i.putExtra("result", "Hi");
setResult(0, i);
finish();
}
});
现整体代码如下,目的:验证7个方法的执行顺序;采用传送复杂数据的方式从MainActivity把“Hello”传到Aty1,并且接收Aty1传回的“Hi”
MainActivity.java
package com.example.jk_jc_sidazujian;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button btnStartAty1;
private TextView tvOut;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStartAty1 = (Button) findViewById(R.id.btnStartAty1);
btnStartAty1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this,Aty1.class);
// i.putExtra("txt1", "Hello!");
// startActivity(i);
Bundle data = new Bundle();
data.putString("txt1", "Hello");
i.putExtras(data);
startActivityForResult(i, 0);
}
});
System.out.println("onCreat");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
String result = data.getStringExtra("result");
tvOut = (TextView) findViewById(R.id.tvOut);
tvOut.setText(result);
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
System.out.println("onStart");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
System.out.println("onResume");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
System.out.println("onPause");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
System.out.println("onStop");
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("onDestory");
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
System.out.println("onRestart");
}
}
Aty1.java
package com.example.jk_jc_sidazujian;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Aty1 extends Activity {
private Button btnClose;
private TextView tvOut;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.aty1);
tvOut= (TextView) findViewById(R.id.tvOut);
Bundle data = getIntent().getExtras();
String txt1 = data.getString("txt1");
tvOut.setText(txt1);
btnClose = (Button) findViewById(R.id.btnClose);
btnClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent();
i.putExtra("result", "Hi");
setResult(0, i);
finish();
}
});
}
}
相应布局xml文件就不赘述了。
运行效果如下:
点击start Aty1按钮
点击btnclose