4. Fetching Data from One Activity to Second Activity

 

1. Manifest

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

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.ActivityHandle"
tools:targetApi="31">
<activity
android:name=".SecondActivity"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

</manifest>

2. Xml

i. First Activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom|end"
tools:context=".SecondActivity">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:layout_marginRight="21dp"
android:layout_marginBottom="11dp"
android:id="@+id/btnNext"
/>

</LinearLayout>

ii. Second Activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#03A9F4"
tools:context=".SecondActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:id="@+id/txtStudenInfo"
android:textSize="32sp"
android:textStyle="bold"
android:textColor="#fff"/>
</LinearLayout>

3. Java

i. First Activity:

package com.example.activityhandle;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

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

btnNext = findViewById(R.id.btnNext);
Intent iNext;
iNext = new Intent(MainActivity.this, SecondActivity.class);
//for fetching data from one to second activity
iNext.putExtra("title", "Home");
iNext.putExtra("StudentName", "Raman");
iNext.putExtra("Roll No", 10);

btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(iNext);
}
});
}
});
}
}

ii. Second Activity:

package com.example.activityhandle;

import androidx.appcompat.app.AppCompatActivity;

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

import java.lang.String;

public class SecondActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent fromAct = getIntent();
String title = fromAct.getStringExtra("title");
String StudentName = fromAct.getStringExtra("StudentName");
int RollNo = fromAct.getIntExtra("Roll No", 0);

TextView txtStudentInfo;

txtStudentInfo = findViewById(R.id.txtStudenInfo);
txtStudentInfo.setText("Roll No: "+RollNo+", Name: "+StudentName);

getSupportActionBar().setTitle(title);
}
}
Post a Comment (0)
Previous Post Next Post