-3

I have an app with an activity sending a broadcast to another activity. Everything appears to be fine. But if I close my app and open it again, this broadcasts seems to get send more than once. In fact it gets send as often I reopened the app. If I print out "Activity.this" right before sending the broadcast I get different instances.

Why could this happen? Why is the activity not dying? I checked, onDestroy get's called. BUT I have a background-service, which I dont stop, when the app is closed. Is it therefore? Can I reopen the old activity, when starting the app instead of opening a new one?

Setting launchmode in the Manifest to singleTask or singleInstance didn't work either.

EDIT Even using an AtomicInteger like in the second answer here Prevent multiple instances of my Android application composed of a single activity didn't work.

Thanks!

Edit:

I got a broadcast receiver in the ClientActivity (main) like that, coming from my service. Here I send a broadcast to another Activity (MessageActivity).

    private BroadcastReceiver serviceMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d("test", ClientActivity.this.tostring();
            sendToMessageActivity(newDBMessageStr, "updateMessageList");
        }
    };

    private void sendToMessageActivity(String message, String putExtra)
    {
        Intent intent = new Intent("de.blabla.bla.sendToMessageActivity");
        sendLocationBroadcast(intent, message, putExtra);
    }

    private void sendLocationBroadcast(Intent intent, String message, String putExtra)
    {
        intent.putExtra(putExtra, message);
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }

I registered this in the onCreate-Method of the ClientActivity:

LocalBroadcastManager.getInstance(this).registerReceiver(
                serviceMessageReceiver, new IntentFilter("de.blabla.bla.serverMessage"));

The Other activity called "MessageActivity" which gets the broadcast from the ClientActivity (main), as described above registers the following in its onCreate:

LocalBroadcastManager.getInstance(this).registerReceiver(
                mainMessageReceiver, new IntentFilter("de.freshD.crapapp.sendToMessageActivity"));

Later it receives the message here:

private BroadcastReceiver mainMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            ...
        }
    };

EDIT:

@Override
    protected void onPause() {
        super.onPause();
    }

@Override
public void onDestroy()
{
    try
    {
        unregisterReceiver(serviceMessageReceiver);
    }
    catch (Throwable e)             //IOException e
    {
        Log.d(logtag, "...Error data send: " + e.getMessage() + "...");
    }

    super.onDestroy();
}

Ohh when onPause is called, I need to unregister the Receiver there?

I edited onPause like this: (it didn't fix the bug)

@Override
    protected void onPause() {
        try
        {
            unregisterReceiver(serviceMessageReceiver);
        }
        catch (Throwable e)             //IOException e
        {
            Log.d(logtag, "...Error data send: " + e.getMessage() + "...");
        }
        super.onPause();
    }
Community
  • 1
  • 1
progNewbie
  • 4,362
  • 9
  • 48
  • 107
  • Is this on an emulator or on devices? if so, what device/version is it running? Finally, how are you ending the Activity? finish()? if so, the activity is not "closed", but rather just taken out of application focus...... Also, how, where, when are you sending the broadcast? – Bonatti Mar 10 '16 at 16:01
  • @Bonatti It is on my device. I tried it on different devices. I need to check the version, I can tell you later. Oh finish doesn't close the activity? How can I close it? I close it by pushing the "back" button. After it is closed I reopen it. I get a message from my service. When I get, I send a broadcast myself to another activity. – progNewbie Mar 10 '16 at 16:05
  • In Android, you should never "exit" your application. The memory allocation energy cost is higher than starting/ending the application. This is why your application runs within your process. To "finalize" a program, you must invoke "System.exit(0)". But this is frowed upon, since your user may want to check your application again, and may even "flip the screen", causing your activity to be recreated.... By default, pressing `Back` just calls your onPause function.... edit your question, post how/when you are sending the broadcast. – Bonatti Mar 10 '16 at 16:10
  • @Bonatti: Thanks for the info. See my edit. – progNewbie Mar 10 '16 at 16:15
  • Just to be sure: (1): Are you [registering that local receiver?](http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html#registerReceiver%28android.content.BroadcastReceiver,%20android.content.IntentFilter%29), (2): When are you sending the broadcast (I presume `onCreate` or `onResume`? (3): Are you using the AndroidManifest to register anything? (4): How many "stacks" of your application do you have? – Bonatti Mar 10 '16 at 16:23
  • @Bonatti: (1)Yes I do, see my edit. I don't send it in onCreate. (2) I send the broadcast after I received one myself from my service. (3) No I don't. (4) I don't know. What exactly does it mean? – progNewbie Mar 10 '16 at 16:29
  • We are getting close. Show us your `onPause()`, `onDestroy()` and wherever else [you are removing the receiver](http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html#unregisterReceiver%28android.content.BroadcastReceiver%29) `unregisterReceiver()`... this seems like you are registering a new receiver each time you create/open your Activity – Bonatti Mar 10 '16 at 16:37
  • @Bonatti: See my edit. – progNewbie Mar 10 '16 at 16:48
  • Closer now.... can you please change: `unregisterReceiver(serviceMessageReceiver);` to `LocalBroadcastManager.getInstance(this).unregisterReceiver(serviceMessageReceiver);`.. while I am unsure this is the problem, you are registering on a `LocalBroadcastManager` and unregistering from the "Broadcast Manager" in `Context` – Bonatti Mar 11 '16 at 11:26
  • @Bonatti: Wow, that fixed it for me!!! THANK YOU!. Pls write an answer, so that I can accept it. – progNewbie Mar 11 '16 at 16:54
  • Glad to help, good luck on your development, and feel free to ask for additional help in new questions.... but please, change your title to guide new users to your similar problem, something like: "[Duplicated Event] Multiple issues of Broadcast event when closing/reopening app." – Bonatti Mar 11 '16 at 17:53

1 Answers1

1

Always make sure you are registering and unregistering whatever receivers you are using in the same "target".

The:

LocalBroadcastManager.getInstance(this).unregisterReceiver(serviceMessageReceiv‌​er);

and

Context.unregisterReceiver(serviceMessageReceiv‌​er);

On a note: using unregisterReceiver(serviceMessageReceiv‌​er) means that you are using the this reference, that points to the local Context that you are adding code to.

Those are two different "watchers", one for each kind of Broadcast.

Bonatti
  • 2,778
  • 5
  • 23
  • 42