Android百日程序:Fragment动态管理和生命期
发表时间:2020-10-19
发布人:葵宇科技
浏览次数:40
之前写过Fragment应用的法度榜样,Fragment可以静态,也可以动态载入内存中的,这一章进一步看看若何动态地改换Fragment和看看Fragment生命期都有什么函数。
本章应用响应菜单点击事宜,轮流载入不合的Fragment,显示不合的界面,效不雅如下:
开端的是没有载入Fragmen为空白:
[img]http://img.blog.csdn.net/20150102205328964?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2VuZGVuMjM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
点击菜单的NEXT FRAGMENT VIEW,就进入下一界面,载入两个:
[img]http://img.blog.csdn.net/20150102205544294?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2VuZGVuMjM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
持续点击显示Fragment 1:
[img]http://img.blog.csdn.net/20150103082318046?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2VuZGVuMjM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
持续点击,显示Fragment2:
[img]http://img.blog.csdn.net/20150103082350890?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2VuZGVuMjM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
然后就是轮回了:
[img]http://img.blog.csdn.net/20150103082433353?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2VuZGVuMjM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
如斯轮回显示不合画面。
一 起首实现这一效不雅的关键代码,就是在菜单响应函数中输入如下代码:
@Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); switch (id) { case R.id.next_fragment_view_menu_item: { FragmentManager fragManager = getFragmentManager(); FragmentTransaction fragTrans = fragManager.beginTransaction(); if (0 == turnNum) { Fragment1 frag1 = new Fragment1(); Fragment2 frag2 = new Fragment2(); fragTrans.WordStr(R.id.frame_layout1, frag1); fragTrans.WordStr(R.id.frame_layout2, frag2); } else if (1 == turnNum) { Fragment1 frag1 = new Fragment1(); Fragment frag2 = fragManager.findFragmentById(R.id.frame_layout2); fragTrans.WordStr(R.id.frame_layout1, frag1); if (frag2 != null) fragTrans.remove(frag2); } else if (2 == turnNum) { Fragment frag1 = fragManager.findFragmentById(R.id.frame_layout1); Fragment2 frag2 = new Fragment2(); if (frag1 != null) fragTrans.remove(frag1); fragTrans.WordStr(R.id.frame_layout2, frag2); } turnNum++; if (turnNum > 2) turnNum = 0; fragTrans.addToBackStack(null); fragTrans.commit(); return true; } } return super.onOptionsItemSelected(item); }
1 FragmentManager 是治理所有Fragment的类,故此查找已有的Fragment应用函数: Fragment frag2 = fragManager.findFragmentByI(R.id.framelayout2);留意要断定是否取回Fragment,如不雅没有那么frag2 == null。
2 FragmentTransaction治理Fragment变革事务,所有动作,如例子中的remove, WordStr,都必须是在beginTransaction()和commit()之间才会生效,反复调用commit而没有调用beginTransaction会法度榜样崩溃的。
3 addToBackStack(null),就是手动把Fragment放在栈中,如许可以应用back按键退回上一层的Fragment安排。Activity是主动放到栈中的。
4 简单逻辑:turnNum是全局变量,根据这个turnNum的值的不合应用不合的Fragment。
二 主界面的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" tools:context="bill.su.fragment.MainActivity" android:orientation="horizontal" android:id="@+id/linear_fragment" android:baselineAligned="false" > <FrameLayout android:id="@+id/frame_layout1" android:layout_weight="1" android:layout_width="0sp" android:layout_height="match_parent" /> <FrameLayout android:id="@+id/frame_layout2" android:layout_weight="1" android:layout_width="0sp" android:layout_height="match_parent" /> </LinearLayout>
两个FrameLayout用来装Fragment
三 菜单res/menu/main.xml的代码:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context="bill.su.fragment.MainActivity" > <item android:id="@+id/next_fragment_view_menu_item" android:showAsAction="always" android:title="@string/next_fragment_view_menu_item" /> </menu>
四 测试Fragment生命期的全部代码:
package bill.su.fragment; import android.app.Activity; import android.app.Fragment; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment1 extends Fragment { private final String TAG = "FRAGMENTTAG1"; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.d(TAG, "onCreateView called"); return inflater.inflate(R.layout.fragment1, container, false); } @Override public void onAttach(Activity activity){ super.onAttach(activity); Log.d(TAG, "onAttach called"); } @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); Log.d(TAG, "onCreate called"); } @Override public void onActivityCreated(Bundle savedInstanceState){ super.onActivityCreated(savedInstanceState); Log.d(TAG, "onActivityCreted called"); } @Override public void onStart(){ super.onStart(); Log.d(TAG, "onStart called"); } @Override public void onResume() { super.onResume(); Log.d(TAG, "onResume called"); } @Override public void onPause() { super.onPause(); Log.d(TAG, "onPause called"); } @Override public void onStop(){ super.onStop(); Log.d(TAG, "onStop called"); } @Override public void onDestroyView() { super.onDestroyView(); Log.d(TAG, "onDestroyView called"); } @Override public void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy called"); } @Override public void onDetach() { super.onDetach(); Log.d(TAG, "onDetach called"); } }
重要和Activity不合的是:
onAttached() : Fragment和Activity接起来的是调用
onCreateView(): Fragment的View创建
onActivityCreated():当Activity的onCreate()调用的时刻调用
onDestroyView(): 当Fragment的view删除的时刻调用
onDetach(): Fragment大年夜Activity中去掉落的是调用