Basic help: Activities, intents and variables

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

Basic help: Activities, intents and variables

Postby Thomas W » Tue Sep 07, 2010 1:42 pm

I have been developing a very small and worthless app for my android phone in my spare time. I am stuck on giving a variable from one activity to another.

By looking online i have found out how to change to another activity, and i have implemented that - and it works - but the storing/retrieving of a variable (string, to be exact) across multiple activities is only ever mentioned in stride, and no one ever seems to elaborate on how exactly to do it.


What i am asking for, is a simple snippet of code, that shows how to do this. Preferably the code for both activities (the sending and the receiving ones) If you could please provide this code (or point be to a tutorial on how to do this) i would be grateful.
Thomas W
Freshman
Freshman
 
Posts: 3
Joined: Tue Sep 07, 2010 1:25 pm

Top

Re: Basic help: Activities, intents and variables

Postby achie1266 » Tue Sep 07, 2010 9:34 pm

in your first activity[A] you do something like this when you start the second activity [B]
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 Intent intent = new Intent(A.this, B.class);
  2.                 intent.putExtra("myString", "achie");
  3.                 startActivity(intent);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

and in the activity B you can retrieve this in this way.[this goes in the onCreate method of activity B]
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.               String str = null;
  2.                 str = savedInstanceState != null ? savedInstanceState.getString("myString") : null;
  3.                 if(str == null){
  4.                         Bundle extras = getIntent().getExtras();
  5.                         str = extras != null ? extras.getString("myString") : null;
  6.                 }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4

now str should have the value that you have passed from activity A. here for example it should be Achie.
achie
User avatar
achie1266
Master Developer
Master Developer
 
Posts: 223
Joined: Mon Nov 09, 2009 10:56 pm
Location: Denver

Re: Basic help: Activities, intents and variables

Postby Thomas W » Tue Sep 07, 2010 11:04 pm

Thank you, it worked exactly as i wanted it to work.
Thomas W
Freshman
Freshman
 
Posts: 3
Joined: Tue Sep 07, 2010 1:25 pm

Re: Basic help: Activities, intents and variables

Postby Thomas W » Fri Sep 10, 2010 12:05 am

I was using this to make a dec -> hex -> dec calculator. Now that I've finished all the coding it works, until you choose to do any basic math in the hex activity. If, for instance, you ask it to do 5 + 1, it will output 0x18p2, which is far off from 6, unless I'm missing something. The number is always in the 0x??p? format.

EDIT: I noticed that i'm sending a float to .toHexString(), and it says to use an int. I'm away from my home computer right now, but when i get back in a couple of hours i can test it with it set as an int, not a float.

EDIT2: It worked, and i have all that i want in funtionality in this program.
Thomas W
Freshman
Freshman
 
Posts: 3
Joined: Tue Sep 07, 2010 1:25 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 17 guests