@Override
  protected void onDestroy() {
    super.onDestroy();

    if (rejoinChat != null) {
      rejoinChat.stop();
    }

    if (restartChat != null) {
      restartChat.stop();
    }

    if (messagingApi != null) {
      messagingApi.removeApiEventListener(this);
      messagingApi.disconnectApi();
    }
  }
        public void onClick(View v) {
          if (!isServiceAvailable()) {
            Utils.showMessage(ChatList.this, getString(R.string.label_continue_chat_failed));
            return;
          }

          // Get selected item
          ChatListItemCache cache = (ChatListItemCache) v.getTag();
          if (cache.isGroupChat()) {
            // Group chat
            IChatSession session = isGroupChatActive(cache.chatId);
            if (session != null) {
              // Session already active on the device: just reload it in the UI
              try {
                Intent intent = new Intent(ChatList.this, GroupChatView.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.putExtra("subject", session.getSubject());
                intent.putExtra("sessionId", session.getSessionID());
                startActivity(intent);
              } catch (Exception e) {
                Utils.showMessage(ChatList.this, getString(R.string.label_api_failed));
              }
            } else {
              // Test if the session may be rejoined or not
              int status = RichMessaging.getInstance().getGroupChatStatus(cache.chatId);
              if (status == EventsLogApi.STATUS_TERMINATED_BY_USER) {
                // The session was terminated by user itself: rejoin or restart are not authorized
                Utils.showMessage(ChatList.this, getString(R.string.label_rejoin_unauthorized));
              } else if (status == EventsLogApi.STATUS_TERMINATED_BY_REMOTE) {
                // The session was terminated: only a restart may be done
                restartChat = new RestartChat(ChatList.this, messagingApi, cache.chatId);
                restartChat.start();
              } else {
                // Session terminated on the device: try to rejoin the session
                rejoinChat = new RejoinChat(ChatList.this, messagingApi, cache.chatId);
                rejoinChat.start();
              }
            }
          } else {
            // 1-1 chat
            IChatSession session = isChatSessionActive(cache.sessionId);
            if (session != null) {
              // Session already active on the device: just reload it in the UI
              try {
                Intent intent = new Intent(ChatList.this, OneToOneChatView.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.putExtra("contact", session.getRemoteContact());
                intent.putExtra("sessionId", session.getSessionID());
                startActivity(intent);
              } catch (Exception e) {
                Utils.showMessage(ChatList.this, getString(R.string.label_api_failed));
              }
            } else {
              // Session terminated on the device: create a new one on the first message
              Intent intent = new Intent(ChatList.this, OneToOneChatView.class);
              intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
              intent.putExtra("contact", cache.contact);
              startActivity(intent);
            }
          }
        }