Putting an object into a bundle

Put your problem here if it does not fit any of the other categories.

Putting an object into a bundle

Postby Grey Goo » Fri May 29, 2009 7:34 am

All the objects in my game are in a World object and I'm trying to set up the pause/resume part, but I can't figure out how to store the World object in a bundle. I noticed a get(String key) that will return a general Object, but I don't see any put methods for anything other than the regular data types.

Is there a way that I can put a World object into a bundle or should I do the pause/resume a different way?

Edit: Just to be clear I'm talking about automatically pausing when the program is interrupted and saving the current game then resuming it later as shown in the Snake sample code.
Grey Goo
Junior Developer
Junior Developer
 
Posts: 11
Joined: Mon May 11, 2009 3:56 am

Top

Postby padde » Fri May 29, 2009 11:07 am

There are two methods the first one is the way it should be done.. but is reeeeeeally a pain in the a**
an the second method is easy to use for the developer but more work for the system.


Method 1 aka pain in the a** Method
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package de.padde.test;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.os.Parcelable;
  6. import android.os.Parcel;
  7.  
  8. public class Test extends Activity {
  9.  
  10.     public void onCreate(Bundle savedInstanceState) {
  11.         super.onCreate(savedInstanceState);
  12.         setContentView(R.layout.main);
  13.        
  14.         if(!savedInstanceState.isEmpty()) {
  15.             SaveMe state = savedInstanceState.getParcelable("laststate");
  16.         }
  17.                
  18.     }
  19.    
  20.     protected void onPause() {
  21.         Bundle b = new Bundle();
  22.            b.putParcelable("laststate", new SaveMe());
  23.  
  24.            onSaveInstanceState(b);
  25.     }
  26. }
  27.  
  28. class SaveMe implements Parcelable {
  29.     static final int test = 1234;
  30.     private int user;
  31.     private String name;
  32.     private double score;
  33.     private SaveMeToo smt;
  34.    
  35.     public void writeToParcel(Parcel out, int flags) {
  36.         out.writeInt(user);
  37.         out.writeString(name);
  38.         out.writeDouble(score);
  39.         out.writeParcelable(smt, flags);
  40.     }
  41.  
  42.     public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
  43.         public SaveMe createFromParcel(Parcel in) { return new SaveMe(in); }
  44.         public SaveMe[] newArray(int size) { return new SaveMe[size]; }
  45.     };
  46.    
  47.     private SaveMe(Parcel in) {
  48.         user = in.readInt();
  49.         name = in.readString();
  50.         score = in.readDouble();
  51.         smt = in.readParcelable(getClass().getClassLoader());
  52.     }
  53.    
  54.     public SaveMe() {}
  55.  
  56.     public int describeContents() { return 0; }
  57. }
  58.  
  59. class SaveMeToo implements Parcelable {
  60.     private int moo;
  61.    
  62.     public void writeToParcel(Parcel out, int flags) {
  63.         out.writeInt(moo);
  64.     }
  65.    
  66.     public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
  67.         public SaveMeToo createFromParcel(Parcel in) { return new SaveMeToo(in); }
  68.         public SaveMeToo[] newArray(int size) { return new SaveMeToo[size]; }
  69.     };
  70.    
  71.     private SaveMeToo(Parcel in) {
  72.         moo = in.readInt();
  73.     }
  74.    
  75.     public int describeContents() { return 0; }
  76. }
  77.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4



Method 2
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package de.padde.test;
  2.  
  3. import java.io.Serializable;
  4.  
  5. import android.app.Activity;
  6. import android.os.Bundle;
  7.  
  8. public class Test extends Activity {
  9.  
  10.     public void onCreate(Bundle savedInstanceState) {
  11.         super.onCreate(savedInstanceState);
  12.         setContentView(R.layout.main);
  13.        
  14.         if(!savedInstanceState.isEmpty()) {
  15.             SaveMe state = (SaveMe) savedInstanceState.getSerializable("laststate");
  16.         }
  17.                
  18.     }
  19.    
  20.     protected void onPause() {
  21.         Bundle b = new Bundle();
  22.            b.putSerializable("laststate", new SaveMe());
  23.  
  24.            onSaveInstanceState(b);
  25.     }
  26. }
  27.  
  28. class SaveMe implements Serializable {
  29.     private static final long serialVersionUID = 1L;
  30.  
  31.     static final int test = 1234;
  32.     private int user;
  33.     private String name;
  34.     private double score;
  35.     private SaveMeToo smt;
  36.  
  37.     public SaveMe() {}
  38. }
  39.  
  40. class SaveMeToo implements Serializable {
  41.     private static final long serialVersionUID = 2L;
  42.    
  43.     private int moo;
  44.  
  45.     public SaveMeToo() {}
  46. }
  47.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Greets Padde
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby Grey Goo » Sun May 31, 2009 12:29 am

Alright, thanks. I'm gonna go with the non pain in the ass method :lol:
Grey Goo
Junior Developer
Junior Developer
 
Posts: 11
Joined: Mon May 11, 2009 3:56 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Yahoo [Bot] and 27 guests