Advertisement
dzar

SecondActivity.java

Oct 18th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. package com.dzar.activitydua;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.EditText;
  8. import android.widget.TextView;
  9.  
  10. /**
  11. * SecondActivity defines the second activity in the app. It is launched from an intent
  12. * with a message, and sends an intent back with a second message.
  13. */
  14. public class SecondActivity extends AppCompatActivity {
  15. /* Unique tag for the intent reply. */
  16. public static final String EXTRA_REPLY =
  17. "com.example.android.twoactivities.REPLY";
  18.  
  19. /* EditText for the reply */
  20. private EditText mReply = null;
  21.  
  22. /**
  23. * Initialize the activity.
  24. * @param savedInstanceState The current state data
  25. */
  26. @Override
  27. protected void onCreate(Bundle savedInstanceState) {
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.activity_second);
  30.  
  31. /* initialize view variables */
  32. mReply = (EditText) findViewById(R.id.editText_second);
  33.  
  34. /* Get the intent that launched this activity, and the message in
  35. the intent extra. */
  36. Intent intent = getIntent();
  37. String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
  38.  
  39. /* Put that message into the text_message TextView */
  40. TextView textView = (TextView) findViewById(R.id.text_message);
  41. if (textView != null) {
  42. textView.setText(message);
  43. }
  44. }
  45.  
  46. /**
  47. * Handle the onClick for the "Reply" button. Gets the message
  48. * from the second EditText, creates an intent, and returns
  49. * that message back to the main activity.
  50. *
  51. * @param view The view (Button) that was clicked.
  52. */
  53. public void returnReply(View view) {
  54. /* Get the reply message from the edit text */
  55. String reply = "";
  56. if (mReply != null) {
  57. reply = mReply.getText().toString();
  58. }
  59.  
  60. /* Create a new intent for the reply, add the reply message to it as an extra,
  61. set the intent result, and close the activity. */
  62. Intent replyIntent = new Intent();
  63. replyIntent.putExtra(EXTRA_REPLY, reply);
  64. setResult(RESULT_OK, replyIntent);
  65. finish();
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement