Fragment的使用随笔

需要记住的几个KeyPoints


  • Fragment中onCreateView中的inflate方法第三个参数需要设置为false,否则会产生多余的view:
    inflater.inflate(R.layout.example_fragment, container, false);

  • onSaveInstanceState() 方法保存的参数可以在onCreate(), onCreateView(), 或者 onActivityCreated()中恢复。

  • add(fragment,tag)方式添加的fragment隶属于那个container view?

  • 如果想让fragment配置自己的optionsMenu必须在 onCreate()中增加setHasOptionsMenu()方法的调用。

  • activity 被系统调用 onPause()后,附属于它的所有fragment都会被调用onPause(),onStop(),onDestory()也一样

  • 可以通过在activity的main layout xml文件中直接添加Fragment,但是这种方式不够灵活,不能再运行的时候删除remove或者替换replace

  • 想在运行时删除或者替换fragment,可以使用动态的方法,在activity的onCreate方法中添加和初始化fragment,记住必须在方法中制定一个fragment的容器id,类似这个R.id.fragment_container:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_articles);

        if (findViewById(R.id.fragment_container) != null) {

            // However, if we're being restored from a previous state,
            // then we don't need to do anything and should return or else
            // we could end up with overlapping fragments.
            if (savedInstanceState != null) {
                return;
            }
            HeadlinesFragment firstFragment = new HeadlinesFragment();

            // In case this activity was started with special instructions from an
            // Intent, pass the Intent's extras to the fragment as arguments
            firstFragment.setArguments(getIntent().getExtras());

            // Add the fragment to the 'fragment_container' FrameLayout
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, firstFragment).commit();
        }
    }
}
  • 删除或者替换fragment的时候,如果想让用户可以撤销操作,需要可以把transaction添加到一个堆栈中,如果用户按back键返回,那么之前删除或者被替换的fragment马上会恢复,(所以如果把transaction添加到堆栈中,fragment被删除或者替换时会被调用onStop但不会被调用onDestroy,如果没有添加到堆栈中,那么fragment被删除或者替换时会被调用onDestroy)
ArticleFragment newFragment = new ArticleFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position);
newFragment.setArguments(args);

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
  • 开发者经常需要两个fragment之前是可以通信的,比如一个显示新闻的目录,一个显示新闻的具体内容,但是Fragment-to-Fragment 之间的通信最好是通过容纳它们的activity来做,永远不要让他们直接通信(fragment出现的目的就是为了降低藕合度,方便的在不同screen的设备上适配,如果直接通信就降低了这个功效)最好的方式是在activity中定义不同的callback,例如:
public class HeadlinesFragment extends ListFragment {
    OnHeadlineSelectedListener mCallback;

    // Container Activity 实现这个接口
    public interface OnHeadlineSelectedListener {
        public void onArticleSelected(int position);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        //onAttach中获取到这个接口否则抛异常
        try {
            mCallback = (OnHeadlineSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }
    }

    ...
}
  • -
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值