android四大組件(一)Activity

來源:互聯網
上載者:User

標籤:

一、建立一個新的Activity

1.android的四大組件都要在資訊清單檔裡面配置

2.如果想讓你的應用有多個啟動表徵圖,你的activity需要這樣配置

    <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>

3.Activity下的lable和icon屬性可以和Application節點的屬性不一樣,預設用Application節點下的屬性

二、意圖(intent)

隱式意圖:通過指定一組動作或資料 

        Intent intent = new Intent();        //設定跳轉的動作        intent.setAction("com.cn.testActivity");        intent.addCategory("android.intent.category.DEFAULT");        //開啟activity        startActivity(intent);

顯:通過指定具體的包名和類名 。Intent intent = new Intent(this,TestActivity.class);

總結:1.開啟自己應用的介面用顯。

         2.開啟其他應用(系統應用)用隱式意圖。(電話撥號器)

         3.顯更安全一些。

三、簡訊大全案例
public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ListView lv = (ListView) findViewById(R.id.lv);        //設定資料        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.item, R.id.tv_content, objects);        //設定資料適配        lv.setAdapter(adapter);                //給ListView設定點擊事件        lv.setOnItemClickListener(new OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view,                    int position, long id) {                //把點擊條目的資料取出來                String content = objects[position];                                 Intent intent = new Intent();                //設定action                intent.setAction("android.intent.action.SEND");                intent.addCategory("android.intent.category.DEFAULT");                intent.setType("text/plain");                //傳遞資料                intent.putExtra("sms_body", content);                //跳轉到傳送簡訊的頁面                startActivity(intent);            }        });    } }
View Code 四、Activity的生命週期

1.onCreate():當activity啟動的時候調用           onDestroy():當activity銷毀的時候調用

2.onRestart()

3.onStart():當activity介面變成可見的時候調用         onStop():當activity介面變成不可見的時候調用

4.onResume()  當介面有按鈕被點擊,擷取焦點的時候調用     onPause() 當介面按鈕不可被點擊,失去焦點的時候調用

橫豎屏切換activity生命週期

 android:screenOrientation="landscape" 橫屏

 android:screenOrientation="portrait"   豎屏

四、任務棧的概念(與activity有關)

1.進棧:開啟一個activity,出棧:關閉一個activity

2.我們操作的activity永遠是棧頂的activity。

3.activity的任務棧是用來維護使用者操作體驗。

4.應用程式退出時任務棧清空了。

5.一般情況下一個應用程式對應一個任務棧。

五、Activity的四種啟動模式(理解中,後續補充)

1.standard

應用情境:瀏覽器的書籤

2.singleTop:會檢查任務棧棧頂的activity,如果存在則不會建立,直接複用。

3.singleTask:檢查當前任務棧,

4.singleInstance:自己建立一個任務棧。

應用情境:來電頁面

 

 

android四大組件(一)Activity

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.