Advertisement
yo2man

TreeHouse: Sending Data to New Activity

Jun 29th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.90 KB | None | 0 0
  1. // Sending Data to New Activity
  2.  
  3. // w/o the comments:
  4. public class MainActivity extends ActionBarActivity {
  5.  
  6.     private EditText mNameField;
  7.     private Button mStartButton;
  8.  
  9.     @Override
  10.     protected void onCreate(Bundle savedInstanceState) {
  11.         super.onCreate(savedInstanceState);
  12.         setContentView(R.layout.activity_main);
  13.  
  14.         mNameField = (EditText)findViewById(R.id.nameEditText);
  15.         mStartButton = (Button)findViewById(R.id.startButton);
  16.  
  17.         mStartButton.setOnClickListener(new View.OnClickListener() {
  18.             @Override
  19.             public void onClick(View v) {
  20.                 String name = mNameField.getText().toString();
  21.                 startStory(name);*
  22.         });
  23.     }
  24.         *private void startStory(String name);
  25.         Intent intent = new Intent(this, StoryActivity.class);
  26.         Intent.putExtra("name", name);        
  27.         startActivity(intent);
  28. }
  29.  
  30.  
  31. //--------------------------------------------------------------------------------------------------------------
  32. StoryActivity.java
  33.  
  34. [basic hello world template activity]
  35.  
  36. //---------------------------------------------------------------------------------------------------------------
  37. //---------------------------------------------------------------------------------------------------------------
  38. //---------------------------------------------------------------------------------------------------------------
  39.  
  40. // w/ the comments:
  41.  
  42. public class MainActivity extends ActionBarActivity {
  43.  
  44.     private EditText mNameField;
  45.     private Button mStartButton;
  46.  
  47.     @Override
  48.     protected void onCreate(Bundle savedInstanceState) {
  49.         super.onCreate(savedInstanceState);
  50.         setContentView(R.layout.activity_main);
  51.  
  52.         mNameField = (EditText)findViewById(R.id.nameEditText);
  53.         mStartButton = (Button)findViewById(R.id.startButton);
  54.  
  55.         mStartButton.setOnClickListener(new View.OnClickListener() {
  56.             @Override
  57.             public void onClick(View v) {
  58.                 String name = mNameField.getText().toString(); [2]
  59.                 startStory(name[3]);*
  60.         });
  61.     }
  62.         *private void startStory([3]String name);
  63.         Intent intent = new Intent(this, StoryActivity.class);
  64.     // We have the user's name here [2] in main activity, but, it's stuck in here. We can't access it in our new story activity.
  65.     // Solution:
  66.     // intent can hold a data and pass it to a new activity
  67.         Intent.putExtra("name", name[3]);            //intent.putExtra(Key = "String", value);  // [3]Oh noes, nameVariable is Out of Scope [in different { } { } ]  //Solution: to make our name available to this scope:
  68.                             // Variable Scope:
  69.                             /* To make our name variable available in this new method,
  70.                             we need to either pass it as a parameter to the start story method,
  71.                             or make it a class level member variable,
  72.                             that is available to all the methods in the class.
  73.                             Like, M name field and M start button.
  74.                             Either way is fine, but I generally like to pass things like this as parameters.
  75.                            
  76.                             so add this [3]
  77.                             IF You are wondering why we can use the same "name" for the variable in both scopes, it is again because of scope. These are two totally different scope thus the variables have nothing to do with each other and can be named the same. Because once we are out of this method, the variable will cease to exist.
  78.  
  79. //What is Key?
  80. // H
  81.  
  82.                             */
  83.        startActivity(intent);
  84. }
  85.  
  86.  
  87. //-------------------------------------------------------------------------------------------------------
  88. [1]
  89. StoryActivity.java
  90.  
  91. [basic hello world template activity]
  92.  
  93. //--------------------------------------------------------------------------------------------------------
  94.  
  95.  
  96.  
  97. // https://teamtreehouse.com/library/build-an-interactive-story-app/intents-and-multiple-activities/sending-data-to-a-new-activity
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement