Android - Communication between Activities -startActivityForResult Example

package myapplication.example1.com.hackonlycom;

/** * Created by AASHISH on 02-04-2017.   Total 4 files, names are
    1.Send.java
    2.Get.java
    3.send.xml
    4.get.xml 
 */
 
1.Send.java
package com.example.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;

public class Send extends Activity implements View.OnClickListener {

    Button start, startFor;
    EditText sendET;
    TextView gotAnswer;
    String setData;
    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout. send);
        initialize();
    }


    private void initialize() {
        // TODO Auto-generated method stub        start= (Button) findViewById(R.id. bSA);
        startFor= (Button) findViewById(R.id. bSAFR);
        sendET= (EditText) findViewById(R.id. etSEND);
        gotAnswer=(TextView) findViewById(R.id.tvGOT);

        start.setOnClickListener( this);
        startFor.setOnClickListener( this);
    }




    @Override    public void onClick(View v) {
        Bundle basket = new Bundle();

        basket.putString("key", "aashish");
        switch (v.getId()) {
            case R.id.bSA:
                //Intent iSA=new Intent("com.example.myapplication.GET");                Intent iSA = new Intent(Send.this, Get.class);
                iSA.putExtras(basket);
                startActivity(iSA);


                break;

            case R.id.bSAFR:
                int resultCode = 0;
                Intent iSFR = new Intent(Send.this, Get.class);
               // Intent iSFR=new Intent("com.example.myapplication.GET");                iSFR.putExtras(basket);
                startActivityForResult(iSFR,resultCode) ;



        }
    }
        @Override        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // TODO Auto-generated method stub            super.onActivityResult(requestCode, resultCode, data);
            if(resultCode==RESULT_OK) {
                Bundle basket = data.getExtras();
                String s = basket.getString("answer");
                gotAnswer.setText(s);
            }
    }
}


2.Get.java
 
 
 
package com.example.myapplication;

/** * Created by AASHISH on 08-04-2017. */import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;

public class Get extends Activity implements OnClickListener,RadioGroup.OnCheckedChangeListener {

    TextView question, test;
    Button returnData;
    RadioGroup selectionList;
    String setData;


    private void initialize() {
        // TODO Auto-generated method stub        question=(TextView) findViewById(R.id. tvQuestion);
        test=(TextView) findViewById(R.id. tvText);
        returnData=(Button) findViewById(R.id.breturn);
        returnData.setOnClickListener(this);

        selectionList=(RadioGroup)findViewById(R.id.rgAnswers);
        selectionList.setOnCheckedChangeListener(this);
    }
    @Override        protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);
        setContentView(R.layout. get);
        initialize();

        Bundle gotBasket=getIntent().getExtras();
        setData=gotBasket.getString("key" );
        question.setText(setData );
    }

    @Override    public void onClick (View arg0) {
        // TODO Auto-generated method stub        Intent person=new Intent();
        Bundle backpack=new Bundle();
        backpack.putString("answer",setData);
        person.putExtras(backpack);
        setResult(RESULT_OK,person);
        finish();
    }

    @Override    public void onCheckedChanged(RadioGroup arg0, int arg1) {
        // TODO Auto-generated method stub        switch(arg1){

        case R.id.rgood :
        setData= "Carry On!!";//String setData        break;
        case R.id.rbad :

        setData= " Leave it!!";
        break;
        case R.id.rboth :
        setData= "I think you are confused!!";
        break;


    }
        test.setText(setData);
}




}
 
 
 
 
//3.send.xml
 
xml version= "1.0" encoding ="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width= "match_parent"    android:layout_height= "match_parent" >

    <EditText        android:id="@+id/etSEND"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentRight="true"        android:layout_alignParentTop="true"        android:layout_marginTop="21dp"        android:ems="10" >

        <requestFocus />
    </EditText >

    <Button        android:id="@+id/bSA"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="StartActivity"        android:layout_below="@+id/etSEND"        android:layout_centerHorizontal="true" />

    <Button        android:id="@+id/bSAFR"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="StartActivityForResult"        android:layout_below="@+id/bSA"        android:layout_centerHorizontal="true" />

    <TextView        android:id="@+id/tvGOT"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="TextView"        android:layout_below="@+id/bSAFR"        android:layout_alignParentLeft="true"        android:layout_alignParentStart="true"        android:layout_alignParentRight="true"        android:layout_alignParentEnd="true" />

</RelativeLayout> 
 


//4.get.xml

xml version= "1.0" encoding ="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width= "match_parent"    android:layout_height= "match_parent"    android:orientation= "vertical" >

    <TextView        android:id="@+id/tvQuestion"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Android is ...." />

    <RadioGroup        android:id="@+id/rgAnswers"        android:layout_width="wrap_content"        android:layout_height="wrap_content" >

        <RadioButton            android:id="@+id/rgood"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:checked="true"            android:text="Good" />

        <RadioButton            android:id="@+id/rbad"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Bad" />

        <RadioButton            android:id="@+id/rboth"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Both" />
    </RadioGroup >

    <Button        android:id="@+id/breturn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Return" />

    <TextView        android:id="@+id/tvText"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="TextView" />

</LinearLayout>




Reactions:

0 comments:

Post a Comment