How to pass data between Activities through intent

An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.

The App looks Like

Github: https://github.com/rincemathew/PassDatabetweenActivities

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:gravity="center"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/content"
        android:layout_margin="20dp"
        android:paddingBottom="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/button"
        android:text="go to next activity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    EditText content;
    Button button;
    String value;

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

        content=findViewById(R.id.content);
        button=findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //take the EditText content to variable 'value'
                value=content.getText().toString();
                //start intent
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                //pass the value
                intent.putExtra("PASS_VALUE", value);
                startActivity(intent);
            }
        });
    }
}

activity_main2.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main2Activity">

    <TextView
        android:id="@+id/textview"
        android:textSize="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

Main2Activity.java

public class Main2Activity extends AppCompatActivity {

    TextView textView;

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

        textView=findViewById(R.id.textview);
        //get Intent
        Intent intent = getIntent();
        //get the value to Sting variable
        String value = intent.getStringExtra("PASS_VALUE");
        // OR
       // String value = getIntent().getStringExtra("PASS_VALUE");

        //set the value to the TextView
        textView.setText(value);
    }
}

Keep exploring more tutorial from this site. In case of any queries make a comment. Happy coding!!!!

Add a Comment

Your email address will not be published. Required fields are marked *