1

In Activity1, I input some data like name and address. When I click the next button, there will be another input form. What I want to do is, when I click BACK, I will return to Activity1 and the data I entered there previously is shown.

HELP please :)

=============

UPDATED: Activity1

private void startActivityForResult()
    {
        TextView textname = (TextView) findViewById(R.id.username);
        TextView textaddress = (TextView) findViewById(R.id.useraddress);

        Intent intent = new Intent(this, GetInformation.class);
        //intent.putExtras(getIntent());

        intent.putExtra("username", textname.getText().toString());
        intent.putExtra("useradd", textaddress.getText().toString());
        startActivityForResult(intent, 0);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        TextView textname = (TextView) findViewById(R.id.username);
        TextView textaddress = (TextView) findViewById(R.id.useraddress);
        textname.setText(data.getStringExtra("returnname").toString());
        textaddress.setText(data.getStringExtra("returnadd").toString());
    }

Activity2

private void startActivityForResult()
{
    final String username;
    final String useraddress;
    Intent intent = getIntent();

    //intent.putExtras(getIntent());

    username = getIntent().getStringExtra("username");
    useraddress = getIntent().getStringExtra("useradd");

    intent.putExtra("returnname", username);
    intent.putExtra("returnadd", useraddress);

    setResult(0, intent);
}
oopsiee
  • 11
  • 2
  • 4
  • Why do you have methods named startActivityForResult ? – Gregory Jul 11 '11 at 16:55
  • [**Please See this Blog. This Can Help You**](http://startandroiddevelopment.blogspot.in/2013/11/how-to-pass-boolean-int-string-integer.html) –  Nov 04 '13 at 04:50

2 Answers2

3

There's a simple way to do this in Android : startActivityForResult. Basically, when you launch the activity, you say that you are expecting a result. The other activity can then add information that will be returned to the starting activity. Here's a very simple code sample from the official doc :

 public class MyActivity extends Activity {
     ...

     static final int PICK_CONTACT_REQUEST = 0;

     protected boolean onKeyDown(int keyCode, KeyEvent event) {
         if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
             // When the user center presses, let them pick a contact.
             startActivityForResult(
                 new Intent(Intent.ACTION_PICK,
                 new Uri("content://contacts")),
                 PICK_CONTACT_REQUEST);
            return true;
         }
         return false;
     }

     protected void onActivityResult(int requestCode, int resultCode,
             Intent data) {
         if (requestCode == PICK_CONTACT_REQUEST) {
             if (resultCode == RESULT_OK) {
                 // A contact was picked.  Here we will just display it
                 // to the user.
                 startActivity(new Intent(Intent.ACTION_VIEW, data));
             }
         }
     }
 }

You can get a much more complete description of all this on the Activity page in the official doc (section Starting Activities and Getting Results).

David Snabel-Caunt
  • 57,804
  • 13
  • 114
  • 132
Gregory
  • 4,384
  • 1
  • 25
  • 21
0

Save Activity1 state in method onSaveInstanceState, and then in method

void onCreate(Bundle savedInstanceState)

you can restore state, using savedInstanceState.

Or, if you want pass entered data to second activity, you can place data in intent. Sample:

Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.putExtra("Key", "Value");
startActivityForResult(i, 0);

in Second Activity you can get data:

 getIntent().getStringExtra("Key");

To return result from second activity:

Intent data = new Intent();
data.put("key", "value");
setResult(RESULT_OK, data);   

then you can retrieve data in first activity using

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        data.getStringExtra("key");
    }
CeKup
  • 989
  • 9
  • 6
  • when I go back to the first activity, the app closes. i updated my question (with code). i have no idea how to fix that... by the way, there are other activities in this application. i'm not sure but maybe the other intents affect it?? i'm so lost. haha. – oopsiee Jul 11 '11 at 16:48