android_learners_hub
i.putExtra("Yourname",name);
startActivity(i);// to start the intent activity
In the second class Example.java (Fetch data)
Before
covering Relative layout lets discuss what is intent.
Intent
in Android
It
is interesting to know that how an activity shifts to another
activity on just clicking a button, the same happens when you get a
notification (when you get message on your social media apps) this
all is just because of Intent.
Hence, Intent
is a messaging object which is used to request an action whenever you
want to communicate with other activities.
For
example,
suppose you are watching a movie on U-Tube and you get a notification
on your Email when you click on that notification you will shift to
that activity means that, this (E mail) activity get started and your
previous activity (U-Tube) will shift onpause method.
If
you want to use Intent then you have to import following you can also
do this task on pressing alt
+ enter.
import
android.content.Intent;
If
you want to categories the uses of Intent then you can categories it
into the followings:-
- It is used when you Start an Activity. (*Noteactivity represent a single screen )
- It is used when you Start a Service.( *Noteservice is invisible because it run on the background of an app)
- It is used when you want to deliver a broadcast. ( *Note broadcast gives us notification)
Types of Intent:-
To
start another activity we have two methods or types:
1.Explicit
Intent
2.Implicit Intent
1.Explicit
Intent :-Explicit
Intent is used where we know the destination path means if we want
to open home page after registration in the app then in that case we
know where the request will go after registration so, we will use
Explicit Intent.
In
the following program we are migrating from one activity to another
here we will also learn how to pass data between these two
activities.
//
How we can pass data between two Activities.
In
MainActivity.java
(send data)
name=text.getText().toString();
//
get the text enter in the EditText
Intent
i=new
Intent
(getApplicationContext() ,Example.class);
//
declare intent object and pass the current and destination class
name.
i.putExtra("Yourname",name);
//putExtra
method is used to send the data to another activity it has to
parameters that are “Key” and “value” the key is then used in
the destination class to fetch the value
startActivity(i);// to start the intent activity
In the second class Example.java (Fetch data)
Intent
i2 = getIntent();
// to get intent make an object i2 of intent
String
ur_name = i2.getStringExtra("Yourname");
//
make a variable of String and pass the key within the parameters of
getStringExtra
For
Int or float value use
getIntExtra(),getFloatExtra().
Code for Layout :-
activity_main.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" tools:context=".MainActivity" android:orientation="vertical"> <EditText android:id="@+id/text" android:hint="Enter Your Name" android:layout_marginTop="3dp" android:layout_width="match_parent" android:layout_height="50dp" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:layout_gravity="center" android:text="Click me" android:textSize="20sp" /> </LinearLayout>
intent.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/name2"
android:layout_width="match_parent"
android:layout_height="118dp"
android:layout_gravity="center"
android:gravity="center"
android:text="TextView"
android:textSize="24sp" />
</LinearLayout>
Code for Java :-
MainActivity.java
package com.example.myfirstapp; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;public class MainActivity extends AppCompatActivity { Button click; EditText text; String name; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text=findViewById(R.id.text); click=findViewById(R.id.button); click.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { name=text.getText().toString(); Intent i = new Intent(getApplicationContext(),Example.class); i.putExtra("Yourname",name); startActivity(i); } }); } }
Example.java
package com.example.myfirstapp; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class Example extends AppCompatActivity { TextView text2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.intent); text2=findViewById(R.id.name2); Intent i2 = getIntent(); String ur_name = i2.getStringExtra("Yourname"); text2.setText("Welcome " + ur_name); } }
Output :-
See the output clearly through video :-
We
will discuss Implicit Intent in the next post.
For any query feel free to comment.
Great Work 👍
ReplyDelete