#StackBounty: #android #listview #android-8.0-oreo #screen-rotation Oreo – issue when rotating a device within a fragment with a listView

Bounty: 50

I’m creating a listview in a fragment and rotate the device. It works well except on Oreo.

At runtime it does not complain and I see my list. Then I rotate and I don’t see the list anymore. Prior to Oreo the code below works as it is: after rotating the device I can still see my list correctly.

To make the small example works I had to add the re-creation of the list (code commented out so that you can test with lower than O version) in onResume.

It seems that I’m missing a call to refresh the view when I rotate. I get a blank screen in place of my ListView.

I can’t find some oreo documentation that explains the way to deal with listview on device’s rotation? or tuto/example? I use a List/adapter mechanism.

What has changed in Oreo?

Fragment code:

public class ListFragment extends Fragment {
    private ListView listView;
    private List<ListItems> listItems;
    private ListAdapter listAdapter;


    public ListFragment() {
        // Required empty public constructor
    }

    @Override
    public void onResume() {
        Log.i("ROT","onResume");
/*
        String[] name = {"linux","win","android" };

        listItems = new ArrayList<>();
        listAdapter = new ListAdapter(getActivity().getApplicationContext(), listItems);
        listView = getActivity().findViewById(R.id.listview);

        for(int i=0;i<name.length;i++){
            ListItems items = new ListItems();
            items.setOsName(name[i]);
            listItems.add(items);
            listView.setAdapter(listAdapter);
        }
*/
        super.onResume();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.i("ROT","onCreateView");
        super.onCreateView(inflater, container, savedInstanceState);
        return inflater.inflate(R.layout.layout_fragment, null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        //  public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Log.i("ROT","onActivityCreated");

        String[] name = {"linux","win","android" };

        listItems = new ArrayList<>();
        listAdapter = new ListAdapter(getActivity().getApplicationContext(), listItems);
        listView = getActivity().findViewById(R.id.listview);

        for(int i=0;i<name.length;i++){
            ListItems items = new ListItems();
            items.setOsName(name[i]);
            listItems.add(items);
            listView.setAdapter(listAdapter);
        }

    }

}

Here is my Main:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ListFragment listFragment = new ListFragment();
        ft.replace(R.id.content_frame, listFragment);
        ft.addToBackStack(null);
        ft.commit();
    }
}

The items creation:

    public class ListItems {
        private String osName;

        String getOsName(){
            return osName;
        }

        void setOsName(String s){
            osName = s;
        }
    }

The adapter:

public class ListAdapter extends BaseAdapter {
    private static List<ListItems> listItems;
    private LayoutInflater layoutInflater;

    ListAdapter(Context context, List<ListItems> objects) {
        listItems = objects;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return listItems.size();
    }

    @Override
    public Object getItem(int position) {
        return listItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private class ViewIPHolder {
        TextView os;
    }

    @SuppressLint("InflateParams")
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewIPHolder viewHolder;

        if(convertView == null) {
            viewHolder = new ViewIPHolder();

            convertView = layoutInflater.inflate(R.layout.layout_list, null);

            viewHolder.os = convertView.findViewById(R.id.osname);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewIPHolder) convertView.getTag();
        }

        viewHolder.os.setText(listItems.get(position).getOsName());

        return convertView;
    }


}

and finally the xml layout.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>

layout_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/listview"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="32dp"
        android:layout_marginEnd="32dp"
        android:layout_marginStart="32dp"
        android:layout_marginTop="32dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

layout_list.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/osname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="TextView"
        android:textColor="@color/colorPrimary"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.