in below code , may there any memory leak problem ?.is there a risk for activity?.this code holds an implicit reference of activity in listview so that may it make a leak memory problem on rotation or opening new activity?
adapter = new BaseAdapter() {
@Override
public int getCount() {
return searchList == null ? 0 : searchList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = new TextView(getApplicationContext());
}
((TextView) convertView).setText(searchList.get(position));
return null;
}
};
myListView.setAdapter(adapter);
I don't think you have a leak problem here. But don't use ApplicationContext for that TextView, you should use the context the view will appear in.
Two solutions if you want to be super extra sure though.
Use an ArrayAdapter
If you use an ArrayAdapter, it will take care of pretty much all of this for you. It's a built in class made by google for use with simple list structures. Then you won't need to use the application context to create your view (which you shouldn't do, you should use the current activity as the context).
https://developer.android.com/reference/android/widget/ArrayAdapter
You probably want to use this constructor: https://developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter(android.content.Context,%20int,%20T[])
Set your adapter to null in your onDestroy() method.
If you set your adapter to null in OnDestroy, it will be GCed and not block GC of Activity. Just keep a private reference to your adapter in your class. You can do this along with number one as well, won't hurt anything.
I don't think either of these are really necessary, code like this is pretty common practice.