Android passes parameters to the next Activity

1. Brief introduction

With Intent, we can move from one page to another. In this process, we can use Bundle packages to deliver messages. The structure of Bundle storing data is actually Map mapping. We just need to put parameters into Bundle, and then use Intent's putExtras method to drop Bundle into Intent. When receiving the parameters of the page, first call the getIntent method to get the Intent, then call the getExtras method to get the Bundle, and finally just like using Map to get the parameters.

2. Simple implementation

  1. Send parameter pages
  • activity_send_parameter.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SendParameterActivity"
    android:orientation="vertical">

    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Full name"
        />

    <EditText
        android:inputType="number"
        android:maxLength="3"
        android:id="@+id/et_age"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Age"
        />

    <EditText
        android:id="@+id/et_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:maxLength="11"
        android:hint="Telephone"/>

    <Button
        android:text="Pass parameters to the next page"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn_ok"/>
</LinearLayout>
  • SendParameterActivity.java
package xyz.strasae.androidlearn.my;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class SendParameterActivity extends AppCompatActivity {
    private EditText et_name;
    private EditText et_phone;
    private EditText et_age;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_parameter);
        et_name = findViewById(R.id.et_name);
        et_age = findViewById(R.id.et_age);
        et_phone = findViewById(R.id.et_phone);
        findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SendParameterActivity.this, ReceiveParameterActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("name", et_name.getText().toString());
                bundle.putInt("age", Integer.valueOf(et_age.getText().toString()));
                bundle.putString("phone", et_phone.getText().toString());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }
}
  1. Receive parameter page
  • activity_receive_parameter.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ReceiveParameterActivity"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_show"
        android:textColor="#000000"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:text="Used to receive parameters from the previous page TextView"
        />
</LinearLayout>
  • ReceiveParameterActivity.java
package xyz.strasae.androidlearn.my;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class ReceiveParameterActivity extends AppCompatActivity {
    private TextView tv_show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive_parameter);
        tv_show = findViewById(R.id.tv_show);
        Bundle bundle = this.getIntent().getExtras();
        String name = bundle.getString("name");
        String phone = bundle.getString("phone");
        int age = bundle.getInt("age");
        tv_show.setText(String.format("Full name:%s\n Telephone:%s\n Age:%d", name, phone, age));
    }
}

Tags: Android xml encoding Java

Posted on Mon, 07 Oct 2019 00:23:48 -0400 by Foser