Android:Fragment(2)—Fragment传递数据

1.Activity向Fragment传递数据

Activity向Fragment传递数据如果是新建Fragment的时候我们可以通过Bundle传递。

public static BlankFragment4 newInstance(String param1, String param2) {
BlankFragment4 fragment = new BlankFragment4();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}

在new Fragment的时候通过Bundle传递数据,并调用fragment.setArguments()方法给Fragment设置bundle

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}

2.Fragment向Activity传递数据

通过回调接口的方式

/**

  • 在Fragment中创建接口
    */
    public interface OnFragmentInteractionListener {
    void onFragmentInteraction(Uri uri);
    }

/**

  • 在onAttach把传递过来的context也就是与Fragment关联的Activity
  • 给属性 OnFragmentInteractionListener mListener 赋值。这就需要
  • 对应的Activity实现OnFragmentInteractionListener接口
  • @param context
    */
    @Override
    public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
    mListener = (OnFragmentInteractionListener) context;
    } else {
    throw new RuntimeException(context.toString()
    + " must implement OnFragmentInteractionListener");
    }
    }

/**

  • 当Fragment与对应的Activity解绑时释放mListener
    */
    @Override
    public void onDetach() {
    super.onDetach();
    mListener = null;
    }

例:传递数据

Android:Fragment(2)—Fragment传递数据_第1张图片
布局如第一篇:https://blog.csdn.net/weixin_43689040/article/details/104244939

JAVA代码

Fragment向Activity传递数据:
1.在Fragment先定义接口,接口中声明方法 方法得有参数 (参数就是你要传给activity的值)
2.实现onAttach 在方法里需要给接口变量赋值
在点击事件里调用接口中方法

FragmentOne代码

/**
 * Created by sq on 2020/2/10.
 */
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class FragmentOne extends Fragment{

    public Back back;


    @Override
    public void onAttach(Context context) {

        back = (Back) context;
        super.onAttach(context);
    }

    /*
        回传值
        1.在Fragment先定义接口,接口中声明方法  方法得有参数 (参数就是你要传给activity的值)
        2.实现onAttach   在方法里需要给接口变量赋值
            在点击事件里调用接口中方法
        3.让activity实现你定义的接口
     */

    public interface Back {
        public void toBack(String s);
    }
    //指定要显示的页面是谁
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

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

        Bundle bundle = getArguments();
        String value = bundle.getString("un");
        TextView textView = (TextView) view.findViewById(R.id.textView);
        textView.setText(value);

        //回传值
        Button button = (Button) view.findViewById(R.id.back);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                back.toBack("123");
            }
        });

        return view;
    }
}

3.让activity实现你定义的接口

MainActivity 代码

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements FragmentOne.Back {
    private Button one,two;
    FragmentManager fm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        one = (Button) findViewById(R.id.one);
        two = (Button) findViewById(R.id.second);

        fm = getFragmentManager();

        fm.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
            @Override
            public void onBackStackChanged() {
                Log.i("message","@@"+fm.getBackStackEntryCount());
            }
        });
    }

    public void click(View view){
        int id = view.getId();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        switch(id){
            case R.id.one:

                FragmentOne fragmentOne = new FragmentOne();
                //  p1  放在哪个布局里

                Bundle bundle = new Bundle();
                bundle.putString("un","admin");
                fragmentOne.setArguments(bundle);
                fragmentTransaction.replace(R.id.line1,fragmentOne);

                fragmentTransaction.addToBackStack("one");
                break;
            case R.id.second:

                FragmentTwo fragmentTwo = new FragmentTwo();
                //  p1  放在哪个布局里
                fragmentTransaction.replace(R.id.line1,fragmentTwo);

                fragmentTransaction.addToBackStack("two");

                break;
            case R.id.back:
                fm.popBackStack();
                break;
        }
        fragmentTransaction.commit();
    }
    @Override
    public void toBack(String s) {
        Log.i("message","###"+s);
    }
}

你可能感兴趣的:(Andiord)