我的應用程序應該在第一次啓動時加載一些數據,但它在恢復時不應該再次加載,當我在onCreate
方法上加載時,如果用戶更改方向,該進程再次執行,我不想要這個,我只需要運行一次加載,有什麼方法可以修復它?onCreate and Initialization in Android
2
A
回答
0
這真的很簡單!只要把:
android:configChanges="keyboardHidden|orientation"
在AndroidManifest.xml
文件中,你需要這樣的功能活動標籤內。
實施例:
<activity
android:name=".MyActivity"
android:configChanges="keyboardHidden|orientation"
android:label="Test"
android:windowSoftInputMode="stateHidden" >
</activity>
2
只需在加載數據時設置一些標誌,並且只在標誌未設置時加載數據。例如:
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
if (! preferences.getBoolean("dataLoaded")) {
loadData();
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("dataLoaded", true);
editor.commit();
}
這將確保每一次安裝的數據僅加載。如果你想每個應用程序加載數據一次instance/run,那麼解決方案就更簡單了。在這種情況下,您可以在某處使用public static
標誌,而不是使用SharedPreferences
。
0
當配置改變在運行時發生時,活動被關閉,並通過默認重新啓動,但聲明該屬性(android:configChanges
)的配置將防止重新啓動活性。在你的情況下,這個屬性的值是 android:configChanges="orientation"
相關問題
- 1. onCreate()in service never called
- 2. LINQ to SQL in and not in
- 3. SSLContext initialization
- 4. Android Pass Bitmap to Native in 2.1 and lower
- 5. Search and Search for searchbar in android and php as a backend
- 6. python/sets/dictionary/initialization
- 7. lapply and data.frame in R
- 8. Group and count in Rails
- 9. onpopstate in chrome and firefox
- 10. memcpy and free in c
- 11. if and else in Aurelia.js
- 12. list array initialization c#
- 13. C++ String \ Vector initialization
- 14. android scrolldown onCreate
- 15. ItextSharp-Exception in the reader initialization「The document has no page root」
- 16. javascript stop autolog-in in firefox and chrome
- 17. jhat OQL AND in where子句
- 18. Transform and Pivot in Access to SQL
- 19. workin with images and crud in mvc
- 20. TabBar beginCustomizingItems and endCustomizingAnimated in iphone
- 21. curl in php,iframe and forms
- 22. If-satement and loop in object arraylist
- 23. Caliburn.Micro:Views and Bootstrapper in different Projects
- 24. Escape < and > in sed/shell
- 25. Heroku,Thin and everything in between
- 26. GET in HTML and Not PHP
- 27. Json and Jquery in crm 2011
- 28. paket drop and split in udp tunnel
- 29. Dash in dictionary key and eval
- 30. ggplot in python:plot size and color
你必須重寫活動 – 2012-03-22 05:44:44
@configurationchange方法@HarshDevChandel - 你能詳細說明一下嗎?我有些困惑,爲什麼很多人似乎認爲「我只想要發生一次」的答案是「設置你的活動忽略/不重新定向改變」。是否會有其他可能導致數據第二次加載的情況與改變設備方向無關? – aroth 2012-03-22 07:02:06
當方向改變時,oncreate函數在activity中被再次調用,如果你不想發生這種情況,你可以在onconfigchange方法上覆蓋,如果你想做一些你可以在這裏檢查的東西,那麼該模式是風景還是potriat,並做任何你想要的 – 2012-03-22 09:14:31