繁体   English   中英

我想将数据活动发送到片段但从未调用过接口方法

[英]I want to Send Data Activity to fragment But InterFace Method Never Called

我在两个片段中实现我的接口我想将数据从活动发送到两个片段..当我这样做时,我的数据在第二个片段中发送,而不是在第一个片段中。 它只调用了第二个 Fragment 接口方法..不调用第一个 Fragment 接口方法我不明白请有人帮助....

这是主要活动

     public class MainActivity extends AppCompatActivity implements FragmentCommunication {
     TextView textView;
     ReceiveMessage receiveMessage;
     EditText editText;
     Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.textAcitivity);
        editText = findViewById(R.id.editActivity);
        button = findViewById(R.id.buttonActivity);

        if (findViewById(R.id.fragment1)!= null)
        {
            if (savedInstanceState != null)
            {
                return;
            }
        }

        FirstFragment firstFragment = new FirstFragment();
        SecondFragment secondFragment = new SecondFragment();
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment1, firstFragment, null);
        fragmentTransaction.commit();
        FragmentTransaction fragmentTransaction1 = getSupportFragmentManager().beginTransaction().add(R.id.fragment2, secondFragment,null);
        fragmentTransaction1.commit();

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = editText.getText().toString().trim();
                 receiveMessage.receiveMessage(name, 3);
            }
        });
    }
    @Override
    public void onAttachFragment(@NonNull Fragment fragment) {
        super.onAttachFragment(fragment);
        try {

            receiveMessage = (ReceiveMessage) fragment;

        } catch (ClassCastException e) {
            throw new ClassCastException(fragment.toString() + " must implement receiveMessage..");
        }

    }
}

这是我的界面

    public interface ReceiveMessage {
    void receiveMessage(String message, int from);
}

这是我的第一个片段


    public class FirstFragment extends Fragment implements ReceiveMessage {
    private View view;
    private TextView textView;
    private EditText editText;
    private Button button;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.firstfragment, container, false);
        textView = view.findViewById(R.id.textView10);
        editText = view.findViewById(R.id.Edit1);
        button = view.findViewById(R.id.button1);
        return view;


    }

    @Override
    public void receiveMessage(String message, int from) {  // THIS METHOD NAVER CALLED WHEN I SEND dATA 
    FROM aCTIVITY
        if (from == 3)
        {
            textView.setText(message);
        }

    }
}

这是我的第二个片段


    public class SecondFragment extends Fragment implements ReceiveMessage {
    private View view;
    private TextView textView;
    private EditText editText;
    private Button button;



    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.secondfragment, container, false);

        textView = view.findViewById(R.id.textView2);
        editText = view.findViewById(R.id.editText2);
        button = view.findViewById(R.id.button);

        return view;
    }




    @Override
    public void receiveMessage(String message, int from) {
        if (from == 3)
        {
            textView.setText(message);
        }
    }
}


每次添加新片段时,都会再次调用活动的“onAttachFragment”。 因此,当您添加第二个片段时,您将“receiveMessage”对象的值重置为第二个片段的接口实现。因此您应该将代码重写为:

ReceiveMessage firstReceiver;
ReceiveMessage secondReceiver;

@Override
public void onAttachFragment(Fragment fragment) {
    super.onAttachFragment(fragment);
    if (fragment instanceof FirstFragment) {
       firstReceiver = (ReceiveMessage) fragment;
    } else if (fragment instanceof SecondFragment) {
       secondReceiver = (ReceiverMessage) fragment;
    } 
}



button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String name = editText.getText().toString().trim();
             firstReceiver.receiveMessage(name, 3);
             secondReceiver.receiveMessage(name, 3); 
        }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM