Saving data on layouts

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

Saving data on layouts

Postby Bob.Nudd » Wed Jan 12, 2011 5:51 pm

Hi

I am trying to some basic stuff with android and having problems, what I am wanting to do is this:

1) App Starts > enter information in 2 textboxs
2) Click a button to show "page 2"
3) Click the "back" button on the phone to show page one with the information you entered

I can do 1 and 2 but when I press the back button the textboxes resets itself to empty fields, also I want to click the "back" button on page1 to close the app but that just goes back to page 2!


Any help is appreciated
Bob.Nudd
Freshman
Freshman
 
Posts: 4
Joined: Wed Jan 12, 2011 5:46 pm

Top

Re: Saving data on layouts

Postby Bob.Nudd » Wed Jan 12, 2011 7:46 pm

I have sorted it, I removed the code to overwrite the back button and it works, my next question is how do I set some text on page2 that's on page one I have tried this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public EditText page2txt;
  2. page2txt = (EditText) findViewById(R.id.widget33);  // this is on page2
  3. page2txt.setText("Text from Page 1");
  4.  
  5. Intent myIntent = new Intent(this, page2.class);
  6. startActivity(myIntent);
  7.  
  8.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
Bob.Nudd
Freshman
Freshman
 
Posts: 4
Joined: Wed Jan 12, 2011 5:46 pm

Re: Saving data on layouts

Postby Jems » Wed Jan 12, 2011 11:36 pm

You can add the String to a bundle to send it into the intent that you use to launch page2.

Intent myIntent = new Intent(this, page2.class);
Bundle b = new Bundle();
b.putString("key1", "Text from Page 1"); // the first arg is a just a key for the String in the second arg
myIntent.putExtras(b);
startActivity(myIntent);


You can then retrieve the String you sent to the second Activity by doing this:
Bundle b = this.getIntent().getExtras();
String s = b.getString("key1");
Jems
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Thu Dec 09, 2010 7:42 pm

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: Alexa [Bot], Yahoo [Bot] and 1 guest