[英]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.