标签:des android style blog http color io os ar
1. Activities
An Activity
is an application component that provides a screen with which users can interact in order to do something, such as dial the phone,
take a photo, send an email, or view a map. Each activity is given a window in which to draw its user interface. The window typically fills the
screen, but may be smaller than the screen and float on top of other windows.
2. Creating an Activity
3. Implementing a user interface
The user interface for an activity is provided by a hierarchy of views—objects derived from the View
class. Each view controls a particular
rectangular space within the activity‘s window and can respond to user interaction. For example, a view might be a button that initiates
an action when the user touches it.
You can set the layout as the UI for your activity with setContentView()
, passing the resource ID for the layout.
4. Declaring the activity in the manifest
<manifest ... > <application ... > <activity android:name=".ExampleActivity" /> ... </application ... > ... </manifest >
5. Using intents filters
An <activity>
element can also specify various intent filters—using the <intent-filter>
element—in order to declare how other application
components may activate it.
<activity android:name=".ExampleActivity" android:icon="@drawable/app_icon"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
The <action>
element specifies that this is the "main" entry point to the application.
The <category>
element specifies that this activity should be listed in the system‘s application launcher (to allow users to launch this activity).
6. Starting a Activity
Intent intent = new Intent(); intent.setClass(MainActivity.this, newClass.class); Bundle bundle = new Bundle(); bundle.putString("Name","zhangsan"); bundle.putString("Age","10"); intent.putExtra(bundle); StartActivity(intent);
in the newClass.class,
//newClass.getIntent() Bundle bundle = this.getIntent().getExtras(); String name = bundle.getString("Name"); String age = bundle.getString("Age"); ....
7. Starting an activity for result
Sometimes, you might want to receive a result from the activity that you start. In that case, start the activity by calling startActivityForResult()
(instead of startActivity()
). To then receive the result from the subsequent activity, implement the onActivityResult()
callback method.
When the subsequent activity is done, it returns a result in an Intent
to your onActivityResult()
method.
private void pickContact() { // Create an intent to "pick" a contact, as defined by the content provider URI Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI); startActivityForResult(intent, PICK_CONTACT_REQUEST); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // If the request went well (OK) and the request was PICK_CONTACT_REQUEST if (resultCode == Activity.RESULT_OK && requestCode == PICK_CONTACT_REQUEST) { // Perform a query to the contact‘s content provider for the contact‘s name Cursor cursor = getContentResolver().query(data.getData(), new String[] {Contacts.DISPLAY_NAME}, null, null, null); if (cursor.moveToFirst()) { // True if the cursor is not empty int columnIndex = cursor.getColumnIndex(Contacts.DISPLAY_NAME); String name = cursor.getString(columnIndex); // Do something with the selected contact‘s name... } } }
8. Shutting Down an Activity
You can shut down an activity by calling its finish()
method.
9. Activity LifeCycle
10. saving Activity state
The system calls onSaveInstanceState()
before making the activity vulnerable to destruction. The system passes this method a Bundle
in which
you can save state information about the activity as name-value pairs, using methods such as putString()
and putInt()
. Then, if the
system kills your application process and the user navigates back to your activity, the system recreates the activity and passes the
Bundle
to both onCreate()
and onRestoreInstanceState()
. Using either of these methods, you can extract your saved state from the
Bundle
and restore the activity state. If there is no state information to restore, then the Bundle
passed to you is null (which is the case
when the activity is created for the first time).
11. Coordinating activities
The order of lifecycle callbacks is well defined, particularly when the two activities are in the same process and one is starting the other.
Here‘s the order of operations that occur when Activity A starts Acivity B:
1. Activity A‘s onPause()
method executes.
2. Activity B‘s onCreate()
, onStart()
, and onResume()
methods execute in sequence. (Activity B now has user focus.)
3. Then, if Activity A is no longer visible on screen, its onStop()
method executes.
标签:des android style blog http color io os ar
原文地址:http://www.cnblogs.com/iMirror/p/4046571.html