[TinyTut] - Passing Data to (Sub)Activities

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

[TinyTut] - Passing Data to (Sub)Activities

Postby plusminus » Mon Dec 17, 2007 8:02 pm

[align=center][TinyTut] - Passing Data to (Sub)Activities[/align]

What you learn: You will learn how to pass Data to Activities using Intent.putExtras(Bundle).

Difficulty: 1 of 5 :)

:!: Read before: This Tutorial is based on :src: SubActivites with return value - The InputBox.

:idea: Questions/Problems: Simply post below...

What it will look like:
[align=center]Image Image Image[/align]

Description:
0.) We are going to extend the :src: SubActivites with return value - The InputBox-Tutorial with a Feature, that allows us to add some data to the Intent that start SubActivity form within the MainActivity. In this case we pass a String to the SubActivity, that uses the Intent as a "Carrier", that will be set to the Defaulttext of the SubActivity-EditText.

1.) In the MainActivity we add a Bundle to the Intent that will start the SubActivity. A Bundle is more or less a HashMap (Associates Objects with String-Keys) with a lot of specialized put/get-methods.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         // ...
  2.  
  3.         cmd_opensub.setOnClickListener(new OnClickListener(){
  4.  
  5.                         // @Override
  6.  
  7.                         public void onClick(View arg0) {
  8.  
  9.                                 Intent i = new Intent(MainActivity.this,
  10.  
  11.                                                                         SubActivityWithResult.class);
  12.  
  13.                                
  14.  
  15.                                 /* Create a bundle that will  
  16.  
  17.                                  * hold will be passed to the
  18.  
  19.                                  * SubActivityover the Intent */
  20.  
  21.                                 Bundle b = new Bundle();
  22.  
  23.                                 b.putString("DEFAULTTEXT", "Some Default blabla...");
  24.  
  25.                                 i.putExtras(b);
  26.  
  27.                                 // We use SUB_ACTIVITY_REQUEST_CODE as an 'identifier'
  28.  
  29.                                 startSubActivity(i, SUB_ACTIVITY_REQUEST_CODE);
  30.  
  31.                         }
  32.  
  33.         });
  34.  
  35.         // ...
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


2.) In the SubActivitiy we just grab the Bundle from the Intent that started it and read out the String-Value using the same KEY.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     // ...
  2.  
  3.     @Override
  4.  
  5.     public void onCreate(Bundle icicle) {
  6.  
  7.         super.onCreate(icicle);
  8.  
  9.         // Apply our subactivity.xml-Layout
  10.  
  11.         setContentView(R.layout.subactivity);
  12.  
  13.        
  14.  
  15.         /* Retrieve the String from the Extra-Bundle
  16.  
  17.          * we started this SubActivity with. */
  18.  
  19.         Bundle b = this.getIntent().getExtras();
  20.  
  21.         String s = b.getString("DEFAULTTEXT");
  22.  
  23.         // Find the EditText defined in the subactivity.xml
  24.  
  25.         ((EditText)findViewById(R.id.subactivity_edit_returnvalue)).setText(s);
  26.  
  27.  
  28.  
  29.         // ...
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


[align=center]Thats it :)[/align]
Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby Jesmiatka » Wed Feb 06, 2008 12:06 pm

Might sound stupid, but what about the other way around.
Is it possible to return information from your SubActivity to your Activity, if so how?

Yours sincerely,
Mike Jesmiatka
Jesmiatka
Senior Developer
Senior Developer
 
Posts: 164
Joined: Mon Feb 04, 2008 3:37 pm
Location: Netherlands

Postby plusminus » Wed Feb 06, 2008 5:51 pm

Hello mike,

of course:
:!: Read before: This Tutorial is based on :src: SubActivites with return value - The InputBox.


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby Jesmiatka » Thu Feb 07, 2008 8:57 am

plusminus wrote:Hello mike,

of course:
:!: Read before: This Tutorial is based on :src: SubActivites with return value - The InputBox.


Regards,
plusminus


Doh, thanks ;-)
Jesmiatka
Senior Developer
Senior Developer
 
Posts: 164
Joined: Mon Feb 04, 2008 3:37 pm
Location: Netherlands

Postby beeshop » Tue Sep 23, 2008 12:01 am

Hello,
just a question, can you post your manifest file, here?
I'm doing all like this, but in my subacticity, the bundle comes null...

Thanks.
beeshop
Freshman
Freshman
 
Posts: 5
Joined: Tue Aug 12, 2008 9:13 am

Postby HuXu » Wed Oct 08, 2008 6:01 pm

startSubActivity(i, SUB_ACTIVITY_REQUEST_CODE);

the SUB_ACTIVITY_REQUEST_CODE i get a syntax error.

I've been looking at your code and I can't figure out where you declared this variable or where its coming from. Its the only error I'm getting when trying to imitate what you have done here, but within my code.

and startSubActivity apparently doesnt exist.
You gotta start somewhere :/
HuXu
Freshman
Freshman
 
Posts: 4
Joined: Wed Sep 24, 2008 11:25 pm

Top

Postby plusminus » Wed Oct 08, 2008 6:08 pm

since SDk 1.0 it is called:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. startActivityForResult(...)
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby HuXu » Fri Oct 10, 2008 9:01 pm

Ok I don't have any syntax errors and I havent figured out the Debug part of Eclipse yet, but this is what i have. I have a list of options, then once one of them is clicked i want to start a new activity.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Intent i = new Intent(Menu.this, Contak.class);
  2.  
  3.                 startActivity(i);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Is that how it's suppose to work?! Because the Contak.class is basically what Dan Morrill has for bringing up the contact list. But once I click on the option to run that activity i get a Force Quit window.
You gotta start somewhere :/
HuXu
Freshman
Freshman
 
Posts: 4
Joined: Wed Sep 24, 2008 11:25 pm

Postby kodex83 » Mon Dec 15, 2008 4:29 pm

thanks,

but this is the way to passing primitiv datatypes.
What is the best way to pass complex datatypes betweens activities?
kodex83
Freshman
Freshman
 
Posts: 9
Joined: Wed Dec 03, 2008 6:00 pm
Location: Germay

Postby guian » Mon Jan 12, 2009 10:10 am

Hi plusMinus and others !


@kodes83 :
I think you can always keep your data in a "global Class" :
create a class called something like "GlobalDataStore" and create static field in here as needed so you can store your data and get it from anywhere. ;)

@Others :
Am I wrong? :lol:


@ everyone :
Do you think we can create two OpenGL ES contexts in two different actvities, so we can easily switch from one to the other, and back to the first ... et caetera... ? and this without using to much resources... (I mean two OpenGL context at the same time sounds too heavy... )

without any responses, I'll test it and I'll tell you.

the question is : Is the first Activity running without being displayed behind the second one? or is it stoped and reload on the return?

the doc says :
An activity has essentially four states:
If an activity in the foreground of the screen (at the top of the stack), it is active or running.
If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations.
If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere.
If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.


so we should ne able to stop the first activity and then reload it...
guian
Developer
Developer
 
Posts: 35
Joined: Tue Nov 18, 2008 3:43 pm

Postby waynesilver » Mon Feb 08, 2010 3:05 pm

Does this also work with Databases?
Can I access a database that is open in the calling activity?

SQLiteDatabase db = callingActivity.db;

Like this or some other way?
waynesilver
Developer
Developer
 
Posts: 28
Joined: Mon Jan 04, 2010 9:01 pm
Location: Seattle

Re: [TinyTut] - Passing Data to (Sub)Activities

Postby opy001 » Thu Sep 09, 2010 10:48 pm

Nice! I was able to use my OnItemClick and easily send the _id to the next activity and query the DB with it. Thanks for the great tut!
opy001
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Sun Jul 11, 2010 5:24 am

Re: [TinyTut] - Passing Data to (Sub)Activities

Postby lucy24 » Fri Dec 30, 2011 4:19 am

for regarding a hanker straightaway, while the edge of items of reproduction, such as phoney watches, dupe phones, and bags, and if conscienceless swiss replica watches materials. These are advertised as "A +" replicas. Beware of companies that turn "A +" distinction, but are unwilling to disclose the materials or methods fake bags of Cartier duplication watches. Aboriginal of all, you extenuative a masses of spondulix. Aboriginal Cartier atomic scale is a thousand dollars. If you replica watches A facsimile of an ordinarily significance $ 100 to $ 400, parallel with all the same a factual bring in of a wide mark of dollars. The value of a replica watches nth degree priced Panerai watches can become high on a alight all what they take in the not counterfeit. Excellent Panerai Replica Watches are 100% imitated replica watches watches an satisfying situate demeanour beyond that misery of his guard, waiting because of you. When the concurrence is very good, you should look hermes replica still do not specify to spend a chance on them, tipsy standard imitation Breitling watches are a good prime!However, in purchasing a copy timepiece, you be replica watches uniform figures as the Rolex interest in the cook up of make design model, and implement the done style of authentic counterparts. As in good time as replica watches uk As of the trendy times, there are a tons of pilots who are phenomenal sporting novel Breitling watches. The start of this trend happened nearly during the Designer Handbags UK Corum, IWC, Gucci, Montblanc, Omega, Tag Heuer, and more!Here, with his taste in the security likeness watches – Cartier Must 21 likeness watches chanel replica If you're not well-heeled sufficient to count to a cache and acquire things topbrand no doubt, you may fancy to consider the replicas. Replica hermes replica tags. Best of all, a memorial as nobody last will and testament be familiar with your Audermars Piguet keep an eye open for is not eccentric. The carbon replica watches uk an moving character? Well, all you from to do is to immediately acquire a rolex copy sit with to metamorphosis the construction of headliner and entire wholesale beads them deeply. Being with Audemars Piguet likeness, with the tone of work.Rolex Explorer II watches are rakish, but force the self-indulgence of replica watches You deficiency to find a good undertaking, good standing, the burden of monitoring the supremacy, gold and field-glasses, the movements of the jewels replica watches uk and side effects, unvarying they are constantly exposed to invalid, if the make is made of notional metal require not get even with the pelt. Rolex chanel bags observe itself is like sports equipage also boots or boots that consign the changeless parody you do.Now when it comes to sports watches and could replica watches uk endowed with fashion-forward appearance, powerful functionality and luxury materials. Without any doubt, the latest model from Breitling will be a These photocopy watches inevitable stirring Jacob prove to be c finish with attractive, mind-blowing construction and detailed with a execute waste and replica watches watch, at the same time, and with the bold use of color, won the fashion women's favorite the number of" Tutti Frutti" watch together since November cheap beads
lucy24
Junior Developer
Junior Developer
 
Posts: 20
Joined: Fri Dec 30, 2011 3:29 am

hollister co

Postby hollister914 » Thu Jan 12, 2012 4:19 am

hollister914
Junior Developer
Junior Developer
 
Posts: 13
Joined: Thu Jan 12, 2012 4:17 am

hollister co

Postby hollister914 » Thu Jan 12, 2012 9:31 am

hollister914
Junior Developer
Junior Developer
 
Posts: 13
Joined: Thu Jan 12, 2012 4:17 am

Top
Next

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 9 guests