美文网首页
Fragment传值到Activity

Fragment传值到Activity

作者: Rambo_Y | 来源:发表于2018-04-10 14:23 被阅读0次

采用接口回调机制

在Fragment中定义接口

public  interface FragmentListener{
        void onFragment(Object object);
    }

当Fragment被加载到Activity的时候会被回调,判断宿主Activity是否实现了接口。

@Override
    public void onAttach(Context context) {
        super.onAttach(context);

        if(context instanceof FragmentListener)
        {
            mFragmentListener = (FragmentListener)context;
        }
        else{
            throw new IllegalArgumentException("Activity must implements FragmentListener");
        }

    }

传值操作,通过接口回调

mFragmentListener.onFragment("数据来自Fragment");

Activity实现接口回调方法,并重写回调方法。

@Override
 public void onFragment(Object object){
 
       button.setText(object.toString());
    }

示例:

在Fragment中

public class RightFragment extends Fragment {
private FragmentListener mFragmentListener;
    private String name;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
        View  view =inflater.inflate(R.layout.avtivity_time,container,false);
        TextView textView = (TextView)view.findViewById(R.id.time_text);
        if (getArguments() != null) {
           name = getArguments().getString("MainActivity");
        }
        textView.setText(name);
        mFragmentListener.onFragment("数据来自Fragment");
        return view;
    }

    public  interface FragmentListener{
        void onFragment(Object object);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        if(context instanceof FragmentListener)
        {
            mFragmentListener = (FragmentListener)context;
        }
        else{
            throw new IllegalArgumentException("Activity must implements FragmentListener");
        }

    }
    
}

在Activity中

public class MainActivity extends AppCompatActivity implements RightFragment.FragmentListener {
private Button button;
    private RightFragment rightFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button)findViewById(R.id.button_1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rightFragment = new RightFragment();
                Bundle args = new Bundle();
                args.putString("MainActivity","Hello");
//                setArguments方法必须在fragment创建以后
                rightFragment.setArguments(args);
                addFragment(rightFragment);

            }
        });
    }

    private void addFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.fragment_contain,fragment);
        transaction.commit();
    }
    @Override
        public  void onFragment(Object object){
            button.setText(object.toString());
    }
}

相关文章

网友评论

      本文标题:Fragment传值到Activity

      本文链接:https://www.haomeiwen.com/subject/nvxnhftx.html