Bob
Bob

Reputation: 111

How to use data of an activity in an other?

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!

Upvotes: 0

Views: 84

Answers (2)

Zapper
Zapper

Reputation: 102

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

Upvotes: 1

PPD
PPD

Reputation: 5890

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); 

Upvotes: 1

Related Questions