zoukankan      html  css  js  c++  java
  • Android

    一些优秀博客:
     
    Fragment生命周期
                
    onAttached() —— 当fragment被加入到activity时调用(在这个方法中可以获得所在的activity)。
    onCreateView() —— 当activity要得到fragment的layout时,调用此方法,fragment在其中创建自己的layout(界面)。
    onActivityCreated() —— 当activity的onCreated()方法返回后调用此方法
    onDestroyView() —— 当fragment中的视图被移除的时候,调用这个方法。
    onDetach() —— 当fragment和activity分离的时候,调用这个方法。
     

    Fragment与Activity
    Fragment获取Activity实例 Activity获取Fragment实例
    getActivtiy() getFragmentManager()>>
    FragmentManager:
    findFragmentById() 
    findFragmentByTag()
    popBackStack():将Fragment从后台栈中弹出(模拟用户按下BACK按键)
    beginTransaction()>>
    FragmentTransAction
    add(int containerViewId, Fragment fragment
    remove(R.id.framework,fragment) addToBackStack() 
    commit()
     
    1. //Activity中
    2. Fragment fragment =newFragment();
    3. FragmentManager fragmentManager = getFragmentManager();
    4. FragmentTransaction transaction =fragmentManager.beginTransaction();
    5. transaction.replace(R.id.framework,fragment);
    6. transaction.addToBackStack();
    7. transaction.commit();
     
    Fragment与Activity相互通信
    Activity向Fragment传递数据
    在Activity中创建Bundle数据包,并调用Fragment的setArguments(Bundle)即可将数据传到Fragment

     
    Fragment向Activity传递数据/Activity需要在Fragment运行中进行实时通信:
    在Fragment中定义一个内部回调接口,再让包含该Activity的Activity实现该回调接口(可通过接口方法在Fragment运行中与其进行实时通信),这样Fragment即可调用该接口方法将数据传回给Activity
     
    例子
    1. //MainActivity
    2. publicclassMainActivityextendsActivityimplements BookFragment.Callbacks
    3. {
    4.     @Override
    5.     publicvoid onCreate(Bundle savedInstanceState)
    6.     {
    7.         super.onCreate(savedInstanceState);
    8.         setContentView(R.layout.activity_book_twopane);
    9.         // 创建Bundle,准备向Fragment传入参数
    10.         Bundle arguments =newBundle();
    11.         arguments.putInt("name", x);
    12.         BookFragment fragment =newBookFragment();
    13.         fragment.setArguments(arguments);
    14.      getFragmentManager().beginTransaction()
                      .replace(R.id.book_detail_container, fragment)
                      .commit();
    15.     }
    16.     // 实现Callbacks接口必须实现的方法
    17.     @Override
    18.     publicvoid someAction(string s)
    19.     {
    20.         String s1=s;//获取到fragment传来的参数
    21.     }
    22. }
     
    1. //activity_book_twopane.xml
    2. <LinearLayout
    3.     xmlns:android="http://schemas.android.com/apk/res/android"
    4.     android:orientation="horizontal"
    5.     android:layout_width="match_parent"
    6.     android:layout_height="match_parent"
    7.     android:layout_marginLeft="16dp"
    8.     android:layout_marginRight="16dp"
    9.     android:divider="?android:attr/dividerHorizontal"
    10.     android:showDividers="middle">
    11.     <!-- 添加一个Fragment -->
    12.     <fragment
    13.         android:name="org.crazyit.app.BookFragment"
    14.         android:id="@+id/book_list"
    15.         android:layout_width="0dp"
    16.         android:layout_height="match_parent"
    17.         android:layout_weight="1"/>
    18.     <!-- 添加一个FrameLayout容器 -->
    19.     <FrameLayout
    20.         android:id="@+id/book_detail_container"
    21.         android:layout_width="0dp"
    22.         android:layout_height="match_parent"
    23.         android:layout_weight="3"/>
    24. </LinearLayout>
     
    1. //BookFragment
    2. publicclassBookFragment extends Fragment
    3. {
    4. privateCallbacks mCallbacks;
    5.         // 定义一个回调接口,该Fragment所在Activity需要实现该接口,该Fragment将通过该接口与它所在的Activity交互
    6. public interface Callbacks
    7. {
    8. publicvoid someAction(String s);
    9. }
    10. @Override
    11. publicvoid onCreate(Bundle savedInstanceState)
    12. {
    13. super.onCreate(savedInstanceState);
    14. }
    15.         @Override
    16.         publicView onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)
    17.         {
    18.         View rootView = inflater.inflate(R.layout.fragment1,container,false);
    19.                   TextView tv= ((TextView) rootView.findViewById(R.id.book_title));
    20.                    tv.setText("");
    21.                    return rootView;
    22.         }
    23. @Override
    24. publicvoid onAttach(Activity activity)
    25. {
    26. super.onAttach(activity);
    27. // 如果Activity没有实现Callbacks接口,抛出异常
    28. if(!(activity instanceof Callbacks))
    29. {
    30. thrownewIllegalStateException(
    31. "BookListFragment所在的Activity必须实现Callbacks接口!");
    32. }
    33. // 把该Activity当成Callbacks对象
    34. mCallbacks =(Callbacks)activity;
    35.      String s ="to activity";
    36.        mCallbacks.someAction(s);
    37. }
    38. @Override
    39. publicvoid onDetach()
    40. {
    41. super.onDetach();
    42. // 将mCallbacks赋为null。
    43. mCallbacks = null;
    44. }
    45. }
     
    1. //fragment1.xml
    2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    3.     android:layout_width="match_parent"
    4.     android:layout_height="match_parent"
    5.     android:orientation="vertical">
    6.   
    7.     <TextView
    8.         style="?android:attr/textAppearanceLarge"
    9.         android:id="@+id/book_title"
    10.         android:layout_width="match_parent"
    11.         android:layout_height="wrap_content"
    12.         android:padding="16dp"/>
    13.     
    14. </LinearLayout>

    Fragment : onActivityResult(int requestCode, int resultCode, Intent intent) not be called in fragment
     
    Option 1 :
    If you're calling startActivityForResult() from the fragment then you should call startActivityForResult() not getActivity().startActivityForResult(), as it will result in fragment onActivityResult().
     
    If you're not sure where you're calling on startActivityForResult() and how you will be calling methods.
    Option 2:
    Since Activity gets the result of onActivityResult(), you will need to override the activity's onActivityResult() and call super.onActivityResult() to propagate to the respective fragment for unhandled results codes or for all.
     

    Fragment设计逻辑
     
     
     
     
     
     
     
     
     





  • 相关阅读:
    juc原子类之五:AtomicLongFieldUpdater原子类
    DICOM:C-GET与C-MOVE对照剖析
    android createbitmap函数内存溢出,求解怎样进行处理out of memory溢出问题
    TRIZ系列-创新原理-32-改变颜色原理
    FP-Growth算法之频繁项集的挖掘(python)
    个人年终总结
    J2EE之ANT
    log4net 使用与配置 每天一份log文件
    Android 完美实现图片圆角和圆形(对实现进行分析)
    Unity3d修炼之路:载入一个预制体,然后为该对象加入组件,然后查找对象,得到组件。
  • 原文地址:https://www.cnblogs.com/Doing-what-I-love/p/5532956.html
Copyright © 2011-2022 走看看