Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to transfer data from one activity to another in android

Tags:

java

android

It might sound simple but I can't make it working. I have two activities. Which first one is a form, and second one is showing data from a JSON file based on values entered in the first activity.

So I am trying to make a simple version of it. I have a EditText, and a button, so when they press the button, whatever was in the EditText will be in the TextView of the next activity.

Here is my code so far: Main Activity

static TextView textView;

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

    textView = (TextView) findViewById(R.id.editText1);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
public void transferIT(View view){
    Intent intent = new Intent(this, Page.class);
    startActivity(intent);
}

public static String getText(){
    String data = (String) textView.getText();
    return data;
}

Main XML

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:ems="10"
    android:hint="Enter Text to transfer" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Transfer it"
    android:onClick="transferIT" />

Second Activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_page);
    // Show the Up button in the action bar.
    setupActionBar();


    TextView enteredValue = (TextView)findViewById(R.id.text);

    enteredValue.setText(MainActivity.getText());

}

Second XML

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="58dp" />

In this way I made a method for the data of the EditText and in the other activity I call it to get access to it. Currently when I press the button it stops working. So what is your solution for having the data of the form in other activity ?

like image 442
user3026034 Avatar asked Nov 24 '13 01:11

user3026034


People also ask

How can I transfer data from one activity to another without intent?

This example demonstrate about How to send data from one activity to another in Android without intent. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do you send data from first activity to third activity?

You can pass the value in 2 ways: Either you make a global Class and set the value in that class and access that class in your 3rd activity. You can use Intent to send your values from 1st activity to 2nd activity.

How do you pass data from one activity to another activity using intent?

The easiest way to do this would be to pass the session id to the signout activity in the Intent you're using to start the activity: Intent intent = new Intent(getBaseContext(), SignoutActivity. class); intent. putExtra("EXTRA_SESSION_ID", sessionId); startActivity(intent);


1 Answers

In first activity you should put extra argument to intent like this:

// I assume Page.class is your second ativity
Intent intent = new Intent(this, Page.class); 
intent.putExtra("arg", getText()); // getText() SHOULD NOT be static!!!
startActivity(intent);

Then in second activity you retrive argument like this:

String passedArg = getIntent().getExtras().getString("arg");
enteredValue.setText(passedArg);

It's also good to store "arg" String in MainActivity as constant and always refer to it in other places.

public static final String ARG_FROM_MAIN = "arg";
like image 113
fada21 Avatar answered Oct 04 '22 02:10

fada21