I have been doing some experimenting trying to understand the ResultReceiver more.
I have have a TestActivity with a TextView this ResultReceiver:
@SuppressLint("ParcelCreator")
class TestResultReceiver extends ResultReceiver {
public TestResultReceiver(Handler handler) {
super(handler);
}
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
// Display the address string or an error message sent from the intent service.
String mydata = resultData.getString("My_cool_data");
if (resultCode == SomeService.SUCCESS_RESULT) {
Log.d(TAG, "Wow we got a result!" + mydata +
"activity hashcode: " + String.valueOf(this.hashCode()));
// Change a UI element
mUTextView.setText("changed by ResultReciever");
}
}
}
I also have this in TestActivity:
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
My IntentService just has a timer to send a result after 7 seconds:
private void handleActionFoo(final ResultReceiver mReceiver) {
new java.util.Timer().schedule(
new java.util.TimerTask() {
@Override
public void run() {
Bundle bundle = new Bundle();
bundle.putString("My_cool_data", "some-data");
mReceiver.send(SUCCESS_RESULT, bundle);
}
},
7000
);
}
Here is what I have found:
If I start my TestActivity from MainActivity, then quickly press back, after 7 seconds I get my log debug message that looks like this:
Wow we got a result! some-data activity hashcode: 114159573
Which is weird, because I override onBackPressed() in my activity, so the activity should be destroyed, yet my Log message runs. I thought maybe my activity was not being destroyed so I did this:
Launched TestActivity, waited 2 seconds, pressed back, then quickly launched TestActivity again.
after a few seconds I got one debug message and after a few seconds I got the next:
Wow we got a result! some-data activity hashcode: 380270833
Wow we got a result! some-data activity hashcode: 920349858
What is interesting is that my TextView's text did not change when the first debug message arrived, but DID change when my second debug message arrived.
What exactly is happening here? Why is my app not crashing when the first debug message arrived? Because clearly that instance of the activity does not exist anymore, so why did I get no null pointer exception for the textview? And why does that first debug message even execute..? Is something being stored in memory?
