博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 4学习(6):概述 - 深入了解Android Activity
阅读量:5367 次
发布时间:2019-06-15

本文共 4575 字,大约阅读时间需要 15 分钟。

参考:《Professional Android 4 Application Development

深入了解Android Activity

每一个Android Activity都对应于一个用户界面(UI)。每个Android Application都有一个main Activity,而这个main Activity大多由多个Fragment组成,而这些Fragment后面往往都由一个或多个secondary Activity支持。当用户在不同的界面(窗口)切换时,Android会生成对应的新Activity

创建Activity

Android提供了Activity类,在编写自己的Activity时,程序员需要继承Activity并重载里面的方法。例如:

package cn.jubincn.activities;import android.app.Activity;import android.os.Bundle;public class MyActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);    }}

 

空白的Activity对应的是一个空白屏幕,因此我们需要添加FragmentLayoutViewUI元素来定制自己的Activity。大部分Activity会占据整个屏幕,除此之外,还有一些半透明的Activity和浮动的Activity

Android应用程序中,用户交互界面和数据的显示是由View来提供的。Android中的layout类,也叫ViewGroup,可以将View打包进行管理。Fragment则用来组织界面元素,从而更方便地适配不同的界面。Activity设置界面的方法有两种,一种是创建View类,将其设置到Activity中;另一种是将layout文件设置到Activity中,然后Inflate对应的Activity

@Overridepublic void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    TextView textView = new TextView(this);    setContentView(textView);}@Overridepublic void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);}

Activity生命周期

Activity的生命周期可以决定Process的优先级,并且Activity需要对生命周期事件进行适当地响应才能提供更好的用户体验。

Activity Stacks

Activity的状态决定于它在Activity Stack中的位置。Activity Stack遵循Stack的“先进后出”的规则:当一个Activity刚被创建时,它会移到栈顶;若用户点击go back按钮,或关闭当

Activity时,栈顶的Activity会被弹出,第二个Activity成为新的栈顶Activity

Activity State

Activity的生命周期中,有这几种状态:

  • Active:在栈顶的Activity,具有最高的优先级,Android会竭力满足它对资源的需求。当其他Activity变为Active状态时,此Activity的状态会变为Paused
  • Paused:处于这个状态的Activity的部分或全部对用户可见,但无法接受用户的输入。
  • Stopped:当Activity对用户不可见时,它将转为Stopped状态。此时尽管Activity仍会保留在内存中,但当系统资源紧张时,它会被回收掉。
  • Inactive:Activity在启动之前,或被kill之后,处于Inactive状态。

监听并响应Activity State的变化

Activity需要在State发生变化时作出相应的反应,为此AndroidActivity的生命周期事件提供了很多相关的ActionHandler,下图展示了Activity的几个生命周期:

对应的代码:

package cn.jubincn.activities;import android.app.Activity;import android.os.Bundle;public class MyStateChangeActivity extends Activity {  // Called at the start of the full lifetime.  @Override  public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);  // Initialize Activity and inflate the UI.  }  // Called after onCreate has finished, use to restore UI state  @Override  public void onRestoreInstanceState(Bundle savedInstanceState) {    super.onRestoreInstanceState(savedInstanceState);    // Restore UI state from the savedInstanceState.    // This bundle has also been passed to onCreate. Will only be called if the Activity has been killed by the system since it was last visible.  }  // Called before subsequent visible lifetimes for an Activity process.  @Override  public void onRestart(){    super.onRestart();    // Load changes knowing that the Activity has already been visible within this process.  }  // Called at the start of the visible lifetime.  @Override  public void onStart(){    super.onStart();  // Apply any required UI change now that the Activity is visible.  }  // Called at the start of the active lifetime.  @Override  public void onResume(){    super.onResume();    // Resume any paused UI updates, threads, or processes required    // by the Activity but suspended when it was inactive.  }  // Called to save UI state changes at the end of the active lifecycle.  @Override  public void onSaveInstanceState(Bundle savedInstanceState) {    // Save UI state changes to the savedInstanceState.    // This bundle will be passed to onCreate and onRestoreInstanceState if the process is killed and restarted by the run time.    super.onSaveInstanceState(savedInstanceState);  }  // Called at the end of the active lifetime.  @Override  public void onPause(){    // Suspend UI updates, threads, or CPU intensive processes that don’t need to be updated when the Activity isn't the active foreground Activity.    super.onPause();  }  // Called at the end of the visible lifetime.  @Override  public void onStop(){    // Suspend remaining UI updates, threads, or processing that aren’t required when the Activity isn’t visible.    // Persist all edits or state changes as after this call the process is likely to be killed.    super.onStop();  }  // Sometimes called at the end of the full lifetime.  @Override  public void onDestroy(){    // Clean up any resources including ending threads, closing database connections etc.    super.onDestroy();  }}

 

 

 

 

 

转载于:https://www.cnblogs.com/jubincn/p/3381081.html

你可能感兴趣的文章
学好python
查看>>
css-IE中的border-radius和box-shadow
查看>>
利用bootstrap和webform的异步CRUD及分页
查看>>
HDUOJ 1879继续畅通工程(并查集)
查看>>
OC12_自动释放池
查看>>
Saiku资源帖
查看>>
解决手机页面中点击文本框,网页放大问题
查看>>
2-5
查看>>
牛客多校3 A-PACM Team(状压降维+路径背包)
查看>>
HDU - 4284 Travel(floyd+状压dp)
查看>>
1027 制作表格
查看>>
Android之Socket通信、List加载更多、Spinner下拉列表
查看>>
面向对象的介绍与特性
查看>>
typing-python用于类型注解的库
查看>>
20189215 2018-2019-2 《密码与安全新技术专题》第13周作业
查看>>
第四周作业
查看>>
一、HTML基础
查看>>
蓝牙进阶之路 (002) - HC-05与HC-06的AT指令的区别(转)
查看>>
mysql的limit经典用法及优化
查看>>
C#后台程序与HTML页面中JS方法互调
查看>>