Thursday, August 30, 2012

Android - Passing Data Between Activities

Hello

Following is a source sample of passing strings back and forth between activities :
- MainActivity
- SubActivity

KeyPoints :
SubActivity is invoked via startActivityForResult.
Data is passed from MainActivity to SubActivity via the intent.putExtra.
Data is passed from SubActivity to MainActivity via the intent.putExtra and setResult.
Data is accessed via getStringExtra
setResult is invoke inside SubActivity onBackPressed;
MainActivity is aware of SubActivity finish via onActivityResult.

ActivitiyPassArgument.zip

MainActivity.java

package com.example.activitiypassargument;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void Click(View v)
    {
     Intent intent = new Intent(this,SubActivity.class);
     EditText editTextUserName = (EditText) findViewById(R.id.editTextUserName);
     intent.putExtra("USER_NAME", editTextUserName.getText().toString());
     this.startActivityForResult(intent, requestCode);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
     super.onActivityResult(requestCode, resultCode, data);
     TextView oTextView = (TextView)findViewById(R.id.textViewBackMail);
     String strMail = data.getStringExtra("MAIL");
     oTextView.setText(String.format("Mail from SubActivity : %s", strMail));
    }
    
    final public static int requestCode = 1;
}


SubActivity.java


package com.example.activitiypassargument;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.EditText;
import android.widget.TextView;

public class SubActivity extends Activity {

    @Override
 public void onBackPressed() {
  Intent oIntent = new Intent();
  EditText oEditText = (EditText)findViewById(R.id.editTextPhoneNumber);
  oIntent.putExtra("MAIL", oEditText.getText().toString());
  setResult(RESULT_OK,oIntent);
  super.onBackPressed();
 }

 

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sub);
        Intent intent = getIntent();
        String strUserName = intent.getStringExtra("USER_NAME");
        TextView textViewUserName = (TextView) findViewById(R.id.textViewUserNameWelcome);
        textViewUserName.setText(String.format("Welcome User : %s", strUserName));
    }
}

Main Activity looks like this :


The user enter his name : Nathan Krasney and click the Button. The user name is passed via the intent and SubActivity is started.


SubActivity looks like this :


Note that the welcomed user - Nathan Krasney was passed from MainActivity - circled !!!!



Now insert the e-mail

Click back - MainActivity is now :



Note that SubActivity has passed the mail to MainActivity  - circled !!!


The following app was published using this post.


Nathan

No comments:

Post a Comment