fragment生命周期自我学习

来源:互联网 发布:java连接zookeeper集群 编辑:程序博客网 时间:2024/05/22 01:31

今天学习了fragment的生命周期附有自己的代码

fragment的生命周期:

开始:

onAttach

onCreat

onCreatView

onActivityCreated

onStart

onResume

onPause

onStop

onDestoryView

onDestory

onDetach


下面是代码:

首先是MainActivity.java

package com.study.myfragmentdemo;import android.content.Context;import android.content.res.Configuration;import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentActivity;import android.view.View;import android.widget.Button;import android.widget.Toast;public class MainActivity extends FragmentActivity {public static Context mContext;private Fragment fragment1 = new Fragment1();private Fragment fragment2 = new Fragment2();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mContext = getApplicationContext();getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout_control, fragment1).addToBackStack("fragment1").commit();Button bt = (Button) findViewById(R.id.bt);}public static void showToast(String text) {Toast.makeText(mContext, text, Toast.LENGTH_SHORT).show();}// 获取手机横竖屏的方法public boolean isScreenChange() {Configuration mConfiguration = this.getResources().getConfiguration(); // 获取设置的配置信息int ori = mConfiguration.orientation; // 获取屏幕方向if (ori == Configuration.ORIENTATION_LANDSCAPE) {// 横屏return true;} else if (ori == Configuration.ORIENTATION_PORTRAIT) {// 竖屏return false;}return false;}@Overridepublic void onConfigurationChanged(Configuration newConfig) {super.onConfigurationChanged(newConfig);System.out.println("怎么回事?"+"执行了吗?");if (newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE) {getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout_control, fragment2).addToBackStack("fragment1").commit();} else {getSupportFragmentManager().beginTransaction().hide(fragment1).add(R.id.frameLayout_control, fragment1).addToBackStack("fragment2").commit();}}public void btClick(View v){getSupportFragmentManager().beginTransaction().hide(fragment1).add(R.id.frameLayout_control, fragment2).addToBackStack("fragment2").commit();}}
fragment1代码

package com.study.myfragmentdemo;import android.app.Activity;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class Fragment1 extends Fragment {@Overridepublic void onAttach(Activity activity) {super.onAttach(activity);System.out.println("fragment开始onAttach……");MainActivity.showToast("onAttach...");}@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);System.out.println("fragment开始onCreate……");MainActivity.showToast("onCreate...");}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View fragment = null;fragment  =  inflater.inflate(R.layout.fragment1, container, false);System.out.println("fragment开始onCreateView……");MainActivity.showToast("onCreateView...");return fragment;}@Overridepublic void onActivityCreated(Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);System.out.println("fragment开始onActivityCreated……");MainActivity.showToast("onActivityCreated...");}@Overridepublic void onStart() {super.onStart();System.out.println("fragment开始onStart……");MainActivity.showToast("onStart...");}@Overridepublic void onResume() {super.onResume();System.out.println("fragment开始onResume……");MainActivity.showToast("onResume...");}@Overridepublic void onPause() {super.onPause();System.out.println("fragment开始onPause……");MainActivity.showToast("onPause...");}@Overridepublic void onStop() {super.onStop();System.out.println("fragment开始onStop……");MainActivity.showToast("onStop...");}@Overridepublic void onDestroyView() {super.onDestroyView();System.out.println("fragment开始onDestroyView……");MainActivity.showToast("onDestroyView...");}@Overridepublic void onDestroy() {super.onDestroy();System.out.println("fragment开始onDestroy……");MainActivity.showToast("onDestroy...");}@Overridepublic void onDetach() {super.onDetach();System.out.println("fragment开始onDetach……");MainActivity.showToast("onDetach...");}}

fragment2代码

package com.study.myfragmentdemo;import android.app.Activity;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class Fragment2 extends Fragment {@Overridepublic void onAttach(Activity activity) {super.onAttach(activity);System.out.println("fragment开始onAttach……");MainActivity.showToast("onAttach...");}@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);System.out.println("fragment开始onCreate……");MainActivity.showToast("onCreate...");}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View fragment = null;fragment  =  inflater.inflate(R.layout.fragment2, container, false);System.out.println("fragment开始onCreateView……");MainActivity.showToast("onCreateView...");return fragment;}@Overridepublic void onActivityCreated(Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);System.out.println("fragment开始onActivityCreated……");MainActivity.showToast("onActivityCreated...");}@Overridepublic void onStart() {super.onStart();System.out.println("fragment开始onStart……");MainActivity.showToast("onStart...");}@Overridepublic void onResume() {super.onResume();System.out.println("fragment开始onResume……");MainActivity.showToast("onResume...");}@Overridepublic void onPause() {super.onPause();System.out.println("fragment开始onPause……");MainActivity.showToast("onPause...");}@Overridepublic void onStop() {super.onStop();System.out.println("fragment开始onStop……");MainActivity.showToast("onStop...");}@Overridepublic void onDestroyView() {super.onDestroyView();System.out.println("fragment开始onDestroyView……");MainActivity.showToast("onDestroyView...");}@Overridepublic void onDestroy() {super.onDestroy();System.out.println("fragment开始onDestroy……");MainActivity.showToast("onDestroy...");}@Overridepublic void onDetach() {super.onDetach();System.out.println("fragment开始onDetach……");MainActivity.showToast("onDetach...");}}

activity_main.Xml代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />    <FrameLayout         android:id="@+id/frameLayout_control"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        ></FrameLayout>    <Button         android:id="@+id/bt"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="切换"        android:onClick="btClick"        /></LinearLayout>

fragment1.Xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="fragment1" /></RelativeLayout>

fragment2.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="fragment2" /></RelativeLayout>


源码下载地址:

点击打开链接











0 0
原创粉丝点击