活动的生命期
1.onCreate():活动第一次创建时调用
2.onStart():活动在由不可见变为可见时调用
3.onResume():活动准备好与用户交互时调用
4.onPause():系统在准备启动别的活动时调用
5.onStop():活动完全不可见时调用
6.onDestroy():活动销毁时调用
7.onRestart():活动由停止状态变为运行状态前调用
当一个活动的上面出现的对话框之类的没完全把当前活动盖住的情况,活动就处于停止状态,启动的是onPause()方法
创建一个对话框式的活动,只需在AndroidManifest.xml中加入如下配置
<activity android:name=".lifecycle.DialogActivity"
android:theme="@style/Theme.AppCompat.Dialog"></activity>
android:theme用于给当前活动指定主题,安卓系统内置的由很多主题,这里我们使用Dialog主题
具体实现生命周期代码:
Activity:
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.example.the2.MainActivity;
import com.example.the2.R;
public class Activity extends AppCompatActivity implements View.OnClickListener {
private Button btn1,btn2;
private static final String TAG = "Activity";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG,"onCreate");
setContentView(R.layout.activity_activity);
initView();
}
private void initView() {
btn1=findViewById(R.id.activity_btn1);
btn2=findViewById(R.id.activity_btn2);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.activity_btn1:
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
break;
case R.id.activity_btn2:
intent = new Intent(this, DialogActivity.class);
startActivity(intent);
default:
}
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG,"onStart");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG,"onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG,"onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG,"onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG,"onDestroy");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG,"onRestart");
}
}
activity_activity:
<?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/activity_btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MainActivity"/>
<Button
android:id="@+id/activity_btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DialogActivity"/>
</LinearLayout>
其中MainActivity和DialogActivity中没东西
活动回收
当活动进入停止状态时,可能是会被系统回收的,被回收之后若用户点击了back键还是会打开被回收的活动,但前面活动中例如对话框里打的字之类的临时数据会被清除,要想避免出现这种情况,就需要用到android中的**onSaveInstanceState()**回调方法,这个方法可以保证活动被回收之前一定会被调用,因此我们可以使用这个方法来解决临时数据得不到保存的问题。
onSaveInstanceState()方法会携带一个Bundle类型的参数,Bundle提供了一系列的方法用于保存数据
例如
putString()方法保存字符串
putInt()方法保存整型数据,以此类推。
每个保存方法中需要传入两个参数值,第一个参数是键,用于后面从Bundle中取值,第二个参数是真正保存的内容
保存数据:
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
String txt = "you just typed";
outState.putString("data",txt);
}
保存的数据都会保存到onCreate()方法中的Bundle参数中
取出数据:
if (savedInstanceState != null){
String txt1 = savedInstanceState.getString(“data”);
Log.d(“IntentActivity”,txt1);
}