2

So I have a Calendar obj which implements serializable by default and I want to pass it in a bundle so the value of it is saved on screen rotation.

Should I pass it as a serializable or extract the values and recreate the obj for efficiency?

bundle.putSerializable("key", calendar);

or

bundle.putInt("dayKey", calendar.get(Calendar.DAY_OF_MONTH));
bundle.putInt("monthKey", calendar.get(Calendar.MONTH));
bundle.putInt("yearKey", calendar.get(Calendar.YEAR));
user2968401
  • 925
  • 3
  • 14
  • 27
  • i will go with putSerializable. i dont know about the efficency, but its easier to implement and to read rather than explode the object into pieces. What if a serializable object have more than 10 attribut – Randyka Yudhistira Mar 02 '15 at 03:50
  • I would go with the simplest and least error prone possibility: `bundle.putInt("time", calendar.getTime().getTime());` – Xaver Kapeller Mar 02 '15 at 05:54

4 Answers4

2

Using Serializable is not recommended in Android; use Parcelable instead.

I would guess it depends on the size of the Object and how cumbersome it is to recreate manually.

Emmanuel
  • 13,083
  • 4
  • 39
  • 53
1

Go with Parcelable over Serializable. If performance matters, Parcelable could be around 10x faster (http://www.developerphil.com/parcelable-vs-serializable/)

Additionally, you can use https://github.com/johncarl81/parceler to avoid generating (and maintaining!) all the boilerplate code that would otherwise exist.

@Parcel
public class Person {
  ...
}

Parcelable parcelable = Parcels.wrap(new Person(..));
Person = Parcels.unwrap(parcelable);
Patrick
  • 2,077
  • 16
  • 12
0

Honestly, while Parcelable is definitely the preferred serialization method, I think for this case you're fine just putting it as a Serializable extra. Considering the amount of work that is done recreating the Activity, I think the expense of serializing a Calendar instance would be negligible.

If nothing else, it makes your code more maintainable and succinct.

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
0
  • onSaveInstanceState method is used for store Activity's state. and it called when activity you rotate the screen or press home button or open new activity from notification bar.
  • It called after onPause() method.
  • You can save your custom Object or any primitive data type i.e. called Activities's states as

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);        
        outState.putSerializable(KEY_CALENDAR, cal);        
    }
    
  • onRestoreInstanceState() method is used for restore your Activities's states.

  • It called after the onStart() method

  • You can Activities's state restore like

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);       
        cal = (Calendar) savedInstanceState.getSerializable(KEY_CALENDAR);
    }
    

    Here you can use serializable for save Activities's state.

  • You can also use onCreate() method for restore your saved state because bundle is same deliver in both method onCreate(), onRestoreInstanceState() like

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 
    
        if(savedInstanceState != null) {
            cal = (Calendar) savedInstanceState.getSerializable(KEY_CALENDAR);
        } else {
            cal = Calendar.getInstance();
            cal.setTime(new Date());    
        }
    }
    
Arun Kumar
  • 2,874
  • 1
  • 18
  • 15