Fragment 生命周期
代码:
package com.example.mepositry.fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.example.mepositry.FirstActivity;
import com.example.mepositry.R;
public class CommunityFragment extends Fragment {
public static CommunityFragment newInstance(String info) {
Bundle args = new Bundle();
CommunityFragment fragment = new CommunityFragment();
args.putString("info", info);
fragment.setArguments(args);
return fragment;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_community, null);
Button button = view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), FirstActivity.class);
startActivity(intent);
}
});
Log.e("Fragment","onCreateView");
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
Log.e("Fragment","onAttach");
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Fragment","onCreate");
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.e("Fragment","onActivityCreated");
}
@Override
public void onStart() {
super.onStart();
Log.e("Fragment","onStart");
}
@Override
public void onResume() {
super.onResume();
Log.e("Fragment","onResume");
}
@Override
public void onPause() {
super.onPause();
Log.e("Fragment","onPause");
}
@Override
public void onStop() {
super.onStop();
Log.e("Fragment","onStop");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.e("Fragment","onDestroyView");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e("Fragment","onDestroy");
}
@Override
public void onDetach() {
super.onDetach();
Log.e("Fragment","onDetach");
}
}
step1:第一次进来
2021-05-21 15:14:12.698 8344-8344/com.example.mepositry E/Fragment: onAttach
2021-05-21 15:14:12.698 8344-8344/com.example.mepositry E/Fragment: onCreate
2021-05-21 15:14:12.722 8344-8344/com.example.mepositry E/Fragment: onCreateView
2021-05-21 15:14:12.729 8344-8344/com.example.mepositry E/Fragment: onActivityCreated
2021-05-21 15:14:12.729 8344-8344/com.example.mepositry E/Fragment: onStart
2021-05-21 15:14:12.729 8344-8344/com.example.mepositry E/Fragment: onResume
step2:跳转到其他页面
2021-05-21 15:15:08.830 8344-8344/com.example.mepositry E/Fragment: onPause
2021-05-21 15:15:09.494 8344-8344/com.example.mepositry E/Fragment: onStop
step3:按back键,再回来刚才的页面
2021-05-21 15:15:36.390 8344-8344/com.example.mepositry E/Fragment: onStart
2021-05-21 15:15:36.392 8344-8344/com.example.mepositry E/Fragment: onResume
step4:退出
2021-05-21 15:16:02.794 8344-8344/com.example.mepositry E/Fragment: onPause
2021-05-21 15:16:03.352 8344-8344/com.example.mepositry E/Fragment: onStop
2021-05-21 15:16:03.354 8344-8344/com.example.mepositry E/Fragment: onDestroyView
2021-05-21 15:16:03.355 8344-8344/com.example.mepositry E/Fragment: onDestroy
2021-05-21 15:16:03.355 8344-8344/com.example.mepositry E/Fragment: onDetach
按下home键盘和跳转到其他页面,执行的生命周期相同
end