Advertisement
Guest User

SecondActivity.java

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