星空网 > 软件开发 > 操作系统

初识Activity

CallbackDescription
onCreate()This is the first callback and called when the activity is first created.
onStart()This callback is called when the activity becomes visible to the user.
onResume()This is called when the user starts interacting with the application.
onPause()The paused activity does not receive user input and cannot execute any code and called when the current activity is being paused and the previous activity is being resumed.
onStop()This callback is called when the activity is no longer visible.
onDestroy()This callback is called before the activity is destroyed by the system.
onRestart()This callback is called when the activity restarts after stopping it.

关键代码如下:

public class MainActivity extends Activity {
private String msg="Android: ";
/**
* Called when the activity is first created.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(msg, "The onCreate() event");
}

/**
* Called when the activity is about to become visible.
*/
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.d(msg, "The onStart() event");
}

/**
* Called when the activity has become visible.
*/
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.d(msg, "The onResume() event");
}

/**
* Called when another activity is taking focus.
*/
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.d(msg, "The onPause() event");
}

/**
* Called when the activity is no longer visible.
*/
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.d(msg, "The onStop() event");
}

/**
* Called just before the activity is destroyed.
*/
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.d(msg, "The onDestroy() event");
}
}

 

运行截图:

当Activity第一次打开时:

初识Activityimages/loading.gif' data-original="http://images2015.cnblogs.com/blog/969341/201607/969341-20160714192311936-1407445162.png" />

当启动另一个Activity时:

初识Activity

当关闭当前的Activity,返回到先前的Activity时:

初识Activity

当销毁当前Activity时:

初识Activity

 




原标题:初识Activity

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流