[Android Samples視頻系列之ApiDemos] App-Activity-Redirection

來源:互聯網
上載者:User

1.Demo說明與示範

Redirection這個Demo會涉及到三個Acitivity: RedirectEnter, RedirectMain,RedirectGetter。其Main Activity是RedirectEnter,它啟動RedirectMain,然後在RedirectMain中根據條件來決定是否再啟動RedirectGetter。主要知識點是SharedPreference的使用.

Demo如下:


2.視頻講解

http://www.eyeandroid.com/thread-10683-1-1.html

 


3.Demo分析

 

 

Redirection樣本涉及到三個Acitivity: RedirectEnter, RedirectMain,RedirectGetter。樣本的主Activity為 RedirectEnter ,RedirectEnter 啟動 RedirectMain, 而Activity 會根據某個條件來決定是否將應用的控制權傳給RedirectGetter 或是保持在RedirectMain。

 

應用代碼中使用到了Shared Preferences (在之前的樣本中介紹過)。 RedirectMain 將檢查某個shared preferences 值是否存在:

 
  1. // Retrieve the current text preference.  If there is no text  
  2. // preference set, we need to get it from the user by invoking the  
  3. // activity that retrieves it.  To do this cleanly, we will  
  4. // temporarily hide our own activity so it is not displayed until the  
  5. // result is returned.  
  6. if (!loadPrefs()) {  
  7.  Intent intent = new Intent(this, RedirectGetter.class);  
  8.  startActivityForResult(intent, INIT_TEXT_REQUEST);  
  9. }  

 

其它用到的還有startActivityForResult 。這個例子沒有什麼新的知識,只是涉及到了三個Activity。示範了如何根據條件觸發不同的Activity,將應用控制權Redirection到不同的Activity。

 

這個例子使用者點擊“Go”按鈕,RedirectEnter 啟動RedirectMain ,RedirectMain 會根據shared preferences是否有值決定是否redirect 到RedirectGetter, 第一次或是點擊”Clear and Exit”後,shared preferences 中不含有text值,應用會顯示RedirectGetter 來取的使用者輸入,此時如果使用者輸入並“Apply”後,RedirectGetter將在Shared Preference儲存textView 的值。此後,按“Back” 退回Activity List再啟動RedirectEnter,按“GO”,由於Shared Preferences中有值,RediectMain不會把應用的控制權Redirect到RedirectGetter.

相關文章

聯繫我們

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