LoginPage

Login page

Simple Login Page

Username Password

Senin, 26 Februari 2018

How to pass data from 2nd activity to 1st activity when pressed back? - android

I've 2 activities, Activity1 and Activity2.
In Activity1 I've a Button and TextView. When the button is clicked Activity2 is started.
In Activity2 I've an EditText.
I want to display the data retrieved from EditText in Activity2 in the TextView in Activity1 when back is pressed from Activity2.
can someone help me with the code to make this work?
Answer :
Start Activity2 with startActivityForResult and use setResult method for sending data back from Activity2 to Activity1. In Activity1 you will need to override onActivityResult for updating TextView with EditText data from Activity2.
For example:
In Activity1, start Activity2 as:
Intent i = new Intent(this, Activity2.class);
startActivityForResult(i, 1);
In Activity2, use setResult for sending data back:
Intent intent = new Intent();
intent.putExtra("editTextValue", "value_here")
setResult(RESULT_OK, intent);        
finish();
And in Activity1, receive data with onActivityResult:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1) {
         if(resultCode == RESULT_OK) {
             String strEditText = data.getStringExtra("editTextValue");
         }     
    }
} 
If you can, also use SharedPreferences for sharing data between Activities.

Source : https://stackoverflow.com/questions/14292398/how-to-pass-data-from-2nd-activity-to-1st-activity-when-pressed-back-android

Next, add the following code to the OnCreate method of the FirstActivity to wire up the resultButton click to launching the second Activity with StartActivityForResult:
var resultButton = FindViewById<Button> (Resource.Id.resultButton);

resultButton.Click += delegate {
  var myIntent = new Intent (this, typeof(SecondActivity));
  StartActivityForResult (myIntent, 0);
};
In SecondActivity, wire up the helloButton in the OnCreate button:
var helloButton = FindViewById<Button> (Resource.Id.helloButton);

helloButton.Click += delegate {
  Intent myIntent = new Intent (this, typeof(FirstActivity));
  myIntent.PutExtra ("greeting", "Hello from the Second Activity!");
  SetResult (Result.Ok, myIntent);
  Finish();
};
Set the data to be sent back to the first Activity with SetResult, passing in the result status and the data (in this case, our Intent). When you're through with passing the data, let Android know you're done by calling Finish.
To handle the result in the first Activity, we will override a method called OnActivityResult. This will listen for our result code and the Intent that we passed into SetResult in the second Activity:
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
  base.OnActivityResult(requestCode, resultCode, data);
  if (resultCode == Result.Ok) {
     var helloLabel = FindViewById<TextView> (Resource.Id.helloLabel);
     helloLabel.Text = data.GetStringExtra("greeting");
  }
}
Finally, we will display the string that we passed to the first Activity as a greeting in the first Activity.

Tidak ada komentar: