Advertisement
Guest User

Untitled

a guest
Oct 21st, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.16 KB | None | 0 0
  1. package com.ambar.twoactivity;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.widget.EditText;
  9. import android.widget.TextView;
  10.  
  11. /**
  12.  * The TwoActivities app contains two activities and sends messages (intents)
  13.  * between them.
  14.  */
  15. public class MainActivity extends AppCompatActivity {
  16.     /* Class name for Log tag */
  17.     private static final String TAG_ACTIVITY = MainActivity.class.getSimpleName();
  18.     /* Unique tag required for the intent extra */
  19.     public static final String EXTRA_MESSAGE = "com.ambar.twoactivity.MESSAGE";
  20.     /* Unique tag for the intent reply */
  21.     public static final int TEXT_REQUEST = 1;
  22.  
  23.     /* EditText view for the message */
  24.     private EditText mMessage = null;
  25.     /* TextView for the reply header */
  26.     private TextView mReplyHead = null;
  27.     /* TextView for the reply body */
  28.     private TextView mReply = null;
  29.  
  30.     /**
  31.      * Initialize the activity.
  32.      * @param savedInstanceState The current state data
  33.      */
  34.     @Override
  35.     protected void onCreate(Bundle savedInstanceState) {
  36.         super.onCreate(savedInstanceState);
  37.         setContentView(R.layout.activity_main);
  38.  
  39.         /* initialize all the view variables */
  40.         mMessage = (EditText) findViewById(R.id.editText_main);
  41.         mReplyHead = (TextView) findViewById(R.id.text_header_reply);
  42.         mReply = (TextView) findViewById(R.id.text_message_reply);
  43.     }
  44.  
  45.     /**
  46.      * Handle the onClick for the "Send" button.  Gets the value
  47.      * of the main EditText, creates an intent, and launches the
  48.      * second activity with that intent.
  49.      *
  50.      * The return intent from the second activity is onActivityResult().
  51.      * @param view The view (Button) that was clicked.
  52.      */
  53.     public void launchSecondActivity(View view) {
  54.         Log.d(TAG_ACTIVITY, "Button clicked!");
  55.  
  56.         Intent intent = new Intent(this, SecondActivity.class);
  57.         String message = "";
  58.         if (mMessage != null) {
  59.             message = mMessage.getText().toString();
  60.         }
  61.  
  62.         intent.putExtra(EXTRA_MESSAGE, message);
  63.         startActivityForResult(intent, TEXT_REQUEST);
  64.     }
  65.  
  66.     /**
  67.      * Handle the data in the return intent from SecondActivity.
  68.      *
  69.      * @param requestCode Code for the SecondActivity request.
  70.      * @param resultCode Code that comes back from SecondActivity.
  71.      * @param data Intent data sent back from SecondActivity.
  72.      */
  73.     @Override
  74.     public void onActivityResult(int requestCode, int resultCode, Intent data) {
  75.         super.onActivityResult(requestCode, resultCode, data);
  76.  
  77.         if (requestCode == TEXT_REQUEST) {
  78.             if (resultCode == RESULT_OK) {
  79.                 String reply = data.getStringExtra(SecondActivity.EXTRA_REPLY);
  80.  
  81.                 if (mReplyHead != null) {
  82.                     mReplyHead.setVisibility(View.VISIBLE);
  83.                 }
  84.  
  85.                 if (mReply != null) {
  86.                     mReply.setText(reply);
  87.                     mReply.setVisibility(View.VISIBLE);
  88.                 }
  89.  
  90.             }
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement