您的位置:首页 > 其它

fragment生命周期自我学习

2014-10-15 11:33 288 查看
今天学习了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"
>&
4000
lt;/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>


源码下载地址:
点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  fragment 生命周期