Tuesday, September 4, 2018

Passing Data between Activity & Passing data back to First Activity

Passing Data between Activity & Passing data back to First Activity: When we building Android application we will use multiple activities for show Data, And We need to send data from One activity to Other activity  Project code : code-from-drive Project Code zip : code-Zip-file
Create two  Activities 1. FirstActivity 2.SecondActivity update code like following Update code On FirstActivity XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="in.arula.statusdownload.FirstActivity">

    <Button
        android:id="@+id/btnFirstActivity"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_centerVertical="true"
        android:layout_margin="20dp"
        android:text="GO TO Second Activity" />

</RelativeLayout>
Update code On SecondActivity XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="in.arula.statusdownload.SecondActivity">

<Button
    android:id="@+id/btnSecondActivity"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_centerVertical="true"
    android:layout_margin="20dp"
    android:text="GO TO First Activity" />
</RelativeLayout>
Update FirstActrivity Java Code:
public class FirstActivity extends AppCompatActivity {
    Button btnFirstActivity;
    final private int REQUESTCODE = 2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        btnFirstActivity = findViewById(R.id.btnFirstActivity);
        btnFirstActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // PROCESS -1 START activity
             //   startActivity(new Intent(FirstActivity.this, SecondActivity.class));
                // PROCESS -2 START activity
                Intent i=new Intent(FirstActivity.this,SecondActivity.class);
                i.putExtra("Value","From-Activity-One");
                startActivityForResult(i,REQUESTCODE);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if ( requestCode== REQUESTCODE)
        {
            if (resultCode==RESULT_OK)
            {
                String data_val= data.getExtras().getString("Value");
                Toast.makeText(this, "FROM 2 :"+data_val, Toast.LENGTH_SHORT).show();
            }
        }

    }
}
Update SecondActrivity Java Code:
public class SecondActivity extends AppCompatActivity {

    Button btnSecondActivity;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        btnSecondActivity = findViewById(R.id.btnSecondActivity);
        if (getIntent().getExtras() != null) {
            String data = getIntent().getExtras().getString("Value");
            Toast.makeText(this, "Value:" + data, Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "No data Found", Toast.LENGTH_SHORT).show();
        }


        btnSecondActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(SecondActivity.this, "Back TO First Activity", Toast.LENGTH_SHORT).show();
                Intent i = getIntent();
                i.putExtra("Value", "DATA FROM SECOND ACTIVITY");
                setResult(RESULT_OK, i);
                finish();

            }
        });
    }


}
Share:

0 comments:

Post a Comment

BTemplates.com

Search This Blog

Powered by Blogger.

Blog Archive