Issue
I am just starting out with Android and I faced the following issue:
I am trying to saveInstanceState when the new Activity is opened, and I need to restore that state when Activity is back to Visible state.
Here is my onSaveInstanceState method
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelableArrayList("movies", (ArrayList<Movie>) movies);
Log.v("onSaveInstanceState ", "SAVING STATE");
Log.v("Saving State", outState.toString());
}
This is my onCreate method that should restore the previously saved instance state
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null){
Log.v("onCreate ", "EXISTING STATE");
this.movies = savedInstanceState.getParcelableArrayList("movies");
}else{
Log.v("onCreate ", "EMPTY STATE");
}
setHasOptionsMenu(true);
}
If you notice, in the onSaveInstanceState method, after I saved the instance I do toString() on the saved instance and it displays me the full instance with the values I saved. However when I am restoring the instance the savedInstanceState variable comes as null.
I am using an Array of custom objects where each object implements Parcelable.
Below is the class:
public class Movie implements Parcelable {
private String title;
private String path;
private String overview;
private String releaseDate;
private double vote;
public Movie(String title, String overview, double vote, String releaseDate, String path) {
this.title = title;
this.overview = overview;
this.vote = vote;
this.releaseDate = releaseDate;
this.path = path;
}
public String getTitle() {
return title;
}
public String getPath() {
return path;
}
public String getOverview() {
return overview;
}
public String getReleaseDate() {
return releaseDate;
}
public double getVote() {
return vote;
}
protected Movie(Parcel source){
this.title = source.readString();
this.path = source.readString();
this.overview = source.readString();
this.releaseDate = source.readString();
this.vote = source.readDouble();
}
public static final Creator<Movie> CREATOR = new Creator<Movie>() {
@Override
public Movie createFromParcel(Parcel source) {
return new Movie(source);
}
@Override
public Movie[] newArray(int size) {
return new Movie[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(title);
dest.writeSerializable(path);
dest.writeString(overview);
dest.writeString(releaseDate);
dest.writeDouble(vote);
}
@Override
public String toString() {
return "Movie{" +
"title='" + title + '\'' +
", path='" + path + '\'' +
", overview='" + overview + '\'' +
", releaseDate='" + releaseDate + '\'' +
", vote=" + vote +
'}';
}
}
I feel like I am saving it in the wrong way somehow. I followed the tutorial on how to do that. Please let me know what I am doing wrong. Thank you
Solution
My problem was resolved by adding to the MainActivity in the manifest file.
android:launchMode="singleTop"
There were multiple instances of Activity created, therefore instance would no be saved.
Answered By - Jenny

0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.