Java Source Code (Cont.)


This slide shows the other activity NextActivity.java, which is similar to the home page MainActivity.java. It prints the user input from the home page and starts the other activity, home page, by clicking the button.

HelloWorld/app/src/main/java/com/example/wenchen/helloworld/NextActivity.java
01package com.example.wenchen.helloworld;
02 
03import android.app.Activity;
04import android.content.Intent;
05import android.os.Bundle;
06import android.view.Menu;
07import android.view.MenuItem;
08import android.view.View;
09import android.widget.Button;
10import android.widget.TextView;
11 
12public class NextActivity extends Activity {
13  /** Called when the activity is first created. */
14  @Override
15  public void onCreate( Bundle savedInstanceState ) {
16    super.onCreate( savedInstanceState );
17    setContentView( R.layout.activity_next );
18 
19    final TextView tvView = (TextView) findViewById( R.id.tvView );
20    Intent intent = getIntent( );
21    String name = intent.getStringExtra( "name" );
22    tvView.setText( "Welcome, " + name );
23    final Button button = (Button) findViewById( R.id.home );
24    button.setOnClickListener(
25      new View.OnClickListener( ) {
26        public void onClick( View v ) {
27          Intent i = new Intent( NextActivity.this, MainActivity.class );
28          startActivity( i );
29        }
30      }
31    );
32  }
33 
34  @Override
35  public boolean onCreateOptionsMenu( Menu menu ) {
36    // Inflate the menu; this adds items to the action bar if it is present.
37    getMenuInflater( ).inflate( R.menu.menu_next, menu );
38    return true;
39  }
40 
41  @Override
42  public boolean onOptionsItemSelected( MenuItem item ) {
43    // Handle action bar item clicks here.  The action bar will
44    // automatically handle clicks on the Home/Up button, so long
45    // as you specify a parent activity in AndroidManifest.xml.
46    int id = item.getItemId( );
47 
48    // noinspection SimplifiableIfStatement
49    if ( id == R.id.action_settings ) {
50      return true;
51    }
52    else if ( id == R.id.home ) {
53      Intent i = new Intent( NextActivity.this, MainActivity.class );
54      startActivity( i );
55    }
56    return super.onOptionsItemSelected( item );
57  }
58}




      I just got my doctor’s test results and I’m really upset.    
      Turns out, I’m not gonna be a doctor.