restore movielist on config change
This commit is contained in:
@@ -42,6 +42,8 @@ public class MovieListFragment extends Fragment implements Callback<MovieList> {
|
|||||||
*/
|
*/
|
||||||
private int mSortingOrder = TMDBApi.SORT_ORDER_MOST_POPULAR;
|
private int mSortingOrder = TMDBApi.SORT_ORDER_MOST_POPULAR;
|
||||||
|
|
||||||
|
private MovieList mMovieList;
|
||||||
|
|
||||||
RecyclerView mMovieGridView;
|
RecyclerView mMovieGridView;
|
||||||
ProgressBar mProgressBar;
|
ProgressBar mProgressBar;
|
||||||
|
|
||||||
@@ -70,8 +72,11 @@ public class MovieListFragment extends Fragment implements Callback<MovieList> {
|
|||||||
super.onActivityCreated(savedInstanceState);
|
super.onActivityCreated(savedInstanceState);
|
||||||
mMovieGridView.setLayoutManager(new GridLayoutManager(getActivity(), getNumColumns()));
|
mMovieGridView.setLayoutManager(new GridLayoutManager(getActivity(), getNumColumns()));
|
||||||
|
|
||||||
if(savedInstanceState != null)
|
if(savedInstanceState != null) {
|
||||||
mSortingOrder = savedInstanceState.getInt("sort_order");
|
mSortingOrder = savedInstanceState.getInt("sort_order");
|
||||||
|
if(savedInstanceState.containsKey("movielist"))
|
||||||
|
mMovieList = savedInstanceState.getParcelable("movielist");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -90,7 +95,18 @@ public class MovieListFragment extends Fragment implements Callback<MovieList> {
|
|||||||
@Override
|
@Override
|
||||||
public void onStart() {
|
public void onStart() {
|
||||||
super.onStart();
|
super.onStart();
|
||||||
loadMovieList();
|
initMovieList();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sets the adapter from the restored movie list if the instance state is restored
|
||||||
|
* starts the download of the new movie list if no restored one is present
|
||||||
|
*/
|
||||||
|
private void initMovieList() {
|
||||||
|
if(mMovieList == null)
|
||||||
|
loadMovieList();
|
||||||
|
else
|
||||||
|
mMovieGridView.setAdapter(new MovieListAdapter( (MovieNavigation) getActivity(), mMovieList));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -128,8 +144,10 @@ public class MovieListFragment extends Fragment implements Callback<MovieList> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(Response<MovieList> response, Retrofit retrofit) {
|
public void onResponse(Response<MovieList> response, Retrofit retrofit) {
|
||||||
if(response.isSuccess())
|
if(response.isSuccess()) {
|
||||||
setListToGrid(response.body());
|
mMovieList = response.body();
|
||||||
|
setListToGrid(mMovieList);
|
||||||
|
}
|
||||||
mProgressBar.setVisibility(View.GONE);
|
mProgressBar.setVisibility(View.GONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -171,6 +189,8 @@ public class MovieListFragment extends Fragment implements Callback<MovieList> {
|
|||||||
@Override
|
@Override
|
||||||
public void onSaveInstanceState(Bundle outState) {
|
public void onSaveInstanceState(Bundle outState) {
|
||||||
outState.putInt("sort_order", mSortingOrder);
|
outState.putInt("sort_order", mSortingOrder);
|
||||||
|
if(mMovieList != null)
|
||||||
|
outState.putParcelable("movielist", mMovieList);
|
||||||
super.onSaveInstanceState(outState);
|
super.onSaveInstanceState(outState);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,16 @@
|
|||||||
package net.headlezz.udacityproject1.tmdbapi;
|
package net.headlezz.udacityproject1.tmdbapi;
|
||||||
|
|
||||||
|
import android.os.Parcel;
|
||||||
|
import android.os.Parcelable;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrapper class for list of movies
|
* Wrapper class for list of movies
|
||||||
*/
|
*/
|
||||||
public class MovieList {
|
public class MovieList implements Parcelable {
|
||||||
|
|
||||||
public MovieList(List<Movie> movieList) {
|
public MovieList(List<Movie> movieList) {
|
||||||
this.movies = movieList;
|
this.movies = movieList;
|
||||||
@@ -15,4 +20,29 @@ public class MovieList {
|
|||||||
|
|
||||||
public List<Movie> getList() { return movies; }
|
public List<Movie> getList() { return movies; }
|
||||||
|
|
||||||
|
/***** Parcelable implementation ******/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int describeContents() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeToParcel(Parcel parcel, int i) {
|
||||||
|
parcel.writeList(movies);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final Parcelable.Creator<MovieList> CREATOR = new Parcelable.Creator<MovieList>() {
|
||||||
|
public MovieList createFromParcel(Parcel src) {
|
||||||
|
return new MovieList(src);
|
||||||
|
}
|
||||||
|
|
||||||
|
public MovieList[] newArray(int size) {
|
||||||
|
return new MovieList[size];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private MovieList(Parcel src) {
|
||||||
|
movies = src.readArrayList(Movie.class.getClassLoader());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user