Java Source Code (Cont.)

public void OnCreate( Bundle savedInstanceState )
It is called by the Android system when your activity starts. You will usually call the following two methods from this method:

  • setContentView(int), which is with a layout resource defining your UI, and

  • findViewById(int), which is to retrieve the widgets in that UI that you need to interact with programmatically.

public void OnCreate( Bundle savedInstanceState )
Bundle savedInstanceState is a way of passing data between activities and storing data between instantiations of the same activity. We will not need to use this for HelloWorld.

super.onCreate( savedInstanceState )
It is information you want returned to your application, via onCreate( ), if the activity is destroyed and restarted due to some implicit reason (e.g., not because the user pressed the back button).

    The reason to call super.onCreate( ) is because your code will not compile otherwise.

super.onCreate( savedInstanceState );
The super keyword refers to the instance of the parent class of the current object. It is used when you overwrite a method in a subclass but still want to call the method defined in the parent class.

setContentView( R.layout.activity_main );
It sets the activity content to an explicit view. Android uses layouts to define screen layouts on the target, and that activity_main was the name of the default layout file that the Android SDK created for us under the res/layout directory.
HelloWorld/app/src/main/java/com/example/wenchen/helloworld/MainActivity.java
package com.example.wenchen.helloworld;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {
  @Override
  protected void onCreate( Bundle savedInstanceState ) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_main );
    final EditText name = (EditText) findViewById( R.id.name );
    final Button button = (Button)   findViewById( R.id.next );
    button.setOnClickListener(
      new View.OnClickListener( ) {
        public void onClick( View v ) {
          /** Here i calls a new screen. **/
          Intent i = new Intent( MainActivity.this, NextActivity.class );
          i.putExtra( "name", name.getText( ).toString( ) );
          startActivity( i );
        }
      }
    );
  }

  @Override
  public boolean onCreateOptionsMenu( Menu menu ) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater( ).inflate( R.menu.menu_main, menu );
    return true;
  }

  @Override
  public boolean onOptionsItemSelected( MenuItem item ) {
    // Handle action bar item clicks here.  The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId( );

    // noinspection SimplifiableIfStatement
    if ( id == R.id.action_settings ) {
      return true;
    }
    else if ( id == R.id.next ) {
      Intent i = new Intent( MainActivity.this, NextActivity.class );
      startActivity( i );
    }
    return super.onOptionsItemSelected( item );
  }
}




      Patient: Oh doctor, I’m just so nervous. This is my first operation.    
      Doctor: Don’t worry. Mine too.