0

Assuming I have 2 activities activity1 and activity2. In activity1, I have a simple form. Here is the xml file.

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">


<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="25dp"
        android:layout_marginTop="25dp"
        android:text="@string/m_informations"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/FirstName"
        android:textAppearance="?android:attr/textAppearanceMedium" />


    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName" >

        <requestFocus />

    </EditText>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/Name"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/Email"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textEmailAddress" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/confirm" />

</LinearLayout>

I want now use the datas of these Edittexts in activity2 knowing that there are no links between these two activities. I mean between these 2 activities, there are others activities starting.

And I want to use these datas in a specific function that I have created in activity2.

Let's say it's something like :

void write(String st)
{
 st = edittext1 value
}

st must for example take the value of edittext1 of the activity1

Thank you in advance!

Bob
  • 111
  • 2
  • 9
  • http://stackoverflow.com/questions/19286970/using-intents-to-pass-data-between-activities-in-android see this link – Illegal Argument Feb 04 '15 at 11:36
  • use intent to pass data, intent.putExtra http://developer.android.com/reference/android/content/Intent.html and http://www.vogella.com/tutorials/AndroidIntent/article.html – Syed Raza Mehdi Feb 04 '15 at 11:37
  • Check this http://developer.android.com/guide/components/intents-filters.html – Skynet Feb 04 '15 at 11:39
  • possible duplicate of [Passing data between activities in Android](http://stackoverflow.com/questions/2965109/passing-data-between-activities-in-android) – Muhammed Refaat Feb 04 '15 at 11:41

2 Answers2

1
Intent in = new Intent(activity1.this, activity2.class);
in.putExtra("message", message);
startActivity(in);

in acticity2 you get data like:

Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message");

If You are getting some data in activity1. But from activity1 you are calling activity3 and after that from activity3 you are calling activity2. And you want that data in activity2 then you can use sharepreference:

In activity1 where you get data you store data like:

SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("myName", "PPD");
editor.putInt("id", 1);
editor.commit();

And in activity2 you will get it as:

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 

  String name = prefs.getString("myName", "No Name");
  int idName = prefs.getInt("id", 0); 
PPD
  • 5,660
  • 12
  • 52
  • 86
1

Use Shared Preferences to store and access the data in another activity. In shared Preference we create a file only accessible to the current application in which we add key value pairs and then load them when needed

Ex. In activity 1:

EditText et1=(EditText) findViewById(R.id.editText1);
String et1_val=et1.getText().toString();
SharedPreferences pref=getSharedPreferences("MYPREF", 0);
//creating a shared preference with the name "MYPREF" and 0 is for default settings

final SharedPreferences.Editor editor=pref.edit();
//creating an editor to add the KeyValue Pairs to the preferences file.

editor.putString("et1_val",et1_val);
//adding the keyvalue pairs to the editior.

editor.commit();
//To close and commit the changes in the shared preference

In activity 2: Your method goes like this:

void write(String st)
{

SharedPreferences pref=getSharedPreferences("MYPREF", 0);
//getting reference to the shared preference we created.

String edit_text1_value=pref.getString("et1_val","");
//"" is the default value when the requested key has no value associated with it.

st=edit_text1_value;
//copiying the value of the edit text 1 into st
}

For more detiails pls visit this link: Shared Preferences in Android

Zapper
  • 102
  • 7
  • Indeed I'm using SharedPreferences, I'll try this and let you know. Thanks PPD and user2653581 – Bob Feb 04 '15 at 12:03