Smack 4.2 roster object gets loaded even before RosterLoadedListener is added

After authenticating my XMPPTCPConnection. I am trying to create a Roster Object :

val roster = Roster.getInstanceFor(connectionObject)

// sometimes roster is already loaded even before adding a listener.

if (!roster.isLoaded) {

roster.addRosterLoadedListener(object : RosterLoadedListener {

override fun onRosterLoaded(roster: Roster?) {

// Do Something

})

}

override fun onRosterLoadingFailed(exception: Exception?) {

}

})

} else {

// Do Something

}

Is there a more clear way to achieve this?

Thanks.

Is there a more clear way to achieve this?
Trying to achieve what?

How about adding the listener before connecting the connection?

This is exactly what I would like to do but I cannot find any method for adding the listener before connecting the connection.

Hu? How about something like:

XMPPTCPConnection connection = new XMPPTCPConnection();

Roster roster = Roster.getInstanceFor(connection);

roster.addRosterLoadedListener();

connection.connect().login();