大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
Android—Activity生命周期
我们提供的服务有:网站设计制作、网站建设、微信公众号开发、网站优化、网站认证、河南ssl等。为上1000+企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的河南网站制作公司
一、图解
二、概念分析
当一个活动被创建时,调用onCreate() 方法
当一个活动能被用户看到(即可见)时,调用onStart() 方法
当一个活动可获取用户焦点时,调用onResume() 方法
所以打开一个活动时,会先后执行onCreate()->onStart()->onResume三个方法
当一个活动不能获取用户焦点时(此时另一个活动进入前台),调用onPause() 方法
当一个活动不可见(即被其他活动完全覆盖)时,调用onStop() 方法
当我们准备启动另一个新的活动覆盖当前活动时时,会依次调用当前活动类的
onPause() -> onStop() 方法
当我们再次启动该活动,该活动由后台转到前台运行时,会依次调用该活动类的onRestart()->onStart()->onResume()三个方法
当我们退出程序,或调用finish() 方法时,则依次调用 onPause() -> onStop() -> onDestroy() 方法
三、例子说明
//FirstActivity.java
package mars.activity05;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class FirstActivity extends Activity {
/** Called when the activity is first created. */
private Button myButton;
@Override
public void onCreate(Bundle savedInstanceState) {
System.out.println("FirstActivity ---> onCreate ");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myButton = (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(new ButtonListener());
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
System.out.println("FirstAcvity --->onDestory");
super.onDestroy();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
System.out.println("FirstAcvity --->onPause");
super.onPause();
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
System.out.println("FirstAcvity --->onRestart");
super.onRestart();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
System.out.println("FirstAcvity --->onResume");
super.onResume();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
System.out.println("FirstAcvity --->onStart");
super.onStart();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
System.out.println("FirstAcvity --->onStop");
super.onStop();
}
class ButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(FirstActivity.this, SecondActivity.class);
intent.putExtra("extra_data", "Hello I'm FirstActivity!");
FirstActivity.this.startActivity(intent);
}
}
}
//mian.xml
android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" />
//SecondActivity.java
package mars.activity05;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class SecondActivity extends Activity{
private Button secondButton;
TextView text = null;
String str = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
System.out.println("SecondActivity--->onCreate");
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
secondButton = (Button)findViewById(R.id.secondButton);
secondButton.setOnClickListener(new ButtonListener());
text = (TextView) findViewById (R.id.text);
Intent intent = getIntent();
str = intent.getStringExtra("extra_data");
text.setText(str);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
System.out.println("SecondActivity--->onDestory");
super.onDestroy();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
System.out.println("SecondActivity--->onPause");
super.onPause();
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
System.out.println("SecondActivity--->onRestart");
super.onRestart();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
System.out.println("SecondActivity--->onResume");
super.onResume();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
System.out.println("SecondActivity--->onStart");
super.onStart();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
System.out.println("SecondActivity--->onStop");
super.onStop();
}
class ButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(SecondActivity.this, FirstActivity.class);
SecondActivity.this.startActivity(intent);
}
}
}
//second.xml
android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" > android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/second_button" >
//AndroidManifest.xml 中注册 SecondActivity
android:label="SecondActivity" android:theme="@android:style/Theme.Dialog"/> //string.xml
网站标题:AndroidActivity生命周期
标题网址:http://dzwzjz.com/article/gepcis.html