StartActivityForResult in Android.

 

Useful Video : https://youtu.be/AD5qt7xoUU8


---------------------------------------------------------------------------------------------








<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">

<EditText
android:id="@+id/number1_editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="90dp"
android:layout_marginTop="213dp"
android:ems="10"
android:hint="Number 1"
android:inputType="textPersonName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/number2_editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="51dp"
android:ems="10"
android:hint="Number 2"
android:inputType="textPersonName"
app:layout_constraintStart_toStartOf="@+id/number1_editText"
app:layout_constraintTop_toBottomOf="@+id/number1_editText" />

<TextView
android:id="@+id/Result_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="54dp"
android:layout_marginBottom="69dp"
android:text="Result"
android:textSize="30dp"
app:layout_constraintBottom_toTopOf="@+id/number1_editText"
app:layout_constraintStart_toStartOf="@+id/number1_editText" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="72dp"
android:layout_marginEnd="14dp"
android:onClick="OpenActivityTwo"
android:text="Open Activity 2"
app:layout_constraintEnd_toEndOf="@+id/number2_editText"
app:layout_constraintTop_toBottomOf="@+id/number2_editText" />
</androidx.constraintlayout.widget.ConstraintLayout>



package com.deepesh.startactivityforresultapp;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

public static final int REQUEST_CODE=1;
TextView result_textview;
EditText number1_ed , number2_ed;

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

result_textview = findViewById(R.id.Result_textview);
number1_ed = findViewById(R.id.number1_editText);
number2_ed = findViewById(R.id.number2_editText);

}

// user clicks button
public void OpenActivityTwo(View view) {

Intent intent = new Intent(this,Activity2.class);
intent.putExtra("number1",Integer.valueOf(number1_ed.getText().toString()));
intent.putExtra("number2",Integer.valueOf(number2_ed.getText().toString()));

//REQUEST_CODE is needed for Confirmation.
startActivityForResult(intent,REQUEST_CODE);

}

// CALLED WHEN THE ACTIVITY2 COMES BACK
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode==REQUEST_CODE && resultCode==RESULT_OK){
int result = data.getIntExtra("result",0);
result_textview.setText(String.valueOf(result));
}else{
Log.d("TAG", "onActivityResult: USER CANCELLED OR CLICKED THE BACK BUTTON");
}
}
}







<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".Activity2">

<TextView
android:id="@+id/number_displayer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="136dp"
android:text="TextView"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/add_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="3dp"
android:layout_marginBottom="51dp"
android:onClick="AddNumbers"
android:text="Add"
app:layout_constraintBottom_toTopOf="@+id/sub_button"
app:layout_constraintEnd_toEndOf="@+id/sub_button" />

<Button
android:id="@+id/sub_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="142dp"
android:onClick="SubtractNumbers"
android:text="Subtract"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>


package com.deepesh.startactivityforresultapp;

import androidx.appcompat.app.AppCompatActivity;

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

public class Activity2 extends AppCompatActivity {

TextView number_displayer;
int number1;
int number2;

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

number_displayer = findViewById(R.id.number_displayer);

Intent intent = getIntent();
number1 = intent.getIntExtra("number1", 0);
number2 = intent.getIntExtra("number2", 0);

number_displayer.setText("NUMBERS : " + number1 + "," + number2);

}

// user clicks add button
public void AddNumbers(View view) {

Intent intent = new Intent();
intent.putExtra("result", number1 + number2);

// If the user cancels or Clicks the BACK button , Request code send from the system is RESULT_CANCEL
setResult(RESULT_OK, intent);
finish();
}

// user clicks sub button
public void SubtractNumbers(View view) {

Intent intent = new Intent();
intent.putExtra("result", number1 - number2);

setResult(RESULT_OK, intent);
finish();
}


}




-------------------------------------------------------------------------------------------------------------








































Comments

Popular posts from this blog

React Js + React-Redux (part-2)

React Js + CSS Styling + React Router (part-1)

ViteJS (Module Bundlers, Build Tools)