简体   繁体   中英

Send BroadcastReceiver data in Activity to a Dialog as it comes in

I have an Activity where I'm receiving some string data through Bluetooth. I need to send this data to a dialog which is already opened as it comes from Bluetooth.

//Getting Bluetooth data from the receiver
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String data = intent.getStringExtra("Status");

            //I need to send data from here to a dialog


        }
    }
};

and my Dialog is normal DialogFragment

Data will be coming counties through Broadcast and I need to get it in Dialog.

Thanks in Advance.

You need to check top visible fragment if its your dialog fragment then you need to pass your data into your dialog fragment:-

// this code inside your onReceive()
String data = intent.getStringExtra("Status");
Fragment fragment = getTopVisibleFragment(getSupportFragmentManager(),R.id.fragment_container);
if (fragment != null && fragment instanceof DialogFragment) {
      fragment.setData(data);
}


public static Fragment getTopVisibleFragment (FragmentManager manager, int containerId) {
    return manager.findFragmentById(containerId);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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