[ACCEPTED]-How to pass the values from one activity to previous activity-android-activity

Accepted answer
Score: 248

To capture actions performed on one Activity 14 within another requires three steps.

Launch 13 the secondary Activity (your 'Edit Text' Activity) as 12 a subactivity by using startActivityForResult from your main Activity.

Intent i = new Intent(this,TextEntryActivity.class);    
startActivityForResult(i, STATIC_INTEGER_VALUE);

Within 11 the subactivity, rather than just closing 10 the Activity when a user clicks the button, you 9 need to create a new Intent and include 8 the entered text value in its extras bundle. To 7 pass it back to the parent call setResult before 6 calling finish to close the secondary Activity.

Intent resultIntent = new Intent();
resultIntent.putExtra(PUBLIC_STATIC_STRING_IDENTIFIER, enteredTextValue);
setResult(Activity.RESULT_OK, resultIntent);

The 5 final step is in the calling Activity: Override 4 onActivityResult to listen for callbacks from the text entry 3 Activity. Get the extra from the returned 2 Intent to get the text value you should 1 be displaying.

public void onActivityResult(int requestCode, int resultCode, Intent data) {     
  super.onActivityResult(requestCode, resultCode, data); 
  switch(requestCode) { 
    case (STATIC_INTEGER_VALUE) : { 
      if (resultCode == Activity.RESULT_OK) { 
      String newText = data.getStringExtra(PUBLIC_STATIC_STRING_IDENTIFIER);
      // TODO Update your TextView.
Score: 10

There are couple of ways by which you can 44 access variables or object in other classes 43 or Activity.

A. Database

B. shared preferences.

C. Object 42 serialization.

D. A class which can hold 41 common data can be named as Common Utilities 40 it depends on you.

E. Passing data through 39 Intents and Parcelable Interface.

It depend 38 upon your project needs.

A. Database

SQLite is an 37 Open Source Database which is embedded into 36 Android. SQLite supports standard relational 35 database features like SQL syntax, transactions 34 and prepared statements.

Tutorials -- http://www.vogella.com/articles/AndroidSQLite/article.html

B. Shared Preferences

Suppose 33 you want to store username. So there will 32 be now two thing a Key Username, Value Value.

How to store

 // Create object of SharedPreferences.
 SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
 //now get Editor
 SharedPreferences.Editor editor = sharedPref.edit();
 //put your value
 editor.putString("userName", "stackoverlow");

 //commits your edits

Using putString(),putBoolean(),putInt(),putFloat(),putLong() you can save your desired dtatype.

How to fetch

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
String userName = sharedPref.getString("userName", "Not Available");


C. Object Serialization

Object 31 serlization is used if we want to save an 30 object state to send it over network or 29 you can use it for your purpose also.

Use 28 java beans and store in it as one of his 27 fields and use getters and setter for that

JavaBeans 26 are Java classes that have properties. Think 25 of properties as private instance variables. Since 24 they're private, the only way they can be 23 accessed from outside of their class is 22 through methods in the class. The methods 21 that change a property's value are called 20 setter methods, and the methods that retrieve 19 a property's value are called getter methods.

public class VariableStorage implements Serializable  {

    private String inString ;

    public String getInString() {
        return inString;

    public void setInString(String inString) {
        this.inString = inString;


Set 18 the variable in you mail method by using

VariableStorage variableStorage = new VariableStorage();

Then 17 use object Serialzation to serialize this 16 object and in your other class deserialize 15 this object.

In serialization an object can 14 be represented as a sequence of bytes that 13 includes the object's data as well as information 12 about the object's type and the types of 11 data stored in the object.

After a serialized 10 object has been written into a file, it 9 can be read from the file and deserialized 8 that is, the type information and bytes 7 that represent the object and its data can 6 be used to recreate the object in memory.

If 5 you want tutorial for this refer this link


Get variable in other classes

D. CommonUtilities

You 4 can make a class by your self which can 3 contain common data which you frequently 2 need in your project.


public class CommonUtilities {

    public static String className = "CommonUtilities";


E. Passing Data through Intents

Please refer this 1 tutorial for this option of passing data.


Score: 6

you don't have to...

Just call newIntent() from 2 second activity

Intent retData=new Intent();

Add data to pass back

putExtras (retData.putExtra("userName", getUsrName()));

Go 1 ahead with setResult

setResult(RESULT_OK, retData);

And can then finish

Score: 4


And here's a link from the SDK with more 2 information:


and scroll down to the part 1 titled "Returning a Result from a Screen"

Score: 2

I often use static variables in the calling 10 activity with static setter methods to set 9 them.

In this way I can change values in 8 any activity at will, regardless of the 7 exact flow of control between the various 6 activities.

Note that this trick can only 5 be used if you don't care about the instantiation 4 of more than one copy of the same activity 3 (class) in the application, yet I found 2 this to be the easiest to implement, and 1 I use it the most.

Score: 0

The best way to do here is to put variable 8 to a common class which is defined outside 7 the scope

public class Utils 
    public static String mPosition;

inside your code (e.g. OnButtonClick 6 etc...)

Intent intent = new Intent(Intent.ACTION_PICK, 
Utils.mPosition = mViewData.mPosition + "";
LogHelper.e(TAG, "before intent: " + Utils.mPosition);
startActivityForResult(intent, Keys.PICK_CONTACT);

inside the code of

@Override public 5 void onActivityResult(int requestCode, int 4 resultCode, Intent data) { if 3 (requestCode == Keys.PICK_CONTACT) { if 2 (resultCode == Activity.RESULT_OK) { Uri 1 contactData = data.getData();

            //you may use the variable here after intent result
            LogHelper.e(TAG, "after intent: " + Utils.mPosition);

More Related questions