編輯:關於Android編程
先占個位置,過會兒來翻譯,:p
DEMO下載地址:http://download.csdn.net/detail/sweetvvck/7728735
Unlike other programming paradigms in which apps are launched with a main()
method, the Android system initiates code in an Activity
instance
by invoking specific callback methods that correspond to specific stages of its lifecycle. There is a sequence of callback methods that start up an activity and a sequence of callback methods that tear down an activity.
This lesson provides an overview of the most important lifecycle methods and shows you how to handle the first lifecycle callback that creates a new instance of your activity.
During the life of an activity, the system calls a core set of lifecycle methods in a sequence similar to a step pyramid. That is, each stage of the activity lifecycle is a separate step on the pyramid. As the system creates a new activity instance, each callback method moves the activity state one step toward the top. The top of the pyramid is the point at which the activity is running in the foreground and the user can interact with it.
As the user begins to leave the activity, the system calls other methods that move the activity state back down the pyramid in order to dismantle the activity. In some cases, the activity will move only part way down the pyramid and wait (such as when the user switches to another app), from which point the activity can move back to the top (if the user returns to the activity) and resume where the user left off.
Depending on the complexity of your activity, you probably don't need to implement all the lifecycle methods. However, it's important that you understand each one and implement those that ensure your app behaves the way users expect. Implementing your activity lifecycle methods properly ensures your app behaves well in several ways, including that it:
As you'll learn in the following lessons, there are several situations in which an activity transitions between different states that are illustrated in figure 1. However, only three of these states can be static. That is, the activity can exist in one of only three states for an extended period of time:
The other states (Created and Started) are transient and the system quickly moves from them to the next state by calling the next lifecycle callback method. That is, after the system calls onCreate()
,
it quickly callsonStart()
,
which is quickly followed by onResume()
.
That's it for the basic activity lifecycle. Now you'll start learning about some of the specific lifecycle behaviors.
When the user selects your app icon from the Home screen, the system calls the onCreate()
method
for theActivity
in
your app that you've declared to be the "launcher" (or "main") activity. This is the activity that serves as the main entry point to your app's user interface.
You can define which activity to use as the main activity in the Android manifest file, AndroidManifest.xml
,
which is at the root of your project directory.
The main activity for your app must be declared in the manifest with an
that
includes the MAIN
action
and LAUNCHER
category.
For example:
Note: When you create a new Android project with the Android SDK tools, the default project files include anActivity
class
that's declared in the manifest with this filter.
If either the MAIN
action
or LAUNCHER
category
are not declared for one of your activities, then your app icon will not appear in the Home screen's list of apps.
Most apps include several different activities that allow the user to perform different actions. Whether an activity is the main activity that's created when the user clicks your app icon or a different activity that your app starts in response to a user action,
the system creates every new instance of Activity
by
calling its onCreate()
method.
You must implement the onCreate()
method
to perform basic application startup logic that should happen only once for the entire life of the activity. For example, your implementation of onCreate()
should
define the user interface and possibly instantiate some class-scope variables.
For example, the following example of the onCreate()
method
shows some code that performs some fundamental setup for the activity, such as declaring the user interface (defined in an XML layout file), defining member variables, and configuring some of the UI.
TextView mTextView; // Member variable for text view in the layout @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Set the user interface layout for this Activity // The layout file is defined in the project res/layout/main_activity.xml file setContentView(R.layout.main_activity); // Initialize member TextView so we can manipulate it later mTextView = (TextView) findViewById(R.id.text_message); // Make sure we're running on Honeycomb or higher to use ActionBar APIs if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { // For the main activity, make sure the app icon in the action bar // does not behave as a button ActionBar actionBar = getActionBar(); actionBar.setHomeButtonEnabled(false); } }
Caution: Using the SDK_INT
to
prevent older systems from executing new APIs works in this way on Android 2.0 (API level 5) and higher only. Older versions will encounter a runtime exception.
Once the onCreate()
finishes
execution, the system calls the onStart()
and onResume()
methods
in quick succession. Your activity never resides in the Created or Started states. Technically, the activity becomes visible to the user when onStart()
is
called, but onResume()
quickly
follows and the activity remains in the Resumed state until something occurs to change that, such as when a phone call is received, the user navigates to another activity, or the device screen turns off.
In the other lessons that follow, you'll see how the other start up methods, onStart()
and onResume()
,
are useful during your activity's lifecycle when used to resume the activity from the Paused or Stopped states.
Note: The onCreate()
method
includes a parameter called savedInstanceState
that's discussed in the latter lesson about Recreating
an Activity.
While the activity's first lifecycle callback is onCreate()
,
its very last callback is onDestroy()
.
The system calls this method on your activity as the final signal that your activity instance is being completely removed from the system memory.
Most apps don't need to implement this method because local class references are destroyed with the activity and your activity should perform most cleanup during onPause()
and onStop()
.
However, if your activity includes background threads that you created during onCreate()
or
other long-running resources that could potentially leak memory if not properly closed, you should kill them during onDestroy()
.
@Override public void onDestroy() { super.onDestroy(); // Always call the superclass // Stop method tracing that the activity started during onCreate() android.os.Debug.stopMethodTracing(); }
Note: The system calls onDestroy()
after
it has already called onPause()
and onStop()
in
all situations except one: when you call finish()
from
within the onCreate()
method.
In some cases, such as when your activity operates as a temporary decision maker to launch another activity, you might call finish()
from
withinonCreate()
to
destroy the activity. In this case, the system immediately calls onDestroy()
without
calling any of the other lifecycle methods.
1. 訪問私有數據庫創建一個項目,在項目中利用SQLiteOpenHelper創建一個名稱為account的數據庫,並在數據庫中創建一張名為info的表。public c
Android 7.1.1 鎖屏界面啟動流程。前幾天遇到一個低概率復現鎖屏界面不顯示,只顯示狀態欄的問題,跟了下鎖屏界面啟動顯示的流程,在這分享下,也方便以後自己查看。前
網絡收集的原因如下,以及解決辦法:我補充總結一下:解決辦法一:在操作網絡類(socket連接)的activity的protected void onCreate(Bund
先上效果圖: Title的Layout為: 彈出的dialog的Layout為