Android四大组件之Activity & Fragement
1.Activity的生命周期
从onResume到onStop之前,为活动周期。
从onStart到onStop为可见周期
从onCreate到onDestory为完整生命周期。
进入onStop之后,Activity可以被销毁。
2.Fragement的生命周期
3.Activity & Fragement的关系
编译运行,启动程序打印日志如下:
切换到后台,日志如下:
从后台切换到前台,日志如下:
切换到其他的activity,日志如下:
从其他的activity切换回来,日志如下:
按home键退出,日志如下:
4.如何定义使用Fragement
package com.joyfulmath.android4example.fragement; import com.joyfulmath.android4example.R; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; import android.util.Log; import android.view.Window; public class FragementActivity extends Activity implements onClickResult { private static final String TAG = "framentdemo.FragementActivity"; TitleFragment mTitleFragment; DetailFragment mDetailFragmentA; DetailFragment mDetailFragmentB; FragmentManager fm = getFragmentManager(); /* * (non-Javadoc) * * @see android.app.Activity#onCreate(android.os.Bundle) */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "[onCreate]"); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.layout_fragement_main); setDefaultFragment(); } /* * (non-Javadoc) * * @see android.app.Activity#onStart() */ @Override protected void onStart() { super.onStart(); } /* * (non-Javadoc) * * @see android.app.Activity#onResume() */ @Override protected void onResume() { super.onResume(); } /* * (non-Javadoc) * * @see android.app.Activity#onPause() */ @Override protected void onPause() { super.onPause(); } /* * (non-Javadoc) * * @see android.app.Activity#onStop() */ @Override protected void onStop() { super.onStop(); } /* * (non-Javadoc) * * @see android.app.Activity#onDestroy() */ @Override protected void onDestroy() { super.onDestroy(); } private void setDefaultFragment() { Log.d(TAG, "[setDefaultFragment]"); FragmentTransaction transaction = fm.beginTransaction(); mTitleFragment = new TitleFragment(); mTitleFragment.setmClicklistener(this); mTitleFragment.SetHighLight(0); transaction.replace(R.id.title, mTitleFragment); mDetailFragmentA = new DetailFragment(); mDetailFragmentA.setName("detail_A"); transaction.replace(R.id.detail, mDetailFragmentA); // transaction.addToBackStack("detail_A"); transaction.commit(); } @Override public void onClickIndex(int index) { Log.d(TAG, "[onClickIndex] index:" + index); FragmentTransaction transaction = fm.beginTransaction(); switch (index) { case 0: mDetailFragmentA = new DetailFragment(); mDetailFragmentA.setName("detail_A"); transaction.replace(R.id.detail, mDetailFragmentA); transaction.addToBackStack("detail_A"); break; case 1: mDetailFragmentB = new DetailFragment(); mDetailFragmentB.setName("detail_B"); transaction.replace(R.id.detail, mDetailFragmentB); transaction.addToBackStack("detail_B"); break; } transaction.commit(); } }
package com.joyfulmath.android4example.fragement; import com.joyfulmath.android4example.R; import android.app.Fragment; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class DetailFragment extends Fragment { private static final String TAG = "framentdemo.DetailFragment"; private String name; TextView mDetailText; /* (non-Javadoc) * @see android.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle) */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.d(TAG, "[onCreateView] "); View view = inflater.inflate(R.layout.layout_detail_fragment, container, false); mDetailText = (TextView) view.findViewById(R.id.detail_text); return view; } /* (non-Javadoc) * @see android.app.Fragment#onStart() */ @Override public void onStart() { super.onStart(); mDetailText.setText(name); } /* (non-Javadoc) * @see android.app.Fragment#onSaveInstanceState(android.os.Bundle) */ @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); } /* (non-Javadoc) * @see android.app.Fragment#onStop() */ @Override public void onStop() { super.onStop(); Log.d(TAG, "[onStop] name:"+name); } /* (non-Javadoc) * @see android.app.Fragment#onDestroyView() */ @Override public void onDestroyView() { super.onDestroyView(); Log.d(TAG, "[onDestroyView] name:"+name); } /** * @param name the name to set */ public void setName(String name) { Log.d(TAG, "[setName] name:"+name); this.name = name; } }
package com.joyfulmath.android4example.fragement; import com.joyfulmath.android4example.R; import android.app.Fragment; import android.graphics.Color; import android.os.Bundle; 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 TitleFragment extends Fragment implements OnClickListener { private static final String TAG = "framentdemo.TitleFragment"; private Button btnA; private Button btnB; private int mHighIndex = 0; onClickResult mClicklistener; /** * @param mClicklistener the mClicklistener to set */ public void setmClicklistener(onClickResult mClicklistener) { this.mClicklistener = mClicklistener; } /* (non-Javadoc) * @see android.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle) */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.d(TAG, "[onCreateView] "); View view = inflater.inflate(R.layout.layout_title_fragment, container, false); btnA = (Button) view.findViewById(R.id.btn_a); btnB = (Button) view.findViewById(R.id.btn_b); btnA.setOnClickListener(this); btnB.setOnClickListener(this); return view; } /* (non-Javadoc) * @see android.app.Fragment#onStart() */ @Override public void onStart() { setHighIndex(); super.onStart(); } /* (non-Javadoc) * @see android.app.Fragment#onSaveInstanceState(android.os.Bundle) */ @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); } /* (non-Javadoc) * @see android.app.Fragment#onStop() */ @Override public void onStop() { super.onStop(); } /* (non-Javadoc) * @see android.app.Fragment#onDestroyView() */ @Override public void onDestroyView() { super.onDestroyView(); } @Override public void onClick(View v) { ShowFragmentDetail(v.getId()); } private void ShowFragmentDetail(int id) { switch(id) { case R.id.btn_a: SetHighLight(0); setHighIndex(); if(mClicklistener!=null) { mClicklistener.onClickIndex(0); } break; case R.id.btn_b: SetHighLight(1); setHighIndex(); if(mClicklistener!=null) { mClicklistener.onClickIndex(1); } break; } } public void SetHighLight(int index) { mHighIndex = index; } private void setHighIndex() { switch(mHighIndex) { case 0: btnA.setTextColor(Color.BLUE); btnB.setTextColor(Color.BLACK); break; case 1: btnA.setTextColor(Color.BLACK); btnB.setTextColor(Color.BLUE); break; } } }
package com.joyfulmath.android4example.fragement; public interface onClickResult { void onClickIndex(int index); }
5.多个Fragement之间如何切换。
using transaction.replace(R.id.detail, mDetailFragmentB);
6.Avtivity 启动其他Activity & Service & BroadCast。
1).activity.startactivity
2).activity.startservice
3).activity.sendbroadcast/context.sendbroadcast.
posted on 2015-01-16 14:36 Joyfulmath 阅读(711) 评论(0) 编辑 收藏 举报