Corrected Parceable Implementation. Wasn't aware that my code was not called. Had to force it to clear the app out of the memory.

This commit is contained in:
Christopher Beckmann 2016-01-31 12:46:22 +01:00
parent 54bf935685
commit a4e7a693f9
5 changed files with 20 additions and 14 deletions

View file

@ -54,7 +54,7 @@ public class GameController implements IModelChangedListener, Parcelable {
private int sectionWidth; private int sectionWidth;
private int usedHints = 0; private int usedHints = 0;
private GameBoard gameBoard; private GameBoard gameBoard;
private int[] solution; private int[] solution = new int[0];
private GameType gameType; private GameType gameType;
private GameDifficulty difficulty; private GameDifficulty difficulty;
private CellConflictList errorList = new CellConflictList(); private CellConflictList errorList = new CellConflictList();
@ -699,6 +699,7 @@ public class GameController implements IModelChangedListener, Parcelable {
out.writeInt(usedHints); out.writeInt(usedHints);
out.writeInt(time); out.writeInt(time);
out.writeInt(solution.length);
out.writeIntArray(solution); out.writeIntArray(solution);
out.writeInt(noteStatus ? 1 : 0); out.writeInt(noteStatus ? 1 : 0);
@ -738,18 +739,18 @@ public class GameController implements IModelChangedListener, Parcelable {
usedHints = in.readInt(); usedHints = in.readInt();
time = in.readInt(); time = in.readInt();
solution = new int[in.readInt()];
in.readIntArray(solution); in.readIntArray(solution);
noteStatus = in.readInt() == 1; noteStatus = in.readInt() == 1;
notifiedOnSolvedListeners = in.readInt() == 1; notifiedOnSolvedListeners = in.readInt() == 1;
gameType = in.readParcelable(null); gameType = in.readParcelable(GameType.class.getClassLoader());
difficulty = in.readParcelable(null); difficulty = in.readParcelable(GameDifficulty.class.getClassLoader());
gameBoard = in.readParcelable(null); gameBoard = in.readParcelable(GameBoard.class.getClassLoader());
undoRedoManager = in.readParcelable(null); undoRedoManager = in.readParcelable(UndoRedoManager.class.getClassLoader());
removeAllListeners(); removeAllListeners();
} }
public void removeAllListeners() { public void removeAllListeners() {
@ -758,4 +759,9 @@ public class GameController implements IModelChangedListener, Parcelable {
hintListener = new LinkedList<>(); hintListener = new LinkedList<>();
timerListeners = new LinkedList<>(); timerListeners = new LinkedList<>();
} }
public void setContextAndSettings(Context applicationContext, SharedPreferences sharedPref) {
context = applicationContext;
setSettings(sharedPref);
}
} }

View file

@ -290,7 +290,7 @@ public class GameBoard implements Cloneable, Parcelable {
dest.writeInt(size); dest.writeInt(size);
for(int i = 0; i < field.length; i++) { for(int i = 0; i < field.length; i++) {
dest.writeParcelableArray(field[i], 0); dest.writeTypedArray(field[i], 0);
} }
} }
@ -308,7 +308,7 @@ public class GameBoard implements Cloneable, Parcelable {
/** recreate object from parcel */ /** recreate object from parcel */
private GameBoard(Parcel in) { private GameBoard(Parcel in) {
//private int id; //private int id;
gameType = in.readParcelable(null); gameType = in.readParcelable(GameType.class.getClassLoader());
sectionHeight = in.readInt(); sectionHeight = in.readInt();
sectionWidth = in.readInt(); sectionWidth = in.readInt();
size = in.readInt(); size = in.readInt();
@ -316,8 +316,7 @@ public class GameBoard implements Cloneable, Parcelable {
field = new GameCell[size][size]; field = new GameCell[size][size];
for(int i = 0; i < field.length; i++) { for(int i = 0; i < field.length; i++) {
// TODO: does this work?! field[i] = in.createTypedArray(GameCell.CREATOR);
field[i] = (GameCell[]) in.readParcelableArray(null);
} }
modelChangedListeners = new LinkedList<>(); modelChangedListeners = new LinkedList<>();

View file

@ -213,10 +213,10 @@ public class GameCell implements Cloneable, Parcelable {
dest.writeInt(row); dest.writeInt(row);
dest.writeInt(col); dest.writeInt(col);
dest.writeInt(value); dest.writeInt(value);
dest.writeInt(size);
dest.writeInt(fixed ? 1 : 0); dest.writeInt(fixed ? 1 : 0);
dest.writeInt(noteCount); dest.writeInt(noteCount);
dest.writeBooleanArray(notes); dest.writeBooleanArray(notes);
dest.writeInt(size);
} }
public static final Parcelable.Creator<GameCell> CREATOR public static final Parcelable.Creator<GameCell> CREATOR
@ -235,9 +235,10 @@ public class GameCell implements Cloneable, Parcelable {
row = in.readInt(); row = in.readInt();
col = in.readInt(); col = in.readInt();
value = in.readInt(); value = in.readInt();
size = in.readInt();
fixed = in.readInt() == 1; fixed = in.readInt() == 1;
noteCount = in.readInt(); noteCount = in.readInt();
notes = new boolean[size];
in.readBooleanArray(notes); in.readBooleanArray(notes);
size = in.readInt();
} }
} }

View file

@ -72,8 +72,7 @@ public enum GameType implements Parcelable{
dest.writeInt(resIDImage); dest.writeInt(resIDImage);
} }
public static final Parcelable.Creator<GameType> CREATOR public static final Parcelable.Creator<GameType> CREATOR = new Parcelable.Creator<GameType>() {
= new Parcelable.Creator<GameType>() {
public GameType createFromParcel(Parcel in) { public GameType createFromParcel(Parcel in) {
GameType g = GameType.values()[in.readInt()]; GameType g = GameType.values()[in.readInt()];
g.resIDString = in.readInt(); g.resIDString = in.readInt();

View file

@ -103,6 +103,7 @@ public class GameActivity extends AppCompatActivity implements NavigationView.On
// in case we get the same object back // in case we get the same object back
// because parceling the Object does not always parcel it. Only if needed. // because parceling the Object does not always parcel it. Only if needed.
gameController.removeAllListeners(); gameController.removeAllListeners();
gameController.setContextAndSettings(getApplicationContext(), sharedPref);
gameSolved = savedInstanceState.getInt("gameSolved") == 1; gameSolved = savedInstanceState.getInt("gameSolved") == 1;
} }