1. 程式人生 > >activity/fragment傳值

activity/fragment傳值

一、activity間互傳:Intent+Bundle

1、不需要回傳:

第一個activity中:

        Intent intent = new Intent(MainActivity.this, MainActivityI.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("homeMessageModel", homeMessageModel);
        ...
        intent.putExtras(bundle);
        startActivity(intent);

第二個activity中:

        Intent intent = this.getIntent();
        Bundle bundle = intent.getExtras();
        HomeMessageModel homeMessageModel = (HomeMessageModel) bundle.getSerializable("homeMessageModel");
        ...

2、需要回傳值:

第一個activity中:

                //傳送給第二個activity資料
                Intent intent = new
Intent(); intent.setClass(getActivity(), MessageActivity.class); Bundle bundle = new Bundle(); bundle.putSerializable("homeMessageModel", homeMessageModel); intent.putExtras(bundle); startActivityForResult(intent, 100); //接受第二個activity資料
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 100 && resultCode == 200) { Bundle bundle = data.getExtras(); boolean hasPersonalMsg = bundle.getBoolean("hasPersonalMsg"); Log.e("FeaturedFragment","hasPersonalMsg==="+hasPersonalMsg); if (!hasPersonalMsg) { mMsgDot.setVisibility(View.GONE); } else { mMsgDot.setVisibility(View.VISIBLE); } } }

第二個activity中:

 //接收第一個activity的資料
        intent = this.getIntent();
        if (intent != null) {
            bundle = intent.getExtras();
            if (bundle != null) {
                homeMessageModel = (HomeMessageModel) bundle.getSerializable("homeMessageModel");
            }
        }


 //返回給第一個activity的資料,
                bundle.putBoolean("hasPersonalMsg", true);
                intent.putExtras(bundle);
                this.setResult(200, intent);

二、fragment間互傳

三、activity傳值給fragment:Bundle

activity中:

                OfficeFragment mFragOffice=new OfficeFragment();
                Bundle bundleOffice = new Bundle();
                bundleOffice.putSerializable("officeMsg", officeMsg);
                mFragOffice.setArguments(bundleOffice);

fragment中

Bundle bundle = getArguments();
if (bundle != null) {
    ArrayList<HomeMsgOfficeModel> officeMsg = (ArrayList<HomeMsgOfficeModel>) bundle.getSerializable("officeMsg");
}

如果在onresume中用這種方法會報錯:

    java.lang.IllegalStateException: Fragment already active

解決辦法:

在activity中


    private ArrayList<HomeMsgOfficeModel> officeMsg = new ArrayList<>();
    private ArrayList<HomeMsgPersonalModel> personalMsg = new ArrayList<>();

    public ArrayList<HomeMsgOfficeModel> getOfficeMsg() {
        return officeMsg;
    }

    public void setOfficeMsg(ArrayList<HomeMsgOfficeModel> officeMsg) {
        this.officeMsg = officeMsg;
    }

    public ArrayList<HomeMsgPersonalModel> getPersonalMsg() {
        return personalMsg;
    }

    public void setPersonalMsg(ArrayList<HomeMsgPersonalModel> personalMsg) {
        this.personalMsg = personalMsg;
    }

    //網路獲取資料後
    personalMsg.addAll(message);
    officeMsg.addAll(gonggao);

然後在fragment中

 personalMsg=((MessageActivity)getActivity()).getPersonalMsg();//獲取activity的值
 officeMsg=((MessageActivity)getActivity()).getOfficeMsg();//獲取activity的值

四、fragment傳值給activity