Fragment的基本使用技巧之add(1)

FragmentTransaction.add把fragment加入到当前的container里面去,commit之后fragment就会自动执行它的生命周期方法,下面是一个简单的fragment使用方法:

package com.example.fragmentdemo;

import java.util.List;

import com.example.fragmentdemo.fragment.MainFragment;
import com.example.fragmentdemo.listener.BackkeyPressListener;
import com.example.fragmentdemo.listener.FragmentOp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentManager.BackStackEntry;
import android.support.v4.app.FragmentManager.OnBackStackChangedListener;
import android.support.v4.app.FragmentTransaction;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {

	private String TAG = "MainActivity";
	
	private MainActivity mActivity = null;
	private FragmentManager mFragmentManager = null;

	private FrameLayout mContainer = null;
	private TextView titleCenter = null;
	private MainFragment mMainFragment = null;
	private String title = null;
	private FragmentOp mFragmentListener = new FragmentOp() {
		
		@Override
		public void onFragmentReplace(Fragment fragment) {
			FragmentTransaction ft = mFragmentManager.beginTransaction();
			ft.replace(R.id.content_layout, fragment, null);
			ft.commit();
		}
		
		@Override
		public void onFragmentAdded(Fragment fragment) {
			setTitle(fragment);
			FragmentTransaction ft = mFragmentManager.beginTransaction();
			ft.setCustomAnimations(R.anim.push_up_in, R.anim.push_up_out);
			ft.add(R.id.content_layout, fragment, title);
			ft.addToBackStack(fragment.getClass().getName());			
			ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
			ft.commit();
		}
	};
	
	
	private BackkeyPressListener mBackkeyPressListener = new BackkeyPressListener() {
		
		@Override
		public void onBackkeyPress() {
			
			if (0 == mFragmentManager.getBackStackEntryCount()) {
				mActivity.finish();
			} else {
				mFragmentManager.popBackStack();
			}
		}
	};
	
	private OnBackStackChangedListener mOnBackStackChangedListener = new OnBackStackChangedListener() {
		
		@Override
		public void onBackStackChanged() {
			Log.v(TAG, "onBackStackChanged");
			int count = mFragmentManager.getBackStackEntryCount();
			Log.v(TAG, "getBackStackEntryCount = " + count);
			String title = null;
			if (0 == count) {
				title = MainFragment.class.getName();
			} else {
				BackStackEntry entry = mFragmentManager.getBackStackEntryAt(count - 1);
				title = entry.getName();
			}
			titleCenter.setText(title);
			
		}
	};
	
	private void setTitle(Fragment fragment) {
		title = fragment.getClass().getName();
		titleCenter.setText(title);
	}
	
	public FragmentManager getMyFragmentManager() {
		return mFragmentManager;
	}
	
	public BackkeyPressListener getBackkeyPressListener() {
		return mBackkeyPressListener;
	}
	
	/*
	 * onCreateView -> onCreate -> onStart -> onResume -> onResumeFragments -> onAttachFragment -> onPostResume
	 * onPause -> onSaveInstanceState -> onStop
	 * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Log.v(TAG, "onCreate");
		initParams();
		findWidget();
		setListener();
	}
	
	
	private void setListener() {
		mFragmentManager.addOnBackStackChangedListener(mOnBackStackChangedListener);
	}
	private void findWidget() {
		mContainer= (FrameLayout) this.findViewById(R.id.content_layout);
		titleCenter = (TextView) this.findViewById(R.id.title_center);
	}
	
	
	private void initParams() {
		mFragmentManager = this.getSupportFragmentManager();
		mActivity = this;
	}

	@Override
	public void onAttachFragment(Fragment fragment) {
		// TODO 自动生成的方法存根
		super.onAttachFragment(fragment);
		Log.v(TAG, "onAttachFragment");
	}

	@Override
	public void onBackPressed() {
		Log.v(TAG, "onBackPressed");
		if (null != mBackkeyPressListener) {
			mBackkeyPressListener.onBackkeyPress();
		}
	}

	@Override
	public View onCreateView(String name, Context context, AttributeSet attrs) {
		// TODO 自动生成的方法存根
		
		Log.v(TAG, "onCreateView");
		return super.onCreateView(name, context, attrs);
	}

	@Override
	protected void onDestroy() {
		// TODO 自动生成的方法存根
		super.onDestroy();
		Log.v(TAG, "onDestroy");
	}

	@Override
	protected void onNewIntent(Intent intent) {
		// TODO 自动生成的方法存根
		super.onNewIntent(intent);
		Log.v(TAG, "onNewIntent");
	}

	@Override
	protected void onPause() {
		// TODO 自动生成的方法存根
		super.onPause();
		Log.v(TAG, "onPause");
	}

	@Override
	protected void onPostResume() {
		// TODO 自动生成的方法存根
		super.onPostResume();
		Log.v(TAG, "onPostResume");
	}

	@Override
	protected void onResume() {
		super.onResume();
		Log.v(TAG, "onResume");		
		if (null == mMainFragment) {
			mMainFragment = new MainFragment();
			mMainFragment.setFragmentListener(mFragmentListener);
		}
		title = mMainFragment.getClass().getName();
		if (!mMainFragment.isAdded()) {
			FragmentTransaction ft = mFragmentManager.beginTransaction();
			ft.add(R.id.content_layout, mMainFragment, title);
			ft.commit();
		}

		titleCenter.setText(title);
	}

	@Override
	protected void onResumeFragments() {
		// TODO 自动生成的方法存根
		super.onResumeFragments();
		Log.v(TAG, "onResumeFragments");
	}

	@Override
	public Object onRetainCustomNonConfigurationInstance() {
		Log.v(TAG, "onRetainCustomNonConfigurationInstance");
		return super.onRetainCustomNonConfigurationInstance();
		
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO 自动生成的方法存根
		super.onSaveInstanceState(outState);
		Log.v(TAG, "onSaveInstanceState");
	}

	@Override
	protected void onStart() {
		// TODO 自动生成的方法存根
		super.onStart();
		Log.v(TAG, "onStart");
	}

	@Override
	protected void onStop() {
		// TODO 自动生成的方法存根
		super.onStop();
		Log.v(TAG, "onStop");
	}

}
上面是Activity控制中心, 负责控制fragment的add、replace,下面是一个fragment,从中可以看出fragmetn的生命周期及基本使用方法,其实它与一般的UI组件类似,之不过它更灵活,它有自己的生命 控制周期,从这点来看它又像是一个特殊的Activitiy,其实它与Activity差不多,只不过它必须依附于Activity运行,当Activity运行到可交互状态后才能自由的在代码中添加、删除的自己fragment,看代码:

package com.example.fragmentdemo.fragment;

import com.example.fragmentdemo.R;
import com.example.fragmentdemo.listener.FragmentOp;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class MainFragment extends Fragment {

	private String TAG = "MainFragment";

	private View mView = null;
	private Activity mActivity = null;
	private Context mContext = null;
	
	private Button btn = null;
	private FragmentOp listener = null;
	private NestedFragment mNestedFragment = null;
	
	public MainFragment() {
		// TODO 自动生成的构造函数存根
	}
	
	public void setFragmentListener(FragmentOp l) {
		this.listener = l;
	}

	/*
	 * onAttach -> onCreate -> onCreateView -> onActivityCreated -> onStart -> onResume
	 * onPause -> onStop -> onDestoryView -> onDestory -> onDettach
	 * @see android.support.v4.app.Fragment#onActivityCreated(android.os.Bundle)
	 */
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO 自动生成的方法存根
		super.onActivityCreated(savedInstanceState);
		Log.v(TAG, "onActivityCreated");
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO 自动生成的方法存根
		super.onAttach(activity);
		Log.v(TAG, "onAttach");
		this.mActivity = activity;
		this.mContext = activity.getApplicationContext();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO 自动生成的方法存根
		super.onCreate(savedInstanceState);
		Log.v(TAG, "onCreate");
	}

	@Override
	public void onPause() {
		// TODO 自动生成的方法存根
		super.onPause();
		Log.v(TAG, "onPause");
	}

	@Override
	public void onResume() {
		// TODO 自动生成的方法存根
		super.onResume();
		Log.v(TAG, "onResume");
	}

	@Override
	public void onStart() {
		// TODO 自动生成的方法存根
		super.onStart();
		Log.v(TAG, "onStart");
	}

	@Override
	public void onStop() {
		// TODO 自动生成的方法存根
		super.onStop();
		Log.v(TAG, "onStop");
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO 自动生成的方法存根
		Log.v(TAG, "onCreateView");
		this.mView = inflater.inflate(R.layout.main_layout, container, false);

		findWidget();
		setListener();
		return mView;
	}

	private void findWidget() {
		btn = (Button) this.mView.findViewById(R.id.btn);
	}
	
	private void setListener() {
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				if (null == mNestedFragment) {
					mNestedFragment = new NestedFragment();
					mNestedFragment.setFragmentListener(listener);
				}
				if (null != listener) {
					listener.onFragmentAdded(mNestedFragment);
				}
			}
			
		});
	}
	@Override
	public void onDestroy() {
		// TODO 自动生成的方法存根
		super.onDestroy();
		Log.v(TAG, "onDestroy");
	}

	@Override
	public void onDestroyView() {
		// TODO 自动生成的方法存根
		super.onDestroyView();
		Log.v(TAG, "onDestroyView");
	}

	@Override
	public void onDetach() {
		// TODO 自动生成的方法存根
		super.onDetach();
		Log.v(TAG, "onDetach");
	}

}

代码的整个工程连接如下:

源代码

你可能感兴趣的:(Android,Fragment)