Unregister Broadcast Receiver and reuse other page


#1

In my nativescript app I am using same broadcast receiver in three different pages. Everytime only first page’s broadcast is being called and what I want is every page it creates it’s own broadcast receiver and unregeister it after page unloaded. if I don’t unregeister it then it will executes multiple time.
ex.
page1.js

    exports.pageloaded = function()
    {
 if (application.android) {

application.android.registerBroadcastReceiver(
  "com.sonim.barcode_read",onReceiveCallback()
 console.log("in page 1 broadcast-------------------------------------------------------------");

   if (intent != null) {
   var data = intent.getStringExtra("data");
if (data != null && data !="") {
 console.log("data"+data);
}
 
);
    }

   exports.pageUnloaded = function()
   {
    if (application.android) {
application.android.unregisterBroadcastReceiver("com.sonim.barcode_read");
}
}

page2.js

             exports.pageloaded = function()
    {
 if (application.android) {

application.android.registerBroadcastReceiver(
  "com.sonim.barcode_read",onReceiveCallback()
 console.log("in page 2 broadcast-------------------------------------------------------------");

   if (intent != null) {
   var data = intent.getStringExtra("data");
if (data != null && data !="") {
 console.log("data"+data);
}
 
);
    }

   exports.pageUnloaded = function()
   {
    if (application.android) {
application.android.unregisterBroadcastReceiver("com.sonim.barcode_read");
}
}

page3.js

             exports.pageloaded = function()
    {
 if (application.android) {

application.android.registerBroadcastReceiver(
  "com.sonim.barcode_read",onReceiveCallback()
 console.log("in page 3 broadcast-------------------------------------------------------------");

   if (intent != null) {
   var data = intent.getStringExtra("data");
if (data != null && data !="") {
 console.log("data"+data);
}
 
);
    }

   exports.pageUnloaded = function()
   {
    if (application.android) {
application.android.unregisterBroadcastReceiver("com.sonim.barcode_read");
}
}

onPause for every page
#2

@radhe12 NativeScript for Android uses single activity and fragments for navigation. No matter how many pages you have, you are still using one single activity. So you actually don’t have to write listeners on each page.

If you want to execute code on active / current page, write a common code that may fire an event on the current page (something like frame.topmost().currentPage.notify(YourEvent)).


#3

@manojdcoder thanks, make sense! I don’t know that. Here I am calling broadcast receiver at each page because Then I am processing data(which i got from broadcast receiver) differently at each different page. then what should I do in that scenario? I don’t want to register BR in app.js as it does’t goes with my application logic.


#4

You may listen for activity create event and register, unregister on destroy from app.js.

As I said already, inside the BR you can get the current page and fire a custom event. Listen for that event in page, get data sent from BR and do what you want.