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
- fragment生命周期自我学习
- Android Fragment学习之一、Fragment生命周期
- Fragment基础点滴学习---Fragment的生命周期
- Fragment学习(一) :生命周期
- Fragment生命周期---Android学习笔记
- Android 学习之Fragment生命周期
- 【学习笔记】 Activity/Fragment 生命周期
- Fragment学习笔记——Fragment生命周期和Fragment通信
- Fragment学习-----简介与生命周期研究
- Fragment学习-----简介与生命周期研究
- Fragment学习-----简介与生命周期研究
- 详细学习和理解Fragment的生命周期!
- Fragment学习-----简介与生命周期研究
- android Fragment生命周期和回退栈学习以及Fragment之间通信
- fragment 生命周期
- Fragment生命周期
- Fragment 生命周期
- Fragment生命周期
- create 函数的重写
- android MotionEvent中getX()和getRawX()的区别
- 经典中的品味:第二章 C++基本的对象,类型和值(上)
- 设置python的默认编码为utf8
- VC2005从开发MFC ActiveX ocx控件到发布到.net网站的全部过程
- fragment生命周期自我学习
- ASP.NET水印文字超出图片边界,显示在图片外面
- android中LayoutInflater以及inflate
- Linux MTD下获取Nand flash各个参数的过程的详细解析
- Objective-C中的闭包性和匿名函数
- ios开发小技巧
- MyEclipse启动SVN报错 Failed to load JavaHL Library
- 4.4.2.3 用函数绘图
- setInterval与setTimeout区别详解