Search code examples
androidadapterweak-references

Should adapters in Android be static inner classes or non-static inner classes


I have a ListView in an Activity and I am setting a custom adapter to the ListView.

Should my adapter class be:

private static class MyAdapter extends ArrayAdapter 

or

private class MyAdapter extends ArrayAdapter

I guess it should make no difference as long as the adapter is enclosed within the activity reference but wanted to confirm that.


Solution

  • Holding onto the context is fine from within an adapter if you are careful about how you use the adapter. Adapters are usually tied to the lifecycle of their Context (an Activity) so it's fine. Use a WeakReference only if it makes sense.