在Activity创建、可视、隐藏、重新可视、销毁等过程中,我们有时候需要做一些特殊处理,到底应该在Activity的哪个函数中进行处理操作呢?这就是今天我要讲的。
1、MainActivity.java
这个Activity是项目的启动Activity,也就是主Activity。通过这个Activity启动其他Activity,但是主Activity只能暂时不可见,不能销毁,否则,其他Activity就无法再返回到这个主Activity了。直接上代码:
package com.ju.testphpserver.activity;
import com.ju.testphpserver.model.UserInfoModel;
import com.ju.testphpserver.object.User;
import com.ju.testphpserver.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView userIDTV;
private TextView userNameTV;
private TextView passwordTV;
private Button getBtn;
private Button deleteBtn;
private Button updateBtn;
private EditText userIDET;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
deleteBtn = (Button) findViewById(R.id.deleteBtn);
intent = this.getIntent();
getBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
initActivity();
}
});
deleteBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, DeleteActivity.class);
startActivity(intent);
}
});
Log.i("ju", "MainActivity:onCreate()");
}
@Override
public void onStart(){
super.onStart();
Log.i("ju", "MainActivity:onStart()");
}
@Override
public void onResume(){
super.onResume();
Log.i("ju", "MainActivity:onResume()");
}
@Override
public void onPause(){
super.onPause();
Log.i("ju", "MainActivity:onPause()");
}
@Override
protected void onStop() {
super.onStop();
Log.i("ju", "MainActivity:onStop()");
}
@Override
public void onDestroy(){
super.onDestroy();
Log.i("ju", "MainActivity:onDestroy()");
}
}
2、DeleteActivity.java
package com.ju.testphpserver.activity;
import com.ju.testphpserver.model.UserInfoModel;
import com.ju.testphpserver.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class DeleteActivity extends Activity {
private Button gobackBtn;
private Button deleteUserBtn;
private EditText userIDTV;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_deletectivity);
gobackBtn = (Button) findViewById(R.id.gobackBtn);
deleteUserBtn = (Button) findViewById(R.id.deleteUserBtn);
intent = this.getIntent();
gobackBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
DeleteActivity.this.setResult(RESULT_OK, intent);
DeleteActivity.this.finish();
}
});
Log.i("ju", "DeleteActivity:onCreate()");
}
@Override
public void onStart(){
super.onStart();
Log.i("ju", "DeleteActivity:onStart()");
}
@Override
public void onResume(){
super.onResume();
Log.i("ju", "DeleteActivity:onResume()");
}
@Override
public void onPause(){
super.onPause();
Log.i("ju", "DeleteActivity:onPause()");
}
@Override
protected void onStop() {
super.onStop();
Log.i("ju", "DeleteActivity:onStop()");
}
@Override
public void onDestroy(){
super.onDestroy();
Log.i("ju", "DeleteActivity:onDestroy()");
}
}
1、当项目启动时,依次启动函数的顺序是:
MainAcitivity:onCreate()
->
MainAcitivity:onStart()
->
MainActivity:onResume()
2、当点击“删除页面”时,视图切换到了删除页面:DeleteActivity,依次启动函数的顺序是:
MainActivity:onPause()
->
DeleteActivity:onCreate()
->
DeleteActivity:onStart()
->
DeleteActivity:onResume()
->
MainActivity:onStop()
3、当点击删除页面的“返回”按钮时,视图再次切换回主Activity:MainActivity,依次启动函数顺序是:
DeleteActivity:onPause()
->
MainActivity:onStart()
->
MainActivity:onResume()
->
DeleteAcvitiy:onStop()
->
DeleteActivity:OnDestroy()
4、当项目正在运行,遇到锁屏时:
MainActivity:onPause()
->
MainActiviy:onStop()
屏幕解锁后,重新唤醒应用,执行以下顺序:
MainActivity:onStart()
->
MainActivity:onResume()
5、当直接退出application时,执行以下顺序:
MainActivity:onPause()
->
MainActiviy:onStop()
->
MainActivity:onDestroy()
如果我们在开发过程中,需要对某个activity的某个过程进行处理,就可以将代码直接写在相应的函数中。
例如:
1、初始化代码,只执行一次,一般写在onCreate()函数中。
2、内存回收类代码,一般写在onDestroy()函数中。
3、某个视图每次重新加载时,需要更新的代码,一般写在onStart()函数中。
以上只是我个人意见,希望对各位写代码有一定的帮助吧。
参考文章(activity):
http://www.runoob.com/android/android-acitivities.html