3Activity生命周期
三大状态,七个方法
public class MainActivity extends Activity {
/**
* 1.Activity 创建时,第一个调用的方法,通常我们在该方法中加载布局文件,初始化UI组件,事件注册等
*
*
* */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("MainActivity--onCreate");
}
/**
* 2.在onCreate之后调用,用于显示界面,此时界面可以看到,但用户还不能进行交互
*
* */
@Override
protected void onStart() {
super.onStart();
System.out.println("MainActivity--onStart");
}
/**
* 当一个stoppend状态的Activity被返回时调用,之后再调用OnResume方法进入运行状态
*
*
*
* */
@Override
protected void onRestart() {
super.onRestart();
System.out.println("MainActivity--onRestart");
}
/**
* 3.在onStart之后调用,该法方执行完后,用户可进行交互,当前Activity进入resumed状态
*
* 当一个paused状态的activity被重新返回时,会再次调用该方法,让Activity 进入运行状态
*
* **/
@Override
protected void onResume() {
super.onResume();
System.out.println("MainActivity--onResume");
}
/**
*
* 当其它Activity(透明或窗体模式) 进入时,该方法会调用,让当前Activity进入pansed状态,(暂停)
*
* 当前Activity 还可见但不可交互,如果其它更高优先级的APP需要内存时,当前Activity可以会被销毁(Kill)
*
* 当前Activity 被返回时会调用 onResume方法
*
* */
@Override
protected void onPause() {
super.onPause();
System.out.println("MainActivity--onPause");
}
/***
*
* 当其它Activity完全覆盖该Activity时,会被调用,当前Activity进入stopped状态(停止状态)
*
* 不可见,如果其它更高优先级的APP需要内存时,当前Activity可以会被销毁(Kill)
*
* 当前Activity 被返回时会调用 onRestart方法
*
* */
@Override
protected void onStop() {
super.onStop();
System.out.println("MainActivity--onStop");
}
/***
*
* 当前Activity被摧毁时调用, 通常在该方法中用来释放资源,当前Activity Killed,死亡
*
*
* **/
@Override
protected void onDestroy() {
super.onDestroy();
System.out.println("MainActivity--onDestroy");
}
}
两个activity之间传递数据:
简但数据传递:
1.主Activity.xml
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:hint="请输入信息"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送"
android:id="@+id/button"
android:layout_marginTop="46dp"
android:layout_below="@+id/textView"
android:layout_alignParentStart="true"
android:onClick="sendClick"
/>
public class MainActivity extends Activity {
private EditText editText ;
private String edata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.textView);
}
//启动activityB ,并传递数据
public void sendClick(View v){
//创建意图,想做什么
//参数: 上下文,要跳转的组件字节码
Intent intent = new Intent(this,Main2Activity.class);
//封装数据的容器
Bundle data = new Bundle();
edata = editText.getText().toString();//获取输入框的值
data.putString("msg",edata);
/**
//封装数据的容器:简化写法
String data = editText.getText().toString();//获取输入框的值
intent.putExtra("data",data);
startActivity(intent);
*/
intent.putExtra("data",data);
startActivity(intent);//启动新的Activity
}
}
2.副Activity.xml
<TextView
android:id="@+id/msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="no data" />
public class Main2Activity extends Activity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textView = (TextView) findViewById(R.id.msg);
//拿到Intent,但不是同一个,但内容是一样的
Intent intent = getIntent();
Bundle data = intent.getBundleExtra("data");
String msg = data.getString("msg");
/**
简化取值:
Intent intent = getIntent();
String msg = intent.getStringExtra("data");
textView.setText(msg);
*/
textView.setText(msg);
}
}
对象传递
对象需要实现Serializable接口
1.发送
public void objClick(View v){
Obj o = new Obj();
o.name = "zhang";
o.address = "北京";
o.age = 20;
Intent intent = new Intent(this,Main2Activity.class);
intent.putExtra("mo",o);//以序列化对象传递对象
startActivity(intent);
}
2.接收:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textView = (TextView) findViewById(R.id.msg);
//拿到Intent,但不是同一个,但内容是一样的
Intent intent = getIntent();
Obj o = (Obj) intent.getSerializableExtra("mo");
textView.setText(o.toString());
}
对象传递 使用Android传递 性能高
1.对象
public class Obj implements Parcelable{
String name;
String address;
int age;
@Override
public String toString() {
return "Obj{" +
"name='" + name + '\'' +
", address='" + address + '\'' +
", age=" + age +
'}';
}
@Override
public int describeContents() {
return 0;
}
//将要传递的数据写到这个包裹里面
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(age);
dest.writeString(address);
}
//对象的创建器,接收的时候,需要用这个方法读数据,也就是解包
public static final Parcelable.Creator<Obj> CREATOR
= new Parcelable.Creator<Obj>() {
public Obj createFromParcel(Parcel in) {
Obj o = new Obj();
o.name = in.readString();
o.age = in.readInt();
o.address = in.readString();
return o;
}
public Obj[] newArray(int size) {
return new Obj[size];
}
};
}
2.发送:
public void objClick2(View v){
Obj o = new Obj();
o.name = "zhang";
o.address = "北京";
o.age = 30;
Intent intent = new Intent(this,Main2Activity.class);
//调用对象的封装数据包裹方法
intent.putExtra("obj",o);
startActivity(intent);
}
3.接收:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textView = (TextView) findViewById(R.id.msg);
//拿到Intent,但不是同一个,但内容是一样的
Intent intent = getIntent();
//会自动调用对象中的读数据的方法
Obj o = intent.getParcelableExtra("obj");
textView.setText(o.toString());
}
实现读取通讯录数据:
通讯录数据
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity3">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
public class MainActivity3 extends Activity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
listView = (ListView) findViewById(R.id.listView);
//准备数据
final String[] numbers = {"123","234","345454","34435","43534"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_list_item_1,numbers
);
listView.setAdapter(adapter);
//列表单击事件
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//选择的电话号码
String number = numbers[position];
//存放数据
Intent intent = new Intent();
intent.putExtra("number",number);
//设置返回的结果
setResult(RESULT_OK,intent);
finish();//结束当前的界面
}
});
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.w7.myapplication.MainActivity4">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/number1"
android:layout_above="@+id/button"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:hint="请输入电话号码"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="selectClick"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_alignEnd="@+id/button2"
android:layout_marginTop="56dp"
android:onClick="selectClick"
android:layout_gravity="right" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="callClick"
android:id="@+id/button4"
android:layout_below="@+id/button"
android:layout_alignParentStart="true"
android:layout_marginTop="123dp"
android:onClick="aa"
/>
</LinearLayout>
public class MainActivity4 extends Activity {
private static final int REQUESTCODE_1 = 0X1;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
editText = (EditText)findViewById(R.id.number1);
}
//选择一个电话号码
public void selectClick(View v){
//在哪个页面选择电话号码
Intent intent = new Intent(this,MainActivity3.class);
//请求编码,区分不现的返回结果
startActivityForResult(intent, REQUESTCODE_1);
}
//重写方法来处理返回的结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//请求标记和返回成功的标记
if(requestCode == REQUESTCODE_1 && resultCode==RESULT_OK){
//取出数据
String number = data.getStringExtra("number");
editText.setText(number);
}
}
//打电话
public void aa(View v){
String number = editText.getText().toString();
Toast.makeText(this,number,Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + number));
startActivity(intent);
//设置打电话的权限
//程序配置权限:
// <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
}
}
清单配置实理:
横屏:
android:screenOrientation="landscape"
竖屏:
android:screenOrientation="portrait"
代码实现:要写在实例化布局前面
横屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
竖屏: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main6);
//设置全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//去标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main6);
通过清单文件主题,实现
android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen"
将activity设置为窗体模式
android:theme="@android:style/Theme.DeviceDefault.Dialog"
屏幕方向自动切换:
/**
*
*
* 屏幕切换时默认情况下会重新创建Activity
* 为了保存当前Activity的状态,我们可以重写onSaveInstanceState方法来保存相关的数据
*
* 然后在onCreate 方法还原数据
*
* */
public class MainActivity6 extends Activity {
int index = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main6);
//如果不为空,则取出数据,还原状态值
if(savedInstanceState !=null){
index = savedInstanceState.getInt("index",0);//没有值时,默认为0
}
}
public void change(View v){
index++;
Log.i("index",index+"");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.i("save","save");
//屏幕切换时,将数据保存下来
outState.putInt("index",index);
}
}
public class MainActivity6 extends Activity {
int index = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main6);
}
public void change(View v){
index++;
Log.i("index",index+"");
}
//在清单中定义 android:configChanges="orientation|screenSize",
// 当屏幕更改时触发这个方法,并且不会调用onCreate
//可以利用这个方法来保存数据,所以就不需要保存数据状态了,来提高效率
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.i("change","change");
//index的值在不断的累加,而不在重新计算
}
}
自动选择布局文件是横屏时,用横屏布局,竖屏时,用竖屏布局文件:
1.新建文件夹:layout-land :横屏布局
public class MainActivity extends Activity {
private SharedPreferences sp;
private EditText editText_msg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText_msg = (EditText) findViewById(R.id.editText_msg);
//获取当前程序的SharedPreferences对象
sp = getSharedPreferences("msg", Context.MODE_PRIVATE);
}
//还原数据,当用户回到本activity时还原数据
@Override
protected void onResume() {
super.onResume();
editText_msg.setText(sp.getString("msg",""));
}
//我们在该事件方法里存储数据
//当用户打开其它Activity时,本activity会进入这个状态,所以在这个状态中保存存数据
@Override
protected void onPause() {
super.onPause();
String msg = editText_msg.getText().toString();
if(TextUtils.isEmpty(msg)){
return;
}
SharedPreferences.Editor editor = sp.edit();
editor.putString("msg",msg);
editor.commit();
}
}