How To Pass Data From One Fragment To Another In Android
In this tutorial you volition acquire to pass data from one activity to another in android with and without using intent.
Basically we can ship data betwixt activities in 2 means.
- Using Intent
- Using Global Variables
Below I have discussed both the methods in particular.
Annotation: In that location are some other means like shared preferences and database (sqlite) but here I have shared but those means that don't involve saving of data.
How to Pass Data from One Activity to Another in Android
Method 1: Using Intent
We can send information while calling one activity from another activity using intent. All we have to do is add the data to Intent object using putExtra() method. The information is passed in key value pair. The value tin be of types like int, float, long, string, etc.
Sending Data
Intent intent = new Intent ( context , DestinationActivityName . class ) ; intent . putExtra ( Key , Value ) ; startActivity ( intent ) ; |
Here I am sending merely one value, in the same style y'all can adhere more values past using putExtra() method.
Retrieving Data
If the data sent is of type string then it tin be fetched in following way.
Intent intent = getIntent ( ) ; Cord str = intent . getStringExtra ( Key ) ; |
There are several other methods like getIntExtra(), getFloatExtra(), etc to fetch other types of data.
Below is the case of passing data betwixt activities using intents.
Example
In this example a message is passed from offset activity to second when a button is pressed.
activity_first.xml
1 2 3 four 5 6 7 eight 9 ten 11 12 13 14 fifteen sixteen 17 xviii xix 20 21 | <? 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.Commencement" android : orientation = "vertical" > < EditText android : layout_width = "match_parent" android : layout_height = "wrap_content" android : id = "@+id/textBox" android : hint = "Enter Your Bulletin" / > < Button android : layout_width = "match_parent" android : layout_height = "wrap_content" android : id = "@+id/passButton" android : text = "Laissez passer" / > < / LinearLayout > |
activity_second.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 > |
First.coffee
1 2 3 4 5 half dozen vii 8 nine ten eleven 12 13 14 15 16 17 eighteen xix 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | bundle com . androidexample ; import android . content . Intent ; import android . support . v7 . app . AppCompatActivity ; import android . bone . Bundle ; import android . view . View ; import android . widget . Button ; import android . widget . EditText ; public class Commencement extends AppCompatActivity { EditText textBox ; Button passButton ; @Override protected void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ) ; setContentView ( R . layout . activity_first ) ; textBox = ( EditText ) findViewById ( R . id . textBox ) ; passButton = ( Push ) 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 . class ) ; intent . putExtra ( "message" , str ) ; startActivity ( intent ) ; } } ) ; } } |
Second.java
1 2 iii 4 v 6 vii 8 9 10 xi 12 xiii 14 15 sixteen 17 18 19 xx 21 22 | packet com . androidexample ; import android . content . Intent ; import android . support . v7 . app . AppCompatActivity ; import android . os . Bundle ; import android . widget . TextView ; public class 2d extends AppCompatActivity { TextView text ; @Override protected void onCreate ( Packet savedInstanceState ) { super . onCreate ( savedInstanceState ) ; setContentView ( R . layout . activity_second ) ; text = ( TextView ) findViewById ( R . id . text ) ; Intent intent = getIntent ( ) ; String str = intent . getStringExtra ( "message" ) ; text . setText ( str ) ; } } |
Screenshots
Method two: Using Global Variables
Nosotros tin declare a global variable in a divide form. To make it global simply declare it as public static.This will let us to straight access the variable anywhere in our application using its class name.
Only assign the value that you want to pass in global variable within source activity and fetch it in destination action by accessing the global variable.
Below example shows how to practise this.
Example
The code for activity_first.xml and activity_second.xml volition exist same every bit above example.
DemoClass.java
parcel com . androidexample ; public class DemoClass { public static String message ; //global variable } |
Commencement.coffee
one 2 3 iv 5 half-dozen 7 eight 9 10 xi 12 13 14 xv xvi 17 18 19 twenty 21 22 23 24 25 26 27 28 29 30 31 32 | packet com . androidexample ; 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 form First extends AppCompatActivity { EditText textBox ; Button passButton ; @Override protected void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ) ; setContentView ( R . layout . activity_first ) ; textBox = ( EditText ) findViewById ( R . id . textBox ) ; passButton = ( Button ) findViewById ( R . id . passButton ) ; passButton . setOnClickListener ( new View . OnClickListener ( ) { @Override public void onClick ( View v ) { DemoClass . bulletin = textBox . getText ( ) . toString ( ) ; Intent intent = new Intent ( getApplicationContext ( ) , 2d . class ) ; startActivity ( intent ) ; } } ) ; } } |
Second.java
ane 2 three four 5 6 vii 8 ix 10 11 12 13 xiv xv 16 17 18 xix | package com . androidexample ; import android . back up . v7 . app . AppCompatActivity ; import android . bone . Bundle ; import android . widget . TextView ; public grade Second extends AppCompatActivity { TextView text ; @Override protected void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ) ; setContentView ( R . layout . activity_second ) ; text = ( TextView ) findViewById ( R . id . text ) ; text . setText ( DemoClass . message ) ; } } |
Comment below if you take doubts or know whatever other way to pass data from ane activeness to another in android.
How To Pass Data From One Fragment To Another In Android,
Source: https://www.thecrazyprogrammer.com/2016/12/pass-data-one-activity-another-in-android.html
Posted by: brownthabould.blogspot.com
0 Response to "How To Pass Data From One Fragment To Another In Android"
Post a Comment