Activity啟動模式
standard
(標準模式)
?每次啟動該 Activity(例如,通過?startActivity()
),系統總會創建一個新的實例,并將其放入調用者(啟動它的那個 Activity)所在的任務棧中。
singleTop
(棧頂復用模式)
?如果要啟動的 Activity?已經位于調用者任務棧的棧頂,系統不會創建新實例,而是通過調用該已有實例的?onNewIntent()
?方法將新的 Intent 傳遞給它。如果要啟動的 Activity?不在棧頂(即使它存在于棧中但不是棧頂),系統會創建一個新的實例。
singleTask
(棧內復用模式 / 單任務模式)
?如果在棧中已經存在該 Activity 的實例(無論是否在棧頂),系統會銷毀該實例之上的所有其他 Activity,將這個實例帶到棧頂,并通過?onNewIntent()
?傳遞新的 Intent。
singleInstance
(單實例模式)
系統會為這個 Activity?創建一個新的任務棧。這個新棧中只能有這個 Activity 的實例。?任何其他 Activity(包括它自己再次被啟動)都不能放入這個棧中。
?Intent
它主要用于在 Android 應用組件之間或不同應用之間執行啟動組件、傳遞數據、廣播事件等操作。
顯式 Intent
明確指定了目標組件的完整類名,系統直接將 Intent 遞送給指定的組件,不進行任何查找或過濾。
隱式 Intent
請求系統或其他應用執行一個通用操作,或者啟動一個能處理這種操作描述的組件。當發出隱式 Intent 時,Android 系統會將其與設備上所有已安裝應用的組件在?AndroidManifest.xml
?中聲明的?<intent-filter>
?進行比較。
Android 四大組件
Activity(活動):?一個?Activity?代表應用中的一個屏幕界面。它是用戶與應用交互的主要入口點
Service(服務):?Service?是一種在后臺執行長時間運行操作的組件,沒有用戶界面。
BroadcastReceiver(廣播接收器):BroadcastReceiver?是一個負責響應系統或應用內部廣播消息的組件。
ContentProvider(內容提供器):?應用間的數據共享。
Activity生命周期
onCreate(Bundle savedInstanceState):
?Activity?首次創建時調用。
onStart():
Activity 即將變得可見(進入前臺)時調用。
onResume():
可以開始與用戶交互時調用。在?onStart()
?之后調用,或者在另一個 Activity 離開前臺(如對話框關閉、電話掛斷)后調用。
onPause():
?當系統即將啟動另一個 Activity?或當前 Activity 正在失去焦點但仍部分可見(如啟動一個半透明或非全屏的 Activity,如對話框)時調用。
onStop():
Activity?對用戶完全不可見時調用。
onRestart():
當 Activity 從?Stopped?狀態重新回到前臺時調用。
onDestroy():
?Activity?被銷毀之前調用。
Fragment
Fragment 的出現是為了解決 Activity 在構建復雜 UI 和適應多屏幕設備時的局限性。
屏幕旋轉后Activity生命周期變化
用戶首次進入頁面后執行的生命周期回調方法onCreate(),onStart(),onResume()
屏幕旋轉后活動被銷毀并新建,onPause(),onStop(),onDestroy(),onCreate(),onStart(),onResume()。
Android 布局
LinearLayout(線性布局)
子視圖按單一方向(水平/垂直)順序排列。
<LinearLayoutandroid:orientation="vertical" <!-- vertical/horizontal -->>...
</LinearLayout>
?RelativeLayout(相對布局)
通過相對定位確定視圖位置.
FrameLayout(幀布局)
所有子視圖疊加顯示
RecyclerView
RecyclerView 是 Android 中用于高效展示大量數據集的高級視圖容器,通過?ViewHolder
?模式減少頻繁的?findViewById()
?開銷。
RecyclerView | 展示數據的容器視圖 |
Adapter | 將數據綁定到視圖上(核心邏輯) |
ViewHolder | 緩存視圖組件,避免重復查找 |
LayoutManager | 控制布局排列方式(線性/網格/瀑布流) |
ItemDecoration | 繪制分割線/間隔樣式 |
LiveData
LiveData
?是一種可觀察的數據存儲器類。LiveData
?只會將更新通知給活躍的觀察者。
優點
數據實時更新:LiveData
?遵循觀察者模式。當底層數據發生變化時,LiveData
?會通知 Observer
對象。
不會發生內存泄漏:觀察者會綁定到Lifecycle
對象,并在其關聯的生命周期遭到銷毀后進行自我清理。
不再需要手動處理生命周期:LiveData
?將自動管理所有這些操作。
MVVM架構
- View層:Activity/Fragment
- ViewModel層:Jetpack ViewModel + Jetpack LivaData
- Model層:Repository倉庫,包含 本地持久性數據 和 服務端數據
Android 中如何優化性能
布局優化:避免嵌套過深的布局,使用 ConstraintLayout 替代嵌套布局。
避免主線程阻塞:將耗時操作放到子線程中。
自定義view
當系統提供的標準控件 無法滿足設計或功能要求時,就需要自定義 View。
生命周期
[改變可見性] --> 構造View() --> onFinishInflate() --> onAttachedToWindow() --> onMeasure() --> onSizeChanged() --> onLayout() --> onDraw() --> onDetackedFromWindow()
onFinishInflate(): view從xml加載完成調用。?
onAttachedToWindow():View 被添加到窗口(Window)時調用。
onMeasure():確定 View 自身及其子 View(如果是 ViewGroup)的大小。
onSizeChanged():View 尺寸首次確定或發生變化時。
onLayout():?確定 View 自身的位置。
onDraw():將 View 內容繪制到屏幕上。
SharedPreferences
是Android平臺上一個輕量級的存儲輔助類,在應用中通常做一些簡單數據的持久化緩存。
//獲取對象,user為文件名,第二個參數為文件存儲方式(是否允許其他應用讀寫)
SharedPreferences sharedPreferences = getSharedPreferences("user",MODE_PRIVATE);
獲取Editor對象的引用
SharedPreferences.Editor editor = sharedPreferences.edit();
//數據持久化,共6種類型String,set,int,long,float,booleaneditor.putString("name", "lucas");//獲取數據
SharedPreferences sharedPreferences= getSharedPreferences("user", MODE_PRIVATE);
//第二個參數為默認值,即user文件中未找到name,直接返回該值。String name=sharedPreferences.getString("name","");int age = sharedPreferences.getInt("age",0);
Activity和Activity之間怎么傳遞數據?
1.通過Intent傳遞數據
直接使用intent傳遞數據
// 發送方 Activity,
Intent intent = new Intent(SenderActivity.this, ReceiverActivity.class);
//通過putExtra直接發送數據
intent.putExtra("key_string", "Hello");
intent.putExtra("key_int", 100);
intent.putExtra("key_boolean", true);
startActivity(intent);// 接收方 Activity (onCreate中)
Intent intent = getIntent();
//通過getExtra獲取數據
String text = intent.getStringExtra("key_string");
int number = intent.getIntExtra("key_int", 0); // 0為默認值
boolean flag = intent.getBooleanExtra("key_boolean", false);
傳遞 Bundle 對象
// 發送方
Bundle bundle = new Bundle();
bundle.putString("name", "Alice");
bundle.putInt("age", 30);Intent intent = new Intent(this, ReceiverActivity.class);
intent.putExtras(bundle); // 或 intent.putExtra("myBundle", bundle)
startActivity(intent);// 接收方
Bundle bundle = getIntent().getExtras(); // 或 getIntent().getBundleExtra("myBundle")
String name = bundle.getString("name");
int age = bundle.getInt("age");
2.通過?SharedPreferences傳遞數據
在第一個Activity中通過SharedPreferences將數據存入xml文件中,再在第二個Activity中通過SharedPreferences獲取數據。
Fragment與Activity之間的傳值?
1.在activity中建一個bundle,把要傳的值存入bundle,然后通過fragment的setArguments(bundle)傳到fragment,在fragment中,用getArguments接收
Bundle bundle=new Bundle();bundle.putString("one","要傳的值");fragment.setArguments(bundle);FragmentManager fm = getFragmentManager();FragmentTransaction ft=fm.beginTransaction();ft.add(R.id.frame,myFragment,"myFragment");ft.commit();Bundle bundle=getArguments();String s=bundle.getString("one");
2.在Fragment中設置set,get方法,通過調用前設置屬性值,完成數據傳遞。
FragmentManager fm = getFragmentManager();
MyFragment myFragment = (MyFragment)fm.findFragmentById(R.id.fragment);
myFragment.setValue("傳值");//Fragment內部private String value;private void setValue(String value){this.value = value;}private String getValue(){return this.value;}
View和Viewgroup區別?
View
主要用來展示信息或與用戶進行交互;而ViewGroup
則更多地作為容器,用來組織和管理一組View
或其他ViewGroup
。?
ViewGroup
可以包含View
和其他ViewGroup
;而View
只能單獨存在。
?