最近研究android内核,学习到android activity的启动模式,掌握好启动模式,对activity的处理例如关闭和开启,以及数据间的传输有很大的帮助。
看此博文前,请花点时间先看一下一个博主写的:http://www.cnblogs.com/lwbqqyumidi/p/3771542.html,有助于下面理解。。
我们同4个activity的启动来看其中的原理。。。。。。
下面是xml文件(很简单):
first.xml
其他的类似。。。。
4个activity的定义:
public class Activity1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v("Standard", "activity1 oncreate");
setContentView(R.layout.first);
TextView textView=(TextView) findViewById(R.id.textView1);
TextView textIdView=(TextView) findViewById(R.id.textIdView1);
textView.setText(this.toString());
textIdView.setText("current task id:"+this.getTaskId());
Button bt=(Button) findViewById(R.id.button1);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(Activity1.this,Activity2.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.v("Standard", "activity1 onStart");
}
@Override
protected void onPause() {
super.onPause();
Log.v("Standard", "activity1 onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.v("Standard", "activity1 onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.v("Standard", "activity1 onDestroy");
}
}
public class Activity2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Log.v("SingleInstance", "activity2 oncreate");
TextView textView=(TextView) findViewById(R.id.textView2);
TextView textIdView=(TextView) findViewById(R.id.textIdView2);
textView.setText(this.toString());
textIdView.setText("current task id:"+this.getTaskId());
Button bt = (Button) findViewById(R.id.button2);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Activity2.this, Activity3.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.v("SingleInstance", "activity2 onStart");
}
@Override
protected void onPause() {
super.onPause();
Log.v("SingleInstance", "activity2 onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.v("SingleInstance", "activity2 onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.v("SingleInstance", "activity2 onDestroy");
}
}
public class Activity3 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third);
Log.v("SingleInstance", "activity3 oncreate");
TextView textView=(TextView) findViewById(R.id.textView3);
TextView textIdView=(TextView) findViewById(R.id.textIdView3);
textView.setText(this.toString());
textIdView.setText("current task id:"+this.getTaskId());
Button bt = (Button) findViewById(R.id.button3);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Activity3.this, Activity4.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.v("SingleInstance", "activity3 onStart");
}
@Override
protected void onPause() {
super.onPause();
Log.v("SingleInstance", "activity3 onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.v("SingleInstance", "activity3 onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.v("SingleInstance", "activity3 onDestroy");
}
}
public class Activity4 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.forth);
Log.v("SingleInstance", "activity4 oncreate");
TextView textView = (TextView) findViewById(R.id.textView4);
TextView textIdView = (TextView) findViewById(R.id.textIdView4);
textView.setText(this.toString());
textIdView.setText("current task id:"+this.getTaskId());
Button bt = (Button) findViewById(R.id.button4);
//bt.setText("forth go to forth");
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Intent intent = new Intent(Activity4.this, Activity4.class);
Intent intent = new Intent(Activity4.this, Activity1.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.v("SingleInstance", "activity4 onStart");
}
@Override
protected void onPause() {
super.onPause();
Log.v("SingleInstance", "activity4 onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.v("SingleInstance", "activity4 onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.v("SingleInstance", "activity4 onDestroy");
}
}
AndroidManifest.xml的配置:
启动顺序:
activity1->activity2->activity3->activity4->activity1
返回退出:
总结:activity的启动模式如果使用standard,那么每启动一个activity就会生成一个activity对象,将每个activity入栈,后退关闭时,需要一个个返回关闭,最先启动的最后关闭。
4个activity的定义:
public class Activity1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v("SingleTop", "activity1 oncreate");
setContentView(R.layout.first);
TextView textView=(TextView) findViewById(R.id.textView1);
TextView textIdView=(TextView) findViewById(R.id.textIdView1);
textView.setText(this.toString());
textIdView.setText("current task id:"+this.getTaskId());
Button bt=(Button) findViewById(R.id.button1);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(Activity1.this,Activity2.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.v("SingleTop", "activity1 onStart");
}
@Override
protected void onPause() {
super.onPause();
Log.v("SingleTop", "activity1 onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.v("SingleTop", "activity1 onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.v("SingleTop", "activity1 onDestroy");
}
}
public class Activity2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Log.v("SingleTop", "activity2 oncreate");
TextView textView=(TextView) findViewById(R.id.textView2);
TextView textIdView=(TextView) findViewById(R.id.textIdView2);
textView.setText(this.toString());
textIdView.setText("current task id:"+this.getTaskId());
Button bt = (Button) findViewById(R.id.button2);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Activity2.this, Activity3.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.v("SingleTop", "activity2 onStart");
}
@Override
protected void onPause() {
super.onPause();
Log.v("SingleTop", "activity2 onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.v("SingleTop", "activity2 onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.v("SingleTop", "activity2 onDestroy");
}
}
public class Activity3 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third);
Log.v("SingleTop", "activity3 oncreate");
TextView textView=(TextView) findViewById(R.id.textView3);
TextView textIdView=(TextView) findViewById(R.id.textIdView3);
textView.setText(this.toString());
textIdView.setText("current task id:"+this.getTaskId());
Button bt = (Button) findViewById(R.id.button3);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Activity3.this, Activity4.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.v("SingleTop", "activity3 onStart");
}
@Override
protected void onPause() {
super.onPause();
Log.v("SingleTop", "activity3 onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.v("SingleTop", "activity3 onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.v("SingleTop", "activity3 onDestroy");
}
}
public class Activity4 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.forth);
Log.v("SingleTop", "activity4 oncreate");
TextView textView = (TextView) findViewById(R.id.textView4);
TextView textIdView = (TextView) findViewById(R.id.textIdView4);
textView.setText(this.toString());
textIdView.setText("current task id:"+this.getTaskId());
Button bt = (Button) findViewById(R.id.button4);
bt.setText("forth go to forth");
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Activity4.this, Activity4.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.v("SingleTop", "activity4 onStart");
}
@Override
protected void onPause() {
super.onPause();
Log.v("SingleTop", "activity4 onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.v("SingleTop", "activity4 onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.v("SingleTop", "activity4 onDestroy");
}
}
AndroidManifest.xml的配置:
activity1->activity2->activity3->activity4->activity4
返回退出:
总结:activity的启动模式如果使用singleTop时,如果一个activity生成对象放于栈顶,这时如果要启动一个相同的activity对象,不会重新生成该activity对象,而是直接从栈顶去该activity对象出来使用,同样返回关闭时,需要将每个activity依次后退关闭,最终才能将程序关闭。
public class Activity1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v("SingleTask", "activity1 oncreate");
setContentView(R.layout.first);
TextView textView=(TextView) findViewById(R.id.textView1);
TextView textIdView=(TextView) findViewById(R.id.textIdView1);
textView.setText(this.toString());
textIdView.setText("current task id:"+this.getTaskId());
Button bt=(Button) findViewById(R.id.button1);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(Activity1.this,Activity2.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.v("SingleTask", "activity1 onStart");
}
@Override
protected void onPause() {
super.onPause();
Log.v("SingleTask", "activity1 onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.v("SingleTask", "activity1 onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.v("SingleTask", "activity1 onDestroy");
}
}
public class Activity2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Log.v("SingleTask", "activity2 oncreate");
TextView textView=(TextView) findViewById(R.id.textView2);
TextView textIdView=(TextView) findViewById(R.id.textIdView2);
textView.setText(this.toString());
textIdView.setText("current task id:"+this.getTaskId());
Button bt = (Button) findViewById(R.id.button2);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Activity2.this, Activity3.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.v("SingleTask", "activity2 onStart");
}
@Override
protected void onPause() {
super.onPause();
Log.v("SingleTask", "activity2 onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.v("SingleTask", "activity2 onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.v("SingleTask", "activity2 onDestroy");
}
}
public class Activity3 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third);
Log.v("SingleTask", "activity3 oncreate");
TextView textView=(TextView) findViewById(R.id.textView3);
TextView textIdView=(TextView) findViewById(R.id.textIdView3);
textView.setText(this.toString());
textIdView.setText("current task id:"+this.getTaskId());
Button bt = (Button) findViewById(R.id.button3);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Activity3.this, Activity4.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.v("SingleTask", "activity3 onStart");
}
@Override
protected void onPause() {
super.onPause();
Log.v("SingleTask", "activity3 onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.v("SingleTask", "activity3 onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.v("SingleTask", "activity3 onDestroy");
}
}
public class Activity4 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.forth);
Log.v("SingleTask", "activity4 oncreate");
TextView textView = (TextView) findViewById(R.id.textView4);
TextView textIdView = (TextView) findViewById(R.id.textIdView4);
textView.setText(this.toString());
textIdView.setText("current task id:"+this.getTaskId());
Button bt = (Button) findViewById(R.id.button4);
//bt.setText("forth go to forth");
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Intent intent = new Intent(Activity4.this, Activity4.class);
Intent intent = new Intent(Activity4.this, Activity2.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.v("SingleTask", "activity4 onStart");
}
@Override
protected void onPause() {
super.onPause();
Log.v("SingleTask", "activity4 onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.v("SingleTask", "activity4 onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.v("SingleTask", "activity4 onDestroy");
}
}
AndroidManifest.xml的配置:
activity1->activity2->activity3->activity4->activity1
返回退出:
启动顺序:
activity1->activity2->activity3->activity4->activity4->activity4->activity4
返回退出:
启动顺序:
activity1->activity2->activity3->activity4->activity2
总结:activity的启动模式如果使用singleTask时,启动activity时,生成activity对象,并依次入栈,当一个activity想跳转到另外一个activity时,不会马上生成对象,而是从栈里寻找是否已经存在该activity对象,如果存在,将栈顶activity对象依次出栈,并且销毁,直到得到该activity对象。返回退出时,依次将栈顶activity对象出栈销毁,最后退出程序。
public class Activity1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v("SingleInstance", "activity1 oncreate");
setContentView(R.layout.first);
TextView textView=(TextView) findViewById(R.id.textView1);
TextView textIdView=(TextView) findViewById(R.id.textIdView1);
textView.setText(this.toString());
textIdView.setText("current task id:"+this.getTaskId());
Button bt=(Button) findViewById(R.id.button1);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(Activity1.this,Activity2.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.v("SingleInstance", "activity1 onStart");
}
@Override
protected void onPause() {
super.onPause();
Log.v("SingleInstance", "activity1 onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.v("SingleInstance", "activity1 onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.v("SingleInstance", "activity1 onDestroy");
}
}
public class Activity2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Log.v("SingleInstance", "activity2 oncreate");
TextView textView=(TextView) findViewById(R.id.textView2);
TextView textIdView=(TextView) findViewById(R.id.textIdView2);
textView.setText(this.toString());
textIdView.setText("current task id:"+this.getTaskId());
Button bt = (Button) findViewById(R.id.button2);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Activity2.this, Activity3.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.v("SingleInstance", "activity2 onStart");
}
@Override
protected void onPause() {
super.onPause();
Log.v("SingleInstance", "activity2 onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.v("SingleInstance", "activity2 onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.v("SingleInstance", "activity2 onDestroy");
}
}
public class Activity3 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third);
Log.v("SingleInstance", "activity3 oncreate");
TextView textView=(TextView) findViewById(R.id.textView3);
TextView textIdView=(TextView) findViewById(R.id.textIdView3);
textView.setText(this.toString());
textIdView.setText("current task id:"+this.getTaskId());
Button bt = (Button) findViewById(R.id.button3);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Activity3.this, Activity4.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.v("SingleInstance", "activity3 onStart");
}
@Override
protected void onPause() {
super.onPause();
Log.v("SingleInstance", "activity3 onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.v("SingleInstance", "activity3 onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.v("SingleInstance", "activity3 onDestroy");
}
}
public class Activity4 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.forth);
Log.v("SingleInstance", "activity4 oncreate");
TextView textView = (TextView) findViewById(R.id.textView4);
TextView textIdView = (TextView) findViewById(R.id.textIdView4);
textView.setText(this.toString());
textIdView.setText("current task id:"+this.getTaskId());
Button bt = (Button) findViewById(R.id.button4);
//bt.setText("forth go to forth");
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Intent intent = new Intent(Activity4.this, Activity4.class);
Intent intent = new Intent(Activity4.this, Activity1.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.v("SingleInstance", "activity4 onStart");
}
@Override
protected void onPause() {
super.onPause();
Log.v("SingleInstance", "activity4 onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.v("SingleInstance", "activity4 onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.v("SingleInstance", "activity4 onDestroy");
}
}
activity1->activity2->activity3->activity4->activity1
返回退出:
总结:activity的启动模式如果使用singleInstance时,没启动一个activity,android系统会单独给每个activity对象一个栈结构,用于存放该activity对象。如果启动的activity已经有存在栈中,还是会生成activity对象,存入该栈。退出返回时,最先将当前栈里的activity对象出栈销毁,再依次后退销毁其他的栈里的activity对象。
至此全部总结完毕,如果我理解有错误,欢迎指正,在此先谢过,如果能帮到你,请给我点一下赞,谢谢。