Get result from another activity using registerForActivityResult | Android Studio | Java
How To Get Result From Another Activity Using the registerForActivityResult?
In this tutorial we will learn how to get result from another activity using registerForActivityResult. You might aware to use the method startActivityForResult which is now deprecated. So we will use the newer way that is registerForActivityResult.
We will create 2 Activities the MainActivity and the FormActivity. From the MainActivity we will launch the FormActivity to input some data and get that data as result back to the MainActivity.
Code:
activity_main.xml
<?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:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/addUserBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add User" /> <TextView android:id="@+id/nameTv" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" /> <TextView android:id="@+id/emailTv" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" /> </LinearLayout>
MainActivity.java
package com.technifysoft.myapplication; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import androidx.activity.result.ActivityResult; import androidx.activity.result.ActivityResultCallback; import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContracts; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { private Button addUserBtn; private TextView nameTv, emailTv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //init UI Views addUserBtn = findViewById(R.id.addUserBtn); nameTv = findViewById(R.id.nameTv); emailTv = findViewById(R.id.emailTv); //handle addUserBtn click, start Next Activity to get data from it addUserBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // The launcher with the Intent you want to start Intent intent = new Intent(MainActivity.this, FormActivity.class); launchSomeActivity.launch(intent); } }); } private ActivityResultLauncher<Intent> launchSomeActivity = registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() { @Override public void onActivityResult(ActivityResult result) { if (result.getResultCode() == Activity.RESULT_OK) { Intent data = result.getData(); String name = data.getStringExtra("name"); String email = data.getStringExtra("email"); nameTv.setText(name); emailTv.setText(email); Toast.makeText(MainActivity.this, name + "\n" + email, Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "Cancelled...", Toast.LENGTH_SHORT).show(); } } }); }
activity_form.xml
<?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:orientation="vertical" android:padding="10dp" tools:context=".FormActivity"> <EditText android:id="@+id/nameEt" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Name" /> <EditText android:id="@+id/emailEt" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Email" /> <Button android:id="@+id/submitBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Submit" /> </LinearLayout>
FormActivity.java
package com.technifysoft.myapplication; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import androidx.appcompat.app.AppCompatActivity; public class FormActivity extends AppCompatActivity { private EditText nameEt, emailEt; private Button submitBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_form); setTitle("Add User"); //init UI Views nameEt = findViewById(R.id.nameEt); emailEt = findViewById(R.id.emailEt); submitBtn = findViewById(R.id.submitBtn); //handle submitBtn click, input data and pass to previous activity submitBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //get data String name = nameEt.getText().toString().trim(); String email = emailEt.getText().toString().trim(); //put data to intent to get in previous activity Intent intent = new Intent(); intent.putExtra("name", name); intent.putExtra("email", email); setResult(RESULT_OK, intent); //finishing activity finish(); } }); } }
Comments
Post a Comment