本文主要说明 Activity的四种状态及相关回调函数的使用
四种状态:运行 暂停 恢复 停止
Activity的相关方法:
OnStart
OnPause
OnResume
OnRestart
OnStop
以下代码实现由MainActivity向OtherActivity的跳转
MainActivity:
OtherActivity:
四种状态:运行 暂停 恢复 停止
Activity的相关方法:
OnStart
OnPause
OnResume
OnRestart
OnStop
以下代码实现由MainActivity向OtherActivity的跳转
MainActivity:
package com.example.lifestyle;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.v("Rich", "MainActivity onPause");
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.v("Rich", "MainActivity onRestart");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.v("Rich", "MainActivity onResume");
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.v("Rich", "MainActivity onStart");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.v("Rich", "MainActivity onStop");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,
OtherActivity.class);
startActivity(intent);
}
});
}
@Override
public 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;
}
}
OtherActivity:
package com.example.lifestyle;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class OtherActivity extends Activity {
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.v("Rich", "OtherActivity onPause");
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.v("Rich", "OtherActivity onRestart");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.v("Rich", "OtherActivity onResume");
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.v("Rich", "OtherActivity onStart");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.v("Rich", "OtherActivity onStop");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(OtherActivity.this,
MainActivity.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.other, menu);
return true;
}
}