yo2man

TreeHouse: new activity and intent

Jun 29th, 2015
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. //New Activity and Intent
  2.  
  3. // w/o the comments:
  4.  
  5. public class MainActivity extends ActionBarActivity {
  6.  
  7.     private EditText mNameField;
  8.     private Button mStartButton;
  9.  
  10.     @Override
  11.     protected void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.activity_main);
  14.  
  15.         mNameField = (EditText)findViewById(R.id.nameEditText);
  16.         mStartButton = (Button)findViewById(R.id.startButton);
  17.  
  18.         mStartButton.setOnClickListener(new View.OnClickListener() {
  19.             @Override
  20.             public void onClick(View v) {
  21.                 String name = mNameField.getText().toString();
  22.         startStory();*
  23.         });
  24.     }
  25.     *private void startStory();
  26.     Intent intent = new Intent(this, StoryActivity.class);
  27.     startActivity(intent);
  28.    
  29. }
  30.  
  31.  
  32. //-----------------------------------------------------------------------------------------------------------------------------------------
  33. [1]
  34. StoryActivity.java
  35.  
  36. [basic hello world template activity]
  37.  
  38. //-----------------------------------------------------------------------------------------------------------------------------------------
  39.  
  40.  
  41. // W/ the comments
  42.  
  43. public class MainActivity extends ActionBarActivity {
  44.  
  45.     private EditText mNameField;
  46.     private Button mStartButton;
  47.  
  48.     @Override
  49.     protected void onCreate(Bundle savedInstanceState) {
  50.         super.onCreate(savedInstanceState);
  51.         setContentView(R.layout.activity_main);
  52.  
  53.         mNameField = (EditText)findViewById(R.id.nameEditText);
  54.         mStartButton = (Button)findViewById(R.id.startButton);
  55.  
  56.         mStartButton.setOnClickListener(new View.OnClickListener() {
  57.             @Override
  58.             public void onClick(View v) {
  59.                 String name = mNameField.getText().toString();
  60.         startStory();* // So when we tap on the button, were going to start our story,
  61.         });
  62.     }
  63.     *private void startStory(); //listed inside OnCreate = to create something when "startStory();" is activated via "OnClickListener"
  64.     // Create Intent:
  65.     Intent intent = new Intent(this, StoryActivity.class);  //Intent (ContextpackageContext, class)
  66.     startActivity(intent);  //we intend to start a new activity [1]
  67.     //An intent is what we use when we intend to do something in an Android app. We usually use them to start a new task, and that task can be starting a new activity, or maybe, handing something off to another app, like a video player or a messaging app.
  68.  
  69.    
  70. }
  71.  
  72.  
  73. //-----------------------------------------------------------------------------------------------------------------------------------------
  74. [1]
  75. StoryActivity.java
  76.  
  77. [basic hello world template activity]
Advertisement
Add Comment
Please, Sign In to add comment