onSaveInstanceState方法中的空Bundle

时间:2016-10-03 15:29:29

标签: android onsaveinstancestate

我在gridview中使用凌空显示来自moviesdb的图片。下一步是在横向模式中也有gridview。所以我的逻辑背后是将包含数据的列表作为键值对存储在onSaveInstantState方法中:

 @Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    outState.putParcelableArrayList("movies", moviesList);
    outState.putStringArrayList("images", images);
    Log.v("TAG","list size:" + moviesList.size());

}

日志输出给出的大小为20,这是正确的。但是,当我使用调试器时,行

    outState.putParcelableArrayList("movies", moviesList);

给了我一个空包。这很奇怪!图像列表工作正常,可以看到其中的内容。有什么想法是错的吗?

这是我的代码。

public class MainFragment extends Fragment {
String BASE_URL = "http://api.themoviedb.org/3/discover/movie?api_key=my_key";
static public ArrayList<Movie> moviesList;
static public ArrayList<String> images;
static public MovieAdapter movieAdapter;
public static String imageIcon;
ImageView imageView;

static public String lastSortOrder;
static GridView gridview;

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

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(savedInstanceState != null) {

        moviesList = savedInstanceState.getParcelableArrayList("movies");
        images = savedInstanceState.getStringArrayList("images");
    }else{
        moviesList = new ArrayList<>();
        images = new ArrayList<>();
        movieAdapter = new MovieAdapter(getActivity());
        updateMovies();
    }
    super.onCreate(savedInstanceState);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    setHasOptionsMenu(true);

    moviesList = new ArrayList<>();
    images = new ArrayList<>();
    gridview = (GridView) rootView.findViewById(R.id.gridview);
    int ot = getResources().getConfiguration().orientation;
    gridview.setNumColumns(ot == Configuration.ORIENTATION_LANDSCAPE ? 3 : 2);
    gridview.setAdapter(movieAdapter);
    return rootView;
}

private void updateMovies(){

    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
            BASE_URL,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.d("TAG", response.toString());

                    try {
                        JSONArray moviesArray = response.getJSONArray("results");

                        for (int i = 0; i < moviesArray.length(); i++) {
                            JSONObject movie = moviesArray.getJSONObject(i);
                            Movie movieItem = new Movie();
                            movieItem.setTitle(movie.getString("title"));
                            movieItem.setId(movie.getInt("id"));
                            movieItem.setBackdrop_path(movie.getString("backdrop_path"));
                            movieItem.setOriginal_title(movie.getString("original_title"));
                            movieItem.setOriginal_language(movie.getString("original_language"));
                            if (movie.getString("overview") == "null") {
                                movieItem.setOverview("No Overview was Found");
                            } else {
                                movieItem.setOverview(movie.getString("overview"));
                            }
                            if (movie.getString("release_date") == "null") {
                                movieItem.setRelease_date("Unknown Release Date");
                            } else {
                                movieItem.setRelease_date(movie.getString("release_date"));
                            }
                            movieItem.setPopularity(movie.getString("popularity"));
                            movieItem.setVote_average(movie.getString("vote_average"));
                            movieItem.setPoster_path(movie.getString("poster_path"));
                            if (movie.getString("poster_path") == "null") {
                                movieItem.setPoster_path(API.IMAGE_NOT_FOUND);
                            } else {
                                images.add(API.IMAGE_URL + API.IMAGE_SIZE_185 + movie.getString("poster_path"));

                            }

                            moviesList.add(movieItem);
                            Log.v("TAG","list size:" + moviesList.size());
                            movieAdapter.notifyDataSetChanged();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
        }
    });
    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(jsonObjReq);
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    outState.putParcelableArrayList("movies", moviesList);
    outState.putStringArrayList("images", images);
    //Log.v("TAG","list size:" + moviesList.size());

  }



}

谢谢,

西奥。

0 个答案:

没有答案