一些基础忘记了;写了个简单的demo记录下;
简单记录下Activity和Fragment生命周期执行顺序
Activity和Fragment生命周期图
写了个demo打印了下:
E/FFFF==>>>OneActivity: onCreate
E/FFFF==>>>FragmentOne: onAttach
E/FFFF==>>>FragmentOne: onCreate
E/FFFF==>>>FragmentOne: onCreateView
E/FFFF==>>>FragmentOne: onActivityCreated
E/FFFF==>>>FragmentOne: onStart
E/FFFF==>>>OneActivity: onStart
E/FFFF==>>>OneActivity: onResume
E/FFFF==>>>FragmentOne: onResume
E/FFFF==>>>FragmentOne: onPause
E/FFFF==>>>OneActivity: onPause
E/FFFF==>>>TwoActivity: onCreate
E/FFFF==>>>FragmentTwo: onAttach
E/FFFF==>>>FragmentTwo: onCreate
E/FFFF==>>>FragmentTwo: onCreateView
E/FFFF==>>>FragmentTwo: onActivityCreated
E/FFFF==>>>FragmentTwo: onStart
E/FFFF==>>>TwoActivity: onStart
E/FFFF==>>>TwoActivity: onResume
E/FFFF==>>>FragmentTwo: onResume
E/FFFF==>>>FragmentOne: onStop
E/FFFF==>>>OneActivity: onStop
E/FFFF==>>>FragmentTwo: onPause
E/FFFF==>>>TwoActivity: onPause
E/FFFF==>>>OneActivity: onRestart
E/FFFF==>>>FragmentOne: onStart
E/FFFF==>>>OneActivity: onStart
E/FFFF==>>>OneActivity: onResume
E/FFFF==>>>FragmentOne: onResume
E/FFFF==>>>FragmentTwo: onStop
E/FFFF==>>>TwoActivity: onStop
E/FFFF==>>>FragmentTwo: onDestroyView
E/FFFF==>>>FragmentTwo: onDestroy
E/FFFF==>>>FragmentTwo: onDetach
E/FFFF==>>>TwoActivity: onDestroy
以下为代码,代码很简单~
public class OneActivity extends AppCompatActivity {
private final static String TAG="FFFF==>>>OneActivity";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
Log.e(TAG, "onCreate");
// Create a new Fragment to be placed in the activity layout
FragmentActiv firstFragment = new FragmentActiv();
// In case this activity was started with special instructions from an
// Intent, pass the Intent's extras to the fragment as arguments
// firstFragment.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.one_1, firstFragment).commit();
}
@Override
protected void onRestart() {
super.onRestart();
Log.e(TAG, "onRestart");
}
@Override
protected void onStart() {
super.onStart();
Log.e(TAG, "onStart");
}
@Override
protected void onResume() {
super.onResume();
Log.e(TAG, "onResume");
}
@Override
protected void onStop() {
super.onStop();
Log.e(TAG, "onStop");
}
@Override
protected void onPause() {
super.onPause();
Log.e(TAG, "onPause");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.e(TAG, "onDestroy");
}
public void doLoginAction(View view) {
startActivity(new Intent(OneActivity.this,TwoActivity.class));
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="[http://schemas.android.com/apk/res-auto](http://schemas.android.com/apk/res-auto)"
xmlns:tools="[http://schemas.android.com/tools](http://schemas.android.com/tools)"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/one_1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="One!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:onClick="doLoginAction"/>
</androidx.constraintlayout.widget.ConstraintLayout>
public class TwoActivity extends AppCompatActivity {
private final static String TAG="FFFF==>>>TwoActivity";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
Log.e(TAG, "onCreate");
// Create a new Fragment to be placed in the activity layout
FragmentBctiv firstFragment = new FragmentBctiv();
// In case this activity was started with special instructions from an
// Intent, pass the Intent's extras to the fragment as arguments
// firstFragment.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.two_2, firstFragment).commit();
}
@Override
protected void onRestart() {
super.onRestart();
Log.e(TAG, "onRestart");
}
@Override
protected void onStart() {
super.onStart();
Log.e(TAG, "onStart");
}
@Override
protected void onResume() {
super.onResume();
Log.e(TAG, "onResume");
}
@Override
protected void onStop() {
super.onStop();
Log.e(TAG, "onStop");
}
@Override
protected void onPause() {
super.onPause();
Log.e(TAG, "onPause");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.e(TAG, "onDestroy");
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="[http://schemas.android.com/apk/res-auto](http://schemas.android.com/apk/res-auto)"
xmlns:tools="[http://schemas.android.com/tools](http://schemas.android.com/tools)"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/two_2">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Two!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:onClick="doLoginAction"/>
</androidx.constraintlayout.widget.ConstraintLayout>
public class FragmentOne extends Fragment {
private final static String TAG="FFFF==>>>FragmentOne";
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
Log.e(TAG, "onAttach");
// getArguments();
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e(TAG, "onCreate");
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Log.e(TAG, "onCreateView");
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.e(TAG, "onActivityCreated");
}
@Override
public void onStart() {
super.onStart();
Log.e(TAG, "onStart");
}
@Override
public void onResume() {
super.onResume();
Log.e(TAG, "onResume");
}
@Override
public void onPause() {
super.onPause();
Log.e(TAG, "onPause");
}
@Override
public void onStop() {
super.onStop();
Log.e(TAG, "onStop");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.e(TAG, "onDestroyView");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e(TAG, "onDestroy");
}
@Override
public void onDetach() {
super.onDetach();
Log.e(TAG, "onDetach");
}
}
public class FragmentTwo extends Fragment {
private final static String TAG="FFFF==>>>FragmentTwo";
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
Log.e(TAG, "onAttach");
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e(TAG, "onCreate");
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Log.e(TAG, "onCreateView");
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.e(TAG, "onActivityCreated");
}
@Override
public void onStart() {
super.onStart();
Log.e(TAG, "onStart");
}
@Override
public void onResume() {
super.onResume();
Log.e(TAG, "onResume");
}
@Override
public void onPause() {
super.onPause();
Log.e(TAG, "onPause");
}
@Override
public void onStop() {
super.onStop();
Log.e(TAG, "onStop");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.e(TAG, "onDestroyView");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e(TAG, "onDestroy");
}
@Override
public void onDetach() {
super.onDetach();
Log.e(TAG, "onDetach");
}
}