How to use putExtra || getIntentExtra in Java

                                     Send Code to Other File 

How to use putExtra  getIntentExtra in Java


First Application Code where we use putExtra:

package com.example.todayclasspratice;

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 {


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

EditText FirstName1=findViewById(R.id.FirstName);
EditText LastName2=findViewById(R.id.LastName);
EditText nickName3=findViewById(R.id.nickName);
Button btn=findViewById(R.id.btn);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String FirstName,LastName,nickName;

FirstName=FirstName1.getText().toString();
LastName=LastName2.getText().toString();
nickName=nickName3.getText().toString();

Intent a=new Intent(MainActivity.this,MainActivity2.class);

a.putExtra("FirstName",FirstName);
a.putExtra("LastName",LastName);
a.putExtra("nickName",nickName);
startActivity(a);

}
});
}
}
Second Application Code where we use getIntent || getStringExtra:
package com.example.todayclasspratice;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity2 extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
TextView txt1,txt2,txt3;

txt1=findViewById(R.id.text1);
txt2=findViewById(R.id.textView2);
txt3=findViewById(R.id.text3);

String a,b,c;

a=getIntent().getStringExtra("FirstName");
b=getIntent().getStringExtra("LastName");
c=getIntent().getStringExtra("nickName");

txt1.setText(a);
txt2.setText(b);
txt3.setText(c);

}
}

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.