Java Source Code (Cont.)

intent( Context packageContext, Class<?> cls )
It is a constructor of the class Intent, an abstract description of an operation to be performed, and creates an intent:

  • Context packageContext,” which is a Context of the application package implementing this class. Context is an interface to information about an application environment.

  • Class<?> cls,” which is the component class that is to be used for the intent

i.putExtra( String name, String[ ] value ) method
Add extended data to the intent, where
  • name, which is the name of the extra data, such as "name", and
  • value, which is the String array data value, such as name.getText( ).toString( ).

startActivity( Intent )
It is a method of Context and launches a new activity. In this case, it will launch the NextPageActivity activity.

onCreateOptionsMenu( Menu menu )
Initialize the contents of the Activity’s standard options menu. This is only called once, the first time the options menu is displayed.

getMenuInflater( ).inflate( R.menu.menu_main, menu );
Inflate a menu hierarchy from the specified XML resource.

onOptionsItemSelected( MenuItem item )
It is called whenever an item in your options menu is selected.

int id = item.getItemId( );
Return the identifier for this menu item.
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.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {
  @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 );
  }
}




      Life would be tragic if it weren’t funny.    
      ― Stephen Hawking