片段中不调用onReceive()方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了片段中不调用onReceive()方法相关的知识,希望对你有一定的参考价值。
我有一个Activity和2个片段.Activity有一个BottomNavigationView,它包含2个项目(片段)。在第一个片段中,用户通过EditText输入数据,点击按钮我发送包含用户输入的Localbroadcast。但是当我尝试时在第二片段中收到它,它根本就没有被检索到。但是当我尝试在第一个片段中检索它时,我得到了数据。BroadcastReceiver在第二个片段的onCreateView中动态注册。任何人都可以帮我解决这个问题吗?这真让我烦恼。期待你的帮助
片段1:
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
答案
这不是使用广播的正确方法。由于第二个片段尚未启动,您无法接收广播。单击发送按钮并将sms文本作为参数时,应更改为第二个片段。
试试这个。这个对我有用 :
first fragment.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();
}
}
试试这个并告诉我它是否正常工作:)
希望它有所帮助。
以上是关于片段中不调用onReceive()方法的主要内容,如果未能解决你的问题,请参考以下文章
onRequestPermissionsResult 在片段中不起作用