The problem is when orientation change your Activity
is recreated and when onCreate()
is called you are setting the adapter with the default list items.
If you don’t maintain different layout
for landscape/portrait, you can simply avoid the Activity
recreate by just adding configChanges
in manifest for your Activity
android:configChanges="orientation|screenSize" // add this line
This will avoid recreating the Activity
and same layout will be used to fit on screen width. Make sure your listView layout width is set to match_parent
.
If you still want your Activity
to recreate, then you need to remember the last selected filter when onSaveInstanceState
is called on orientation change
@Override
public void onSaveInstanceState(Bundle outState)= {
outState.putString(“selectedFilter","some name");
super.onSaveInstanceState(outState);
}
And then when onCreate
in called after rotate, you can get the selectedFilter name
String filterName = null;
if(savedInstanceState != null){
filterName = savedInstanceState.getString("lastFilter");
}
Finally , set the listView
with items based on the filter name.
if(filterName != null && filterName.equalsIgnoreCase("some name")){
// filtered list items
} else {
// default list items
}