Blog purpose for android basic example for android app developer. any query please my contact

Saturday 4 April 2020

How to Pass Data from One Activity to Another in Android

How to Pass Data from One Activity to Another in Android


In this tutorial, you'll learn to pass information from one activity to a different in humanoid with and while not mistreatment intent.

Basically, we are able to send information between activities in 2 ways in which.

Using Intent
Using international Variables
Below I actually have mentioned each of the strategies thoroughly.

Note There is another way in which like shared preferences and information (SQLite) however here I actually have shared solely those ways in which don’t involve saving of information.

How to Pass information from One Activity to a different in humanoid
Method 1: mistreatment Intent
We can send information whereas career one activity from another activity mistreatment intent. All we've to try to to is add the info to Intent object mistreatment putExtra() technique. the info is passed in key worth try. the worth is of sorts like int, float, long, string, etc.

What is Intent?

Through Intent we can move from one activity to another activity and Intent can also be used to pass the data from one activity to another activity.
In the previous article, we designed the Login Page and now we will learn how to use Intent to pass data from LoginActivity to the next activity.

Sending Data

Intent intent = new Intent(context, DestinationActivityName.class);
intent.putExtra(Key, Value);
startActivity(intent);


Here I am sending just one value, in the same way, you can attach more values by using the putExtra() method.
Retrieving Data
If the data sent is of type string then it can be fetched in the following way.

Intent intent = getIntent();
String str = intent.getStringExtra(Key);


There are several other methods like getIntExtra(), getFloatExtra(), etc to fetch other types of data.
Below is the example of passing data between activities using intents.


1.MainActivity.java:


package com.akash.intentexample;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    EditText textBox;
    Button passButton;

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

        textBox = (EditText)findViewById(R.id.textBox);
        passButton = (Button)findViewById(R.id.passButton);

        passButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = textBox.getText().toString();

                Intent intent = new Intent(getApplicationContext(), Second_activity.class);
                intent.putExtra("message", str);

                startActivity(intent);
            }
        });
    }
}



2.activity_main.xml:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp"
    android:orientation="vertical">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textBox"
        android:hint="Enter Your Message"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/passButton"
        android:text="Pass"/>
</LinearLayout>





3.Second_activity.java:


package com.akash.intentexample;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class Second_activity extends AppCompatActivity {

    TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second_activity);
        text = (TextView)findViewById(R.id.text);
        Intent intent = getIntent();
        String str = intent.getStringExtra("message");
        text.setText(str);
    }
}


4.activity_second_activity.xml:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp"
    tools:context="com.androidexample.Second">
 
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/text"/>
</LinearLayout>


5.AndroidManifest.xml:


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.akash.intentexample">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".Second_activity"></activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


6.Output:



How to Pass Data from One Activity to Another in Android



No comments:

Post a Comment