Is it possible to extract a string in presenter class without context?

102 Views Asked by At

I am looking into extracting a string in a presenter class. There's a way to do it with context but it's not recommended to use context in presenter due to memory leaks and other issues. Is there a way I can do it without using context? Here's my code for example :

       testLine = when {
        sourceIds.isEmpty() -> ""
        sourceIds.size > 1 -> String.format(Locale.getDefault(), "%d test", sourceIds.size)
        else -> String.format(Locale.getDefault(), "%s", newMembers?.getNameForId(sourceIds[0]))
    }
1

There are 1 best solutions below

0
Anshul On

Best approach would be to drop context explicitly for example

Presenter

private var mContext : Context? = null

fun takeContext( context : Context ){
    this.mContext = context
}

fun dropContext(){
    this.mContext = null
}

calling above from

takeContext -> onViewCreated, dropContext -> onDestroyView

So that your context is only available when your view is drawed.