文章目录
- Intro
- log 及结论
- 代码
Intro
界面设计:点击左侧按钮,会将右侧 青色的RightFragment
替换成 黄色的AnotherRightFragment
,而这两个 Fragment 的生命周期方法都会打印日志。
所以只要看执行结果中的日志,就可以知道 Fragment 的不同生命周期方法的执行顺序。
点击按钮后,右侧的Fragment被替换:
log 及结论
2024-03-24 20:40:26.903 FragmentUsageTestActivi W Fragment的生命周期:=====》切换Fragment
2024-03-24 20:40:26.916 RightFragment D Fragment的生命周期: RightFragment onAttach:
2024-03-24 20:40:26.916 RightFragment D Fragment的生命周期: RightFragment onCreate:
2024-03-24 20:40:26.917 RightFragment D Fragment的生命周期: RightFragment onActivityCreated:
2024-03-24 20:40:26.919 RightFragment D Fragment的生命周期: RightFragment onStart:
2024-03-24 20:40:26.921 RightFragment D Fragment的生命周期: RightFragment onResume:
2024-03-24 20:40:38.070 FragmentUsageTestActivi W Fragment的生命周期:=====》切换Fragment
2024-03-24 20:40:38.081 RightFragment D Fragment的生命周期: RightFragment onPause:
2024-03-24 20:40:38.081 RightFragment D Fragment的生命周期: RightFragment onStop:
2024-03-24 20:40:38.082 AnotherRightFragment D Fragment的生命周期: AnotherRightFragment onAttach:
2024-03-24 20:40:38.082 AnotherRightFragment D Fragment的生命周期: AnotherRightFragment onCreate:
2024-03-24 20:40:38.087 AnotherRightFragment D Fragment的生命周期: AnotherRightFragment onActivityCreated:
2024-03-24 20:40:38.088 AnotherRightFragment D Fragment的生命周期: AnotherRightFragment onStart:
2024-03-24 20:40:38.090 RightFragment D Fragment的生命周期: RightFragment onDestroyView:
2024-03-24 20:40:38.091 AnotherRightFragment D Fragment的生命周期: AnotherRightFragment onResume:
2024-03-24 20:40:42.935 FragmentUsageTestActivi W Fragment的生命周期:=====》点击了【返回】按钮 (onBackPressed)
2024-03-24 20:40:42.936 AnotherRightFragment D Fragment的生命周期: AnotherRightFragment onPause:
2024-03-24 20:40:42.937 AnotherRightFragment D Fragment的生命周期: AnotherRightFragment onStop:
2024-03-24 20:40:42.939 RightFragment D Fragment的生命周期: RightFragment onActivityCreated:
2024-03-24 20:40:42.939 RightFragment D Fragment的生命周期: RightFragment onStart:
2024-03-24 20:40:42.940 AnotherRightFragment D Fragment的生命周期: AnotherRightFragment onDestroyView:
2024-03-24 20:40:42.940 AnotherRightFragment D Fragment的生命周期: AnotherRightFragment onDestroy:
2024-03-24 20:40:42.940 AnotherRightFragment D Fragment的生命周期: AnotherRightFragment onDetach:
2024-03-24 20:40:42.940 RightFragment D Fragment的生命周期: RightFragment onResume:
2024-03-24 20:40:44.765 FragmentUsageTestActivi W Fragment的生命周期:=====》点击了【返回】按钮 (onBackPressed)
2024-03-24 20:40:44.766 RightFragment D Fragment的生命周期: RightFragment onPause:
2024-03-24 20:40:44.767 RightFragment D Fragment的生命周期: RightFragment onStop:
2024-03-24 20:40:44.768 RightFragment D Fragment的生命周期: RightFragment onDestroyView:
2024-03-24 20:40:44.769 RightFragment D Fragment的生命周期: RightFragment onDestroy:
2024-03-24 20:40:44.769 RightFragment D Fragment的生命周期: RightFragment onDetach:
2024-03-24 20:40:48.307 FragmentUsageTestActivi W Fragment的生命周期:=====》点击了【返回】按钮 (onBackPressed)
结论与 《第一行代码》中所述一致:
代码
可以查看以下三个提交点(改动一样, gitee网速最快,不过三个网站界面有不同,看你习惯看哪个):
gitee: https://gitee.com/wuyujin1997/android-demo/commit/0d75711e244b7c59ed009326b8174bedcb4caff8
github: https://github.com/wuyujin1997/AndroidDemo/commit/0d75711e244b7c59ed009326b8174bedcb4caff8
bitbucket: https://bitbucket.org/wuyujin1997/androiddemo/commits/0d75711e244b7c59ed009326b8174bedcb4caff8
主要的生命周期方法日志类如下(其他的就不贴了,见上 git commit link):
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class BaseFragmentForLifecycleLog extends Fragment {
private String TAG;
public BaseFragmentForLifecycleLog() {
TAG = this.getClass().getSimpleName();
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
Log.d(TAG, "Fragment的生命周期: " + TAG + " onAttach: ");
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "Fragment的生命周期: " + TAG + " onCreate: ");
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Log.d(TAG, "Fragment的生命周期: " + TAG + " onCreateView: ");
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d(TAG, "Fragment的生命周期: " + TAG + " onActivityCreated: ");
}
@Override
public void onStart() {
super.onStart();
Log.d(TAG, "Fragment的生命周期: " + TAG + " onStart: ");
}
@Override
public void onResume() {
super.onResume();
Log.d(TAG, "Fragment的生命周期: " + TAG + " onResume: ");
}
@Override
public void onPause() {
super.onPause();
Log.d(TAG, "Fragment的生命周期: " + TAG + " onPause: ");
}
@Override
public void onStop() {
super.onStop();
Log.d(TAG, "Fragment的生命周期: " + TAG + " onStop: ");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.d(TAG, "Fragment的生命周期: " + TAG + " onDestroyView: ");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "Fragment的生命周期: " + TAG + " onDestroy: ");
}
@Override
public void onDetach() {
super.onDetach();
Log.d(TAG, "Fragment的生命周期: " + TAG + " onDetach: ");
}
}