There are two common page Jump methods:
1. Intent
Intent is suitable for jump between Activity and Activity. you can press the return key to directly return to the previous page.
Disadvantage: You need to register Activity with Manifest
2. setContentView
Suitable for jump between different views in the same Activity
Advantage: pressing the return key does not return to the previous page. You need to add the button listening code to implement this function.
Only switching Layout will speed up, Because starting activity is the most time-consuming.
Data transmission is also simple, without Intent. setExtra or the like.
The disadvantage is that all controls are managed by activity in the complete set of operations such as event processing and loading, and the split is not clear enough.
Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. view. KeyEvent;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget .*;
Public class IntentTest extends Activity {
Private LinearLayout mainView = null;
Private Button button1 = null;
Private Button button2 = null;
Private LinearLayout layout = null;
Private TextView TV = null;
/*
* Because the setContentVeiw () function returns void, the system does not provide a function to obtain the current View.
* Therefore, an integer variable is added to indicate the id of the current View.
*/
Private int currentViewId;
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
// TV Initialization
TV = new TextView (this );
TV. setText ("This Is TV in layout ");
// Layout Initialization
Layout = new LinearLayout (this );
Layout. setId (100); // you must specify the id here. Otherwise, the default id will be the same as that of mainView.
Layout. setLayoutParams (new LinearLayout. LayoutParams (-1,-1 ));
Layout. addView (TV );
// Button1 initializes www.2cto.com
Button1 = new Button (this );
Button1.setLayoutParams (new LinearLayout. LayoutParams (-2,-2 ));
Button1.setText ("Intent jump ");
Button1.setOnClickListener (new OnClickListener (){
Public void onClick (View v ){
/* Intent is used to jump between the Activity and the Activity */
Intent intent = new Intent ();
Intent. setClass (IntentTest. this, MyActivity. class );
// Intent intent = new Intent (IntentTest. this, MyActivity. class );
StartActivity (intent );
}
});
// Initialize button2
Button2 = new Button (this );
Button2.setLayoutParams (new LinearLayout. LayoutParams (-2,-2 ));
Button2.setText ("setContentView jump ");
Button2.setOnClickListener (new OnClickListener (){
Public void onClick (View v ){
// SetContentView is used to redirect different views of a unified Activity.
SetCurrentView (layout );
}
});
// MianView Initialization
MainView = new LinearLayout (this );
MainView. setId (1000); // specify the id
MainView. setLayoutParams (new LinearLayout. LayoutParams (-1,-1 ));
MainView. setOrientation (LinearLayout. VERTICAL );
MainView. addView (button1 );
MainView. addView (button2 );
// Display mainView
SetCurrentView (mainView );
}
/*
* Here, the onKeyDown () function is rewritten to monitor the return key.
* If keyListener is listened through the displayed View, the Return key does not
* In this example, the onKeyDown () function is rewritten in the Activity to implement listening.
* If the listener is not intercepted, press the return key after layout is displayed to directly exit the program.
*/
Public boolean onKeyDown (int keyCode, KeyEvent event ){
If (keyCode = KeyEvent. KEYCODE_BACK ){
If (currentViewId = layout. getId ()){
SetCurrentView (mainView );
} Else {
System. exit (0 );
}
Return false;
}
Return false;
}
/* Custom function setCurrentView ()*/
Public void setCurrentView (View view ){
CurrentViewId = view. getId ();
// SetContentView is used to redirect different views of a unified Activity.
SetContentView (view );
}
}
Import android. app. Activity;
Import android. OS. Bundle;
Import android. widget. TextView;
Public class MyActivity extends Activity {
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
TextView TV = new TextView (this );
TV. setText ("My name is TV in MyActivity ");
SetContentView (TV );
}
}
From hero Xiaoqiang