Fragment 的 startActivityForResul

2017-10-10  本文已影响0人  珞神

一、为什么要写 startActivityForResult()

二、实际操作

2.1 Fragment1,即 FirstFragment 代码如下:

public class FirstFragment extends Fragment {


    @BindView(R.id.listview)
    ListView listview;

    private String name;

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

        Log.i("FirstFragment", "onCreateView");
        View inflate = inflater.inflate(R.layout.huituizhan_first_fragment, container, false);
        ButterKnife.bind(this, inflate);
 

        initListViewData();

        return inflate;
    }
    

    private void initListViewData() {
        list = new ArrayList<>();
        int m = 0;
        for (int i = 0; i < 20; i++) {
            list.add("珞神"+m);
            m++;
        }
        myAdapter = new MyAdapter();
        listview.setAdapter(myAdapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                //点击跳转
                Intent intent = new Intent(getActivity(),ResultActivity.class);
                intent.putExtra("item",position+"");
                startActivityForResult(intent,0);

            }
        });
    }

    /**
     * 调用 startActivityForResult 后,目标activity 返回的数据会在这个方法内
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        list.add(requestCode+resultCode+data.getStringExtra("item"));

        myAdapter.notifyDataSetChanged();
    }

    private ArrayList<String> list;

    private MyAdapter myAdapter;

    public class MyAdapter extends BaseAdapter {



        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
             View view = null;
            ViewHolder viewHolder;
            if (convertView == null) {
                view = View.inflate(getActivity(), R.layout.litt_item, null);
                viewHolder = new ViewHolder(view);
                view.setTag(viewHolder);
            }else {
                view = convertView;
                viewHolder = (ViewHolder) view.getTag();
            }
            viewHolder.listText.setText(list.get(position));

            return view;
        }

    }
        static class ViewHolder {
            @BindView(R.id.list_text)
            TextView listText;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }


}

2.2 Activity2 即 ResultActivity 的代码如下:

public class ResultActivity extends AppCompatActivity {

    @BindView(R.id.framelayout_resultfragment)
    FrameLayout framelayoutResultfragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sencond);
        ButterKnife.bind(this);

        //取出传递过来的数据, 注意 newInstance 方法设置的有 Bundle
        String title = getIntent().getStringExtra("item");
        ResultFragment resultFragment = ResultFragment.newInstance(title);


        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.framelayout_resultfragment,resultFragment,"resultfragment")
                .commit();


    }



}

注意:ResultFragment 一定要是同一个对象,不要创建两个对象

2.3 Fragment2 即 ResultFragment 的代码如下:
package com.example.administrator.fragment_text.huituizhan;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.fragment_text.R;

public class ResultFragment extends Fragment {



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle bundle = getArguments();
        if (bundle != null) {
             String item = bundle.getString("item");

            Log.i("ResultFragment",item+"");
            Intent intent = new Intent();
            intent.putExtra("item","result");
            getActivity().setResult(1,intent);

        }


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_result, container, false);
    }
    //返回 resultFragment 对象,并把传递过来的数据设置在 bundle 中
    public static ResultFragment newInstance(String argument){
        Bundle bundle = new Bundle();
        bundle.putString("item", argument);
        ResultFragment resultFragment = new ResultFragment();
        resultFragment.setArguments(bundle);
        return resultFragment;
    }


}

注意: 这里的逻辑是,在创建 ResultFragment 对象的时候,就把数据设置进 Bundle 中了,然后在 onCreate 方法中取出 bundle 中的数据,并设置返回的数据。

2.4 然后在 FirstFragment 中的 onActivityResult() 中 根据 resultCode 取回数据进行操作!
上一篇下一篇

猜你喜欢

热点阅读