Frgment生命周期

Fragment生命周期:
        与Actiivity类似,Fragment也存在如下状态
            运行状态:Fragment获得焦点,用户可见
                  1.添加Fragment
09-22 11:06:53.639 21950-21950/testpermission.fragmentliftcycle D/Test-LifecycleFragment: ---onAttach---
09-22 11:06:53.644 21950-21950/testpermission.fragmentliftcycle D/Test-LifecycleFragment: ---onCreate---
09-22 11:06:53.644 21950-21950/testpermission.fragmentliftcycle D/Test-LifecycleFragment: ---onCreateView---
09-22 11:06:53.649 21950-21950/testpermission.fragmentliftcycle D/Test-LifecycleFragment: ---onActivityCreated---
09-22 11:06:53.649 21950-21950/testpermission.fragmentliftcycle D/Test-LifecycleFragment: ---onStart---
09-22 11:06:53.649 21950-21950/testpermission.fragmentliftcycle D/Test-LifecycleFragment: ---onResume--- 运行状态
            状态状态:其他Activity位于前台,Fragment可见但是失去焦点
09-22 11:11:56.614 25118-25118/testpermission.fragmentliftcycle D/Test-LifecycleFragment: ---onPause---
09-22 11:11:56.644 25118-25118/testpermission.fragmentliftcycle D/Test-LifecycleFragment: ---启动了窗口Activity---
            停止状态:该Fragment不可见,失去焦点
09-22 11:13:36.139 25118-25118/testpermission.fragmentliftcycle D/Test-LifecycleFragment: ---onStop---
            销毁状态:该Fragment被完全删除,或该Fragment所在的Activity被结束
09-22 11:16:02.669 25118-25118/testpermission.fragmentliftcycle D/Test-LifecycleFragment: ---onDestroyView---
09-22 11:16:02.670 25118-25118/testpermission.fragmentliftcycle D/Test-LifecycleFragment: ---onDestroy---
09-22 11:16:02.670 25118-25118/testpermission.fragmentliftcycle D/Test-LifecycleFragment: ---onDetach---

Fragment代码:

package testpermission.fragmentliftcycle;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
* Created by 75213 on 2016/9/22.
*/
public class LifecycleFragment extends Fragment {
final String TAG = "Test-LifecycleFragment";

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Log.d(TAG , "---onAttach---");
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "---onCreate---");
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(TAG , "---onCreateView---");
TextView tv = new TextView(getActivity());
tv.setGravity(Gravity.CENTER_HORIZONTAL);
tv.setText("测试Fragment生命周期");
return tv;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d(TAG, "---onActivityCreated---");
}

@Override
public void onStart() {
super.onStart();
Log.d(TAG, "---onStart---");
}

@Override
public void onResume() {
super.onResume();
Log.d(TAG, "---onResume---");
}

@Override
public void onPause() {
super.onPause();
Log.d(TAG, "---onPause---");
}

@Override
public void onStop() {
super.onStop();
Log.d(TAG, "---onStop---");
}

@Override
public void onDestroyView() {
super.onDestroyView();
Log.d(TAG, "---onDestroyView---");
}

@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "---onDestroy---");
}

@Override
public void onDetach() {
super.onDetach();
Log.d(TAG, "---onDetach---");
}
}


MainActivity代码
package testpermission.fragmentliftcycle;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
private Button bn , bn2 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bn = (Button)findViewById(R.id.bn);
bn2 = (Button)findViewById(R.id.bn2);
bn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LifecycleFragment fragment = new LifecycleFragment();
getFragmentManager().beginTransaction().replace(R.id.fragment , fragment).commit();
}
});
bn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this , WindowActivity.class);
startActivity(intent);
}
});
}
}
原文地址:https://www.cnblogs.com/Rookietake/p/5895593.html