Basic activity jump, activity jump
The problem solved today is activity redirection.
Currently, I have learned two ways to jump.
1. Use setContentView () method
2. Use Intent object
Method 1: setContentView ()
This method does not require the target activity to configure the activity node in the AndroidManifest. xml file, or requires a. java file.
If you do not respond after the jump, press the return key.
The Code is as follows to implement the round-trip switching between activity_main and activity_2.
Public class MainActivity extends Activity {// StringBuffer mes = new StringBuffer (); // DisplayMetrics dm = null; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); Button btn2 = (Button) findViewById (R. id. main_btn_2); btn2.setOnClickListener (new Button. onClickListener () {@ Overridepublic void onClick (View arg0) {toActivity_2 () ;}) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the Menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true;} public void toActivity_2 () {setContentView (R. layout. activity_2); // jump to the target activity_2Button btn = (Button) findViewById (R. id. act2_btn_1); // obtain the button act2_btn_1btn.setOnClickListener (new OnClickListener () {// button event listener @ Overridepublic void onClick (View arg0) on activity_2) {// return activity_maintoActivity_main () ;}});} public void toActivity_main () {setContentView (R. layout. activity_main); // jump to the target activity_mainButton btn = (Button) findViewById (R. id. main_btn_2); // obtain the button main_btn_2btn.setOnClickListener (new OnClickListener () {// button event listener @ Overridepublic void onClick (View arg0) on activity_main) {// jump to activity_2toActivity_2 ();}});}}
Method 2: Intent
In layout, the activity node needs to be configured in the AndroidManifest. xml file, and the corresponding. java class is required.
Each jump is recorded as the return key, so the return key identifies the jump of this method.
Code on the activity_main page
public class MainActivity extends Activity {//StringBuffer mes=new StringBuffer();//DisplayMetrics dm=null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button btn2=(Button)findViewById(R.id.main_btn_2);btn2.setOnClickListener(new Button.OnClickListener() {@Overridepublic void onClick(View arg0) {intentToActivity_2();}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}public void intentToActivity_2(){Intent intent=new Intent();intent.setClass(MainActivity.this, activity_2.class);startActivity(intent);}}
Code on the activity_2 page
public class activity_2 extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_2);Button btn=(Button)findViewById(R.id.act2_btn_1);btn.setOnClickListener(new Button.OnClickListener() {@Overridepublic void onClick(View arg0) {Intent t=new Intent();t.setClass(activity_2.this,MainActivity.class);startActivity(t);}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// TODO Auto-generated method stubreturn super.onCreateOptionsMenu(menu);}}
The above two jump methods are introduced, which are good or bad and cannot be mentioned for the time being. They mainly refer to application scenarios. I am not good at writing, but I hope it will be helpful to me like I just learned android!