@linux1s1s
2016-11-17T15:45:00.000000Z
字数 5397
阅读 1824
AndroidRefine
2016-11
系列博文:
Android 生命周期 - Activity
Android 生命周期 - Service
Android 生命周期 - BroadcastReceiver
Android 生命周期 - Fragment
Android 生命周期 - View
直观的看一下Fragment的生命周期
public static MyFragment newInstance(int index){
MyFragment mf = new MyFragment();
Bundle args = new Bundle();
args.putInt("index",index);
mf.setArguments(args);
return mf;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle args = getArguments();
if (args != null) {
mLabel = args.getCharSequence("label", mLabel);
}
}
View v = inflater.inflate(R.layout.hello_world, container, false);
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="fresco.mutex.com.lifecycle.MainActivity">
<Button
android:id="@+id/fragment_main_jump"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Jump SecondActivity Now!"
android:textSize="20dp" />
<Button
android:id="@+id/fragment_main_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/fragment_main_jump"
android:layout_centerInParent="true"
android:layout_marginTop="20dp"
android:text="Send BroadCasatReceiver Now!"
android:textSize="20dp" />
<fresco.mutex.com.lifecycle.BCView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_below="@id/fragment_main_send"
android:layout_centerInParent="true"
android:background="@android:color/holo_blue_light"
android:visibility="gone" />
</RelativeLayout>
public class MainFragment extends Fragment implements View.OnClickListener {
private Context mContext;
public static MainFragment newInstance() {
return new MainFragment();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
mContext = context;
Log.i(MainActivity.TAG, "MainFragment: onAttach(Context context)");
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Log.i(MainActivity.TAG, "MainFragment: onAttach(Activity activity)");
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(MainActivity.TAG, "MainFragment: onCreate(@Nullable Bundle savedInstanceState)");
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Log.i(MainActivity.TAG, "MainFragment: onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)");
return inflater.inflate(R.layout.fragment_main, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.i(MainActivity.TAG, "MainFragment: onViewCreated(View view, @Nullable Bundle savedInstanceState)");
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i(MainActivity.TAG, "MainFragment: onActivityCreated(@Nullable Bundle savedInstanceState)");
initContentView();
}
private void initContentView() {
final View rootView = getView();
if (rootView == null)
return;
rootView.findViewById(R.id.fragment_main_send).setOnClickListener(this);
rootView.findViewById(R.id.fragment_main_jump).setOnClickListener(this);
}
@Override
public void onStart() {
super.onStart();
Log.i(MainActivity.TAG, "MainFragment: onStart()");
}
@Override
public void onResume() {
super.onResume();
Log.i(MainActivity.TAG, "MainFragment: onResume()");
}
@Override
public void onPause() {
Log.i(MainActivity.TAG, "MainFragment: onPause()");
super.onPause();
}
@Override
public void onStop() {
Log.i(MainActivity.TAG, "MainFragment: onStop()");
super.onStop();
}
@Override
public void onDestroyView() {
Log.i(MainActivity.TAG, "MainFragment: onDestroyView()");
super.onDestroyView();
}
@Override
public void onDestroy() {
Log.i(MainActivity.TAG, "MainFragment: onDestroy()");
super.onDestroy();
}
@Override
public void onDetach() {
Log.i(MainActivity.TAG, "MainFragment: onDetach()");
super.onDetach();
}
public void jumpNow() {
startActivity(new Intent(mContext, SecondActivity.class));
}
public void sendNow() {
Intent intent = new Intent("fresco.mutex.com.lifecycle.RECEIVER_FOR_TEST");
mContext.sendBroadcast(intent);
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.fragment_main_send:
sendNow();
break;
case R.id.fragment_main_jump:
jumpNow();
break;
default:
break;
}
}
}