1

In the code below (shortened for clarity) when I rotate the device, the adapter variable receives null and gridView variable receives null.

Can anyone help me keep the adapter and gridView after rotating the device screen?

Thank.

(Sorry for bad english)

    public class FlickrXmlFragment extends Fragment {

    private GridView gridView;
    private FlickrGridViewAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        //...
        View view = darkInflater.inflate(R.layout.fragment_flickr_xml,
                container, false);
        //...
        gridView = (GridView) view.findViewById(R.id.flickr_gridView);

        return view;
    }

    class FlickrPhotoSearchThread extends
            AsyncTask<String, Void, List<FlickrImagem>> {

        @Override
        protected void onPreExecute() { //... process...    }

        @Override
        protected List<FlickrImagem> doInBackground(String... params) { //process... }

        @Override
        protected void onPostExecute(List<FlickrImagem> result) {
            //...
            adapter = new FlickrGridViewAdapter(getActivity(), 0, result);
            gridView.setAdapter(adapter);
            adapter.notifyDataSetChanged();
            //...
        }
    }
}
  • 2
    Fetch those values in `onResume` again and use it, since activity restarts on rotation, which means your data may be lost – Manoj Kumar Feb 05 '13 at 13:52
  • you might take a look at this link http://stackoverflow.com/questions/4584015/handle-screen-orientation-changes-when-there-are-asynctasks-running – Poonam Anthony Feb 05 '13 at 14:13

1 Answers1

-1

Handle the Orientation changes for your activity.

Please refer to this: http://developer.android.com/guide/topics/manifest/activity-element.html#config

Declare your activity like this:

<activity android:name=".MyActivity"
          android:configChanges="orientation|keyboardHidden"
          android:label="@string/app_name">
Shrikant Ballal
  • 7,067
  • 7
  • 41
  • 61
  • Thank you! I had only just declared android:configChanges="orientation" and was not working – Marcius Bezerra Feb 05 '13 at 14:06
  • You should probably save the state and then restore it, this method makes it so the activity doesn't restart. See [this](https://developer.android.com/guide/components/activities/activity-lifecycle.html#saras). – EmmanuelMess Jul 30 '17 at 17:35