developer.android.com学习笔记Activity篇(一)

Activity学习篇

一个application由多个松散activity组成。每个application都有其主入口,一个activity一旦停止,就进入一个堆栈里。当界面停止时,要求手动释放任何大的对象,比如网络或数据库,当界面进入resume时,你可以重新请求资源。

一个标准的activity应该要如下:

public class TabHost extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.tab_host_fourth_view);
 }

}

继承Activity,两个最重要的回调函数是onCreate()OnStop()。调用 setContentView()  定义用户界面。

任何一个界面的使用都得在AndroidManifest.xml里做出声明。在声明里用<intent-filter>来过滤,告诉其他程序可以怎么样来激活这个界面。比如

<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).

If you intend for your application to be self-contained and not allow other applications to activate its activities, then you don't need any other intent filters. 

启动一个界面

Intent intent= new Intent(Intent.ACTION_SEND);

intent.putExtra(Intent.EXTRA_EMAIL, recipientArray);

startActivity(intent);

上面代码是用来告诉启动系统启动发邮件的命令。在putExtra里放入邮件的地址。

启动一个有结果返回的界面

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...
        }
    }
}

一个activity的生命周期

 



public class ExampleActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // The activity is being created.
    }
    @Override
    protected void onStart() {
        super.onStart();
        // The activity is about to become visible.
    }
    @Override
    protected void onResume() {
        super.onResume();
        // The activity has become visible (it is now "resumed").
    }
    @Override
    protected void onPause() {
        super.onPause();
        // Another activity is taking focus (this activity is about to be "paused").
    }
    @Override
    protected void onStop() {
        super.onStop();
        // The activity is no longer visible (it is now "stopped")
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // The activity is about to be destroyed.
    }
}


第一次启动一个界面会经历OnCreate,OnStart,OnResume,按顺序执行。OnCreate是第一次创建界面时会调用。OnStart是指界面在被用户看见前被调用。OnResume代表界面获得用户焦点,此时用户可以看见界面并且焦点在界面上。在我认为中,onPasueOnStop的区别就在于OnPause单纯指用户焦点不在此界面上,OnStop是指用户不仅焦点不在此界面上,连这个界面都看不到了。所以当弹出对话框,popWindowactivityonPause.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值