• Android_Fragment和Activity之间的通信


    Fragment 的生命周期是随着activity变化而变化的。

    如果activity要给在运行的时候给fragment传人数据则需要fragment实现一个自定义的接口,并且实现接口里面的方法,在activity里面保存这个接口并且在需要传人数据的时候调用这个方法即可。

    如果fragment运行的时候需要给activity传数据了?activity先实现这个接口,然后在fragment里面重写onAttach(Activity act)这个方法,并在这个方法里面讲activity强制转换为接口类,然后在fragment使用的时候调用这个接口类的方法。

    请看下面的例子:

    activity给fragment里面传人数据:

    public class MainActivity extends Activity{
        private TestFragment fragment;//fragment
        private UpdateFragment updateFragment;//fragment实现的接口
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main_layout);
            initView();
            
        }    
        private void initView() {
            FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
            fragment = new TestFragment();
            updateFragment = fragment;
            fragmentTransaction.add(R.id.main, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commitAllowingStateLoss();
        }
        @Override
        protected void onResume() {
            //改变fragment里面textView的值
            updateFragment.setData("王五");
            super.onResume();
        }
    
    }

    接口类

    public interface UpdateFragment {
        void setData(String str);
    }

    Fragment类:

    public class TestFragment extends Fragment implements UpdateFragment{
    
        private View view;
        private TextView txt;
        public TestFragment() {
            super();
        }
    
        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
        }
    
        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            if (null == view){
                view = inflater.inflate(R.layout.activity_main, container, false);
                txt = (TextView)view.findViewById(R.id.tv);
            }
            return view;
        }
        @Override
        public void setData(String str) {
            if (null != txt) {
                txt.setText(str);
            }
        }
    
    }

    Fragment给activity传入数据demo:

    Activity类:

    public class MainActivity extends Activity implements UpdateFragment{
        private static final String TAG = MainActivity.class.getSimpleName();
        private TestFragment fragment;//fragment
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main_layout);
            initView();
            
        }    
        private void initView() {
            FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
            fragment = new TestFragment();
            fragmentTransaction.add(R.id.main, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commitAllowingStateLoss();
        }
        @Override
        protected void onResume() {
            super.onResume();
        }
        @Override
        public void setData(String str) {
            if (null != str ){
                Log.d(TAG, "setData:"+str);
            }
        }

    接口类和上面的是一样的就不写了

    Fragment类:

    public class TestFragment extends Fragment{
    
        private View view;
        private TextView txt;
        private Button btn;
        private UpdateFragment updateFragment;
        public TestFragment() {
            super();
        }
    
        @Override
        public void onAttach(Activity activity) {
            try {
                updateFragment = (UpdateFragment)activity;
            } catch (Exception e) {
                e.printStackTrace();
            }
            super.onAttach(activity);
        }
    
    
        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            if (null == view){
                view = inflater.inflate(R.layout.activity_main, container, false);
                txt = (TextView)view.findViewById(R.id.tv);
                btn = (Button)view.findViewById(R.id.btn);
                btn.setOnClickListener(clickListener);
            }
            return view;
        }
        private OnClickListener clickListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != txt) {
                    updateFragment.setData(txt.getText().toString());
                }
                
            }
        };
    
    }
  • 相关阅读:
    [日常] PHP库函数fgetss的BUG
    [PHP] 算法-选择排序的PHP实现
    [PHP] 算法-数值的整数次方的PHP实现
    [PHP] 算法-顺时针打印矩阵的PHP实现
    [PHP] 算法-字符串的左循环的PHP实现
    [PHP] 算法-将一个字符串转换成一个整数的PHP实现
    [PHP] 算法-字符串的全排列的PHP实现
    [PHP]算法-堆排序的PHP实现
    [PHP]算法-二进制中1的个数的PHP实现
    [PHP]算法-旋转数组的最小值的PHP实现
  • 原文地址:https://www.cnblogs.com/changkai244/p/4146514.html
Copyright © 2020-2023  润新知