Tutorial for bundle,parcel

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Tutorial for bundle,parcel

Postby radhika » Mon Jun 09, 2008 2:14 pm

Hi PlusMinus,

Can you give a tutorial for the following:

1. How o create a bundle?
2. How to create a parcel? what are the parameters required to create it?
3. How to use the parcelable interface?
4. How to pass a bundle with a parcel to another activity and how to recover the data in the parcel in received activity?

Pleae help as am unable to do any of this.

Thanks in advance.
Thank you
Regards
radhika
Desire to learn
radhika
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed May 14, 2008 8:09 am
Location: India

Top

Postby bavarol » Tue Jun 10, 2008 6:03 pm

That's an example for passing data between Activities, the only purpose is you understand how it works, the task self is rough, I have just passed Photo Path in my Programm, NOT the Bitmap but it worked in any case.

It's very important to see what Interface is implemented by the Class of the objet to be passed, in this case, Bitmap implements Parcelable.

On the MainActivity:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.    Intent intent = new Intent(CameraPreview.this, EditPhoto.class);
  2.  
  3.  
  4.  
  5.    Bundle bundle = new Bundle();
  6.  
  7.    bundle.putParcelable("picture", myPic);
  8.  
  9.    intent.putExtras(bundle);
  10.  
  11.    
  12.  
  13.    startSubActivity(intent, SUB_ACTIVITY_REQUEST_CODE);
  14.  
  15.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


On the Subactivity:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.        
  2.  
  3.  
  4.  
  5.         Bundle bundle = this.getIntent().getExtras();
  6.  
  7.         Bitmap myPic=null;
  8.  
  9.         if ( bundle != null ) {
  10.  
  11.            myPic = (Bitmap)bundle.getParcelable("picture");
  12.  
  13.         }
  14.  
  15.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


I hope you have understand all.

C u ;)
bavarol
Experienced Developer
Experienced Developer
 
Posts: 52
Joined: Mon Dec 10, 2007 9:19 pm

Postby coding_android » Thu Jun 19, 2008 3:30 pm

Thank you for that. If I understand the life cycle right I have to save my actual status of my activity in a bundle when the onFreeze method is called. If I have a list of photos that I've downloaded from the internet shown in a Gallery. How should I store the status of that Gallery the right way? (the users could have scrolled down a bit in the Gallery) Should I store the Gallery itself into a Bundle or what is the right way to achieve that?

--

After reading through all the documentation again I would describe it the following way:

The system saves the actual status of the different default views automatically. I just have to implement the onFreeze methode an do something like

super.onFreeze(outState)

and in the onCreate methode:

super.onCreate(icicle)

Everything besides the views I have to put into the outState bundle.

Is that right and what things do I have to save by myself?

Regards!
User avatar
coding_android
Moderator
Moderator
 
Posts: 76
Joined: Mon May 05, 2008 10:22 am
Location: Germany

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 5 guests