Reputation: 4817
I'm using a lib that uses an interface I pass by to communicate with the calling activity, the problem is once I rotate the device the new instance of the activity wouldn't receive the callback anymore. I was thinking in saving an instance of this lib in a AndroidViewModel, would I get a memory issue doing so because the instance of the lib references my activity?
Causes problem when rotating:
public class MainActivity implements Lib.Callback
{
private Lib mLib;
@Override
public void onCreate ( final Bundle savedInstanceState){
mLib = new Lib(this);
}
@Override
protected void callback() {
...
}
}
Makes it better if I put "mLib" in an AndroidViewModel? Or is there a better alternative for such a case?
Upvotes: 0
Views: 139
Reputation: 1508
If you do so you will leak the Activity. And you still will have only the old instance. Not the new one. Implement the callback for example in the ViewModel and then expose some LiveData from the ViewModel which the Activity can observe. It will work on device rotation.
Upvotes: 1
Reputation: 6107
Would I get a memory issue doing so because the instance of the lib references my activity?
Saving lib reference in ViewModel
will cause memory leak for your
scenario.As your lib has reference of Activity
, when you rotate your phone that lib will hold a reference of your old activity.
Upvotes: 0