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
01package com.example.wenchen.helloworld;
02 
03import android.app.Activity;
04import android.content.Intent;
05import android.os.Bundle;
06import android.view.View;
07import android.widget.Button;
08import android.widget.EditText;
09import android.support.v7.app.AppCompatActivity;
10import android.view.Menu;
11import android.view.MenuItem;
12 
13public class MainActivity extends AppCompatActivity {
14  @Override
15  protected void onCreate( Bundle savedInstanceState ) {
16    super.onCreate( savedInstanceState );
17    setContentView( R.layout.activity_main );
18    final EditText name = (EditText) findViewById( R.id.name );
19    final Button button = (Button)   findViewById( R.id.next );
20    button.setOnClickListener(
21      new View.OnClickListener( ) {
22        public void onClick( View v ) {
23          /** Here i calls a new screen. **/
24          Intent i = new Intent( MainActivity.this, NextActivity.class );
25          i.putExtra( "name", name.getText( ).toString( ) );
26          startActivity( i );
27        }
28      }
29    );
30  }
31 
32  @Override
33  public boolean onCreateOptionsMenu( Menu menu ) {
34    // Inflate the menu; this adds items to the action bar if it is present.
35    getMenuInflater( ).inflate( R.menu.menu_main, menu );
36    return true;
37  }
38 
39  @Override
40  public boolean onOptionsItemSelected( MenuItem item ) {
41    // Handle action bar item clicks here.  The action bar will
42    // automatically handle clicks on the Home/Up button, so long
43    // as you specify a parent activity in AndroidManifest.xml.
44    int id = item.getItemId( );
45 
46    // noinspection SimplifiableIfStatement
47    if ( id == R.id.action_settings ) {
48      return true;
49    }
50    else if ( id == R.id.next ) {
51      Intent i = new Intent( MainActivity.this, NextActivity.class );
52      startActivity( i );
53    }
54    return super.onOptionsItemSelected( item );
55  }
56}