0

So, I'm facing a strange behavior. I'm implementing MVP design pattern in my app. I have 2 activities, A and B. The presenter of activity A is listening to a socket connection to the server. In the activity's onDestroy() method, I call a method to unregister the presenter from the socket helper class.

In case of a certain message "TYPE X" from the socket, I start a new activity B. The onDestroy() method is called normally, and the socket listener is unregistered.

Here's the weird part. When receiving a message of the same TYPE "X", activity A is recreated and activity B is restarted, repeating the whole cycle again.

The activity A:

@Override
protected void onDestroy() {
    mainActivityPresenter.terminate();
    // some code
    super.onDestroy();
}

The presenter:

@Override
public void terminate() {
    socketHelper.unregisterListener(socketListener);
}

Does anyone have an explanation for this behavior?

Edit: added some code snippet of how I start the new activity.

@Override
public void showOrderActivity(int orderType) {
    Intent orderIntent = new Intent(MainActivity.this, OrderActivity.class);
    orderIntent.putExtra(Constants.BUNDLE_CONTENT_NAME, orderType);
//        orderIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
//        orderIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//        orderIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        finishAffinity();
//        orderIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(orderIntent);
    finish();
}

The commented code is my trials to make sure that Activity A is destroyed.

4
  • Perhaps onDestroy() is not being called. Note that starting Activity B will not trigger onDestroy() on Activity A, in normal cases. Sep 9, 2018 at 12:55
  • I checked. onDestroy() is called. I edited my questions and added some details. Sep 9, 2018 at 13:02
  • Perhaps unregisterListener() is not doing what it is supposed to be doing. Sep 9, 2018 at 13:06
  • So, upon double checking, I discovered the reason for this behavior. It was totally my misunderstanding of a part of the business logic. Thanks for your response. Sep 9, 2018 at 13:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.