代码笔记安卓集中营

fragment与activity。什么时候使用?

2018-08-06  本文已影响11人  _VITA

Fragment与Activity。什么时候用什么时候不用?

回答: 看需求!

个人觉得推荐场景

(使用Fragment完全替换Activity,而Activity用来管理Fragment或者把fragment当作一个轻量级activity使用)


    FragmentManager fm = getSupportFragmentManager();

    FragmentTransaction ft = fm.beginTransaction();

    ft.hide(firstStepFragment);

    if (secondStepFragment==null){

        ft.add(R.id.fl_content, secondStepFragment);

    }else {

        ft.show(secondStepFragment);

    }

    ft.addToBackStack(null);

    ft.commit();


注意:这里使用了 hide() 方法,而不是 replace() 方法,因为我们当然希望用户返回上一步操作时,之前设置的内容不会消失。

Activity管理Fragment的思路:(曾经用过)
   public class BaseFragment extends Fragment implements View.OnClickListener{

       public interface IOneFragmentClickListener{

           void onOneFragmentClick();

       }

       @Nullable

       @Override

       public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

           View contentView = inflater.inflate(R.layout.fragment_one, null);

           contentView.findViewById(R.id.edt_one).setOnClickListener(this);

           return contentView;

       }

       @Override

       public void onClick(View v) {

           if (getActivity() instanceof IOneFragmentClickListener){

                ((IOneFragmentClickListener) getActivity()).onOneFragmentClick();

           }

       }

   }

只要在宿主 Activity 实现 Fragment 定义的对外接口 IOneFragmentClickListener,便可以实现 Fragment 调用 Activity 的功能。

fragment的使用

作为 Activity 界面的一部分,Fragment 的存在必须依附于 Activity,并且与 Activity 一样,拥有自己的生命周期,同时处理用户的交互动作。同一个 Activity 可以有一个或多个 Fragment 作为界面内容,并且可以动态添加、删除 Fragment,灵活控制 UI 内容,也可以用来解决部分屏幕适配问题。
另外,support v4 包中也提供了 Fragment,兼容 Android 3.0 之前的系统(当然,现在 3.0 之前的系统在市场上已经很少见了,可以不予考虑),使用兼容包需要注意两点:

fragment 生命周期:

一边看一边提出一些问题:

image

个人对提出问题的回复:

Applications should generally not implement a constructor. Prefer
{@link #onAttach(Context)} instead. It is the first place application code can run where
the fragment is ready to be used - the point where the fragment is actually associated with its context. Some applications may also want to implement {@link #onInflate} to retrieve
attributes from a layout resource, although note this happens when the fragment is attached.</pre>

Called when a fragment is being created as part of a view layout
inflation, typically from setting the content view of an activity. This may be called immediately after the fragment is created from a <fragment>
tag in a layout file. Note this is <em>before</em> the fragment's
{@link
#onAttach(Activity)} has been called; all you should do here is
parse the attributes and save them away. <p>This is called every time the fragment is inflated, even if it is
being inflated into a new instance with saved state. It typically makes sense to re-parse the parameters each time, to allow them to change with different configurations.</p> </pre>

attach与detach及回退栈的配合,如果当你detach fragment时,那么被移除的fragment就被停止了(没有消亡,但视图已经销毁了),如果用户导航回来重新加载这个fragment,它将会重新启动,视图也会重新创建,如果你没有把事务加入到堆栈中,当fragment被remove时,这个fragment也就消亡了。

通信方式


通常,Fragment 与 Activity 通信存在三种情形:

流行的tab栏结构可以为: image.png
上一篇 下一篇

猜你喜欢

热点阅读