fragment生命周期自我学习

今天学习了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();
	

	@Override
	protected 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;
	}
	@Override
	public 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 {
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		System.out.println("fragment开始onAttach……");
		MainActivity.showToast("onAttach...");
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		System.out.println("fragment开始onCreate……");
		MainActivity.showToast("onCreate...");
	}
	@Override
	public 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;
		
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		System.out.println("fragment开始onActivityCreated……");
		MainActivity.showToast("onActivityCreated...");
	}
	@Override
	public void onStart() {
		super.onStart();
		System.out.println("fragment开始onStart……");
		MainActivity.showToast("onStart...");
	}
	@Override
	public void onResume() {
		super.onResume();
		System.out.println("fragment开始onResume……");
		MainActivity.showToast("onResume...");
	}
	@Override
	public void onPause() {
		super.onPause();
		System.out.println("fragment开始onPause……");
		MainActivity.showToast("onPause...");
	}
	@Override
	public void onStop() {
		super.onStop();
		System.out.println("fragment开始onStop……");
		MainActivity.showToast("onStop...");
	}
	@Override
	public void onDestroyView() {
		super.onDestroyView();
		System.out.println("fragment开始onDestroyView……");
		MainActivity.showToast("onDestroyView...");
	}
	@Override
	public void onDestroy() {
		super.onDestroy();
		System.out.println("fragment开始onDestroy……");
		MainActivity.showToast("onDestroy...");
	}
	@Override
	public 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 {
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		System.out.println("fragment开始onAttach……");
		MainActivity.showToast("onAttach...");
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		System.out.println("fragment开始onCreate……");
		MainActivity.showToast("onCreate...");
	}
	@Override
	public 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;
		
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		System.out.println("fragment开始onActivityCreated……");
		MainActivity.showToast("onActivityCreated...");
	}
	@Override
	public void onStart() {
		super.onStart();
		System.out.println("fragment开始onStart……");
		MainActivity.showToast("onStart...");
	}
	@Override
	public void onResume() {
		super.onResume();
		System.out.println("fragment开始onResume……");
		MainActivity.showToast("onResume...");
	}
	@Override
	public void onPause() {
		super.onPause();
		System.out.println("fragment开始onPause……");
		MainActivity.showToast("onPause...");
	}
	@Override
	public void onStop() {
		super.onStop();
		System.out.println("fragment开始onStop……");
		MainActivity.showToast("onStop...");
	}
	@Override
	public void onDestroyView() {
		super.onDestroyView();
		System.out.println("fragment开始onDestroyView……");
		MainActivity.showToast("onDestroyView...");
	}
	@Override
	public void onDestroy() {
		super.onDestroy();
		System.out.println("fragment开始onDestroy……");
		MainActivity.showToast("onDestroy...");
	}
	@Override
	public 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>


源码下载地址:

点击打开链接











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值