We can use Intents which are basically used to go from one activity to another but also by intent we can send data from one activity to another in many data types using key-value pair.
For understand this >You need to understand the cycle(Logic which contain 90% of weightage of any programming) of intent that how it works and once you understand that then left is just a code(only 10% of programming). So let's get start and follow the steps below and if you got any question then feel free to comment or contact us in any social media sites.
Step 1: Create 2 Activities
here i created two activities by named Main Activity and page2.
xml files |
Note: Main activity is default activity in android and you can change its name by just right click on file and then select rename.
2 java files by same name as activities will also created automatically.
Step2: Design your both activities xml files according to yourself and put button on main activity.
Step3: In page2 make controller i.e. Textview or Edittext where you want to show the text after intent and here i take Textview as you can see below activity_page2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:text="This is Page no.2 "
android:textAlignment="center"/>
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Step4: In MainActivity.java file write below code with full explanation
public void clickOnMe(View view){
Intent intent=new Intent(this,page2.class);
intent.putExtra("name","amit");
startActivity(intent);
Intent is a class so we need to make it object
and in argument of intent we provide from which activity to where our intent go i.e
for present class(here its main) we used this keyword and in second parameter we set the page2.class because we want to go from main activity to page2 activity.
Intent intent=new Intent(this,page2.class);
For add/put any string in intent we need to used key-value pair and now question arises that why we need to put string or any other data in the form of key-value so when here amit is the value and name is key. name work as address of amit.
intent.putExtra("name","amit");
now we create intent and set the string/data but still it will not work because we only declare them but not start the intent So here we use predefined method named startActivity() and in parameter we have to put the same name of intent as declare above.
startActivity(intent);
Our work in first or home activity is done now
Now Let's go to another activity to show this intent data
In page2.java file and inside onCreate method :
String intent1=getIntent().getStringExtra("name");
TextView editText=findViewById(R.id.textView3);
editText.setText(intent1);
in First line we create a new variable(intent1) to save the data which is passed through intent and also declare its data type as String.
getIntent() is used to call the intent method of previous activity
getStringExtra("name") is used to tell the intent that what we are trying to retrieve/get from the intent and here we put key("name") and if the value in int datatypes then we need to use getIntExtra in place of getStringExtra.
String intent1=getIntent().getStringExtra("name");
Now think a little bit that where you want to show that data which comes through intent and all of you will answer its page2 But the question is at what position or where that data will show to user.
So in XML file of page2 we need to create a textView or editText and here we used textView.
and find that textView by some id which we already declare and save that in variable name editText
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:text="This is Page no.2 "
android:textAlignment="center"/>
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
TextView editText=findViewById(R.id.textView3);
Now we need to add that that data which comes through intent to our textView
and our data in saved in intent1
editText.setText(intent1);