Activity的啟動模式

來源:互聯網
上載者:User

轉載自:http://blog.163.com/stay_rui/blog/static/1954750352012528101429907/

Activity的幾種模式啟動模式:FLAG_ACTIVITY_CLEAR_TOP、FLAG_ACTIVITY_REORDER_TO_FRONT、FLAG_ACTIVITY_SINGLE_TOP

1. 如果已經啟動了四個Activity:A,B,C和D。在D Activity裡,我們要跳到B Activity,同時希望C finish掉,可以在startActivity(intent)裡的intent裡添加flags標記,如下所示:

Intent intent = new Intent(this, B.class);    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);   startActivity(intent);  

這樣啟動B Activity,就會把D,C都finished掉,如果你的B Activity的啟動模式是預設的(multiple) ,則B Activity會finished掉,再啟動一個新的Activity B。  
如果不想重新再建立一個新的B Activity,則在上面的代碼裡再加上:

intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);   

這樣B Activity就不會再建立一個新的了,而是會重用之前的B Activity,同時調用B Activity的onNewIntent()方法。

 

2. 如果已經啟動了四個Activity:A,B,C和D,在D Activity裡,想再啟動一個Actvity B,但不變成A,B,C,D,B,而是希望是A,C,D,B,

就是用來切換Activity在堆棧中的位置,則可以像下面寫代碼:

Intent intent = new Intent(this, MainActivity.class);   intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);    startActivity(intent);   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.