我有一个Activity和2个片段.Activity有一个BottomNavigationView,其中包含2个项目(片段)。在第一个片段中,用户通过EditText输入数据,然后单击按钮以发送 Localbroadcast 包含用户输入。但是当我尝试在“第二个片段”中接收它时,根本就不会检索它。但是,当我尝试在第一个片段中检索它时,就得到了数据。 BroadcastReceiver 被动态注册到第二个片段的 onCreateView 中。有人可以帮我解决这个问题吗?这真的困扰着我。期待您的帮助
Fragment1:
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
input = user_input.getText().toString();
if (input.trim().length() != 0 && !TextUtils.isEmpty(input)) {
Log.e("data",input);
Intent intent = new Intent("send");
intent.putExtra("sms", input);
LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intent);
}
Fragment2:
if (smsreceiver==null)
{
smsreceiver=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("Retreive","Retrieve");
}
};
LocalBroadcastManager.getInstance(getContext()).registerReceiver(smsreceiver,new IntentFilter("send"));
}
日志:
09-04 14:45:51.007 14829-14829/com.globemaster.com.messageapp E/data: cxs
09-04 14:45:51.857 14829-14829/com.globemaster.com.messageapp D/ViewRootImpl: ViewPostImeInputStage processPointer 0
09-04 14:45:51.937 14829-14829/com.globemaster.com.messageapp D/ViewRootImpl: ViewPostImeInputStage processPointer 1
09-04 14:45:51.977 14829-14829/com.globemaster.com.messageapp E/OnCreateView: Called
09-04 14:45:51.977 14829-14829/com.globemaster.com.messageapp D/TextView: setTypeface with style : 0
setTypeface with style : 0
09-04 14:45:52.047 14829-14829/com.globemaster.com.messageapp E/Started: Called
09-04 14:45:52.047 14829-14829/com.globemaster.com.messageapp E/Onresume: called
09-04 14:45:52.077 14829-14829/com.globemaster.com.messageapp E/RecyclerView: No adapter attached; skipping layout
答案 0 :(得分:0)
这不是使用广播的正确方法。由于第二个片段尚未开始,您无法接收广播。单击带有sms文本作为参数的发送按钮时,应更改为第二个片段。
尝试一下。它对我有用:
FirstFragment.java
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
input = user_input.getText().toString();
if (!TextUtils.isEmpty(input)) {
Log.d("SendFrag", "sending : "+input);
// Set data into fragment
ReceiveSmsFragment fragment = new ReceiveSmsFragment();
Bundle arguments = new Bundle();
arguments.putString("sms", input);
fragment.setArguments(arguments);
// Change fragment with argument
FragmentTransaction transaction=getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragcontainer, fragment);
transaction.commit();
} else {
Toast.makeText(getContext(), "Please enter a valid input", Toast.LENGTH_SHORT).show();
}
input = null;
}
});
和SecondFragment.java
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Log.e("OnCreateView","Called");
view = inflater.inflate(R.layout.receive_sms_frag, container, false);
Bundle arguments = getArguments();
String sms_string = arguments.getString("sms");
if (sms == null) {
sms = new Sms();
}
sms.add(sms_string);
Log.d("ReceiveFrag", "received : "+sms_string);
return view;
}
最后,您的短信课程很奇怪。我在这里发布更好的版本 Sms.java
public class Sms {
private static ArrayList<String> sms_list;
// Sms constructor with no private/public, it's a constructor !
Sms()
{
sms_list = new ArrayList<>();
}
public void add(String sms)
{
sms_list.add(sms);
}
public String get(int position)
{
return sms_list.get(position);
}
public static int getListLength()
{
return sms_list.size();
}
}
尝试一下,告诉我它是否有效:)
希望有帮助。