Middle+
Tell me about anti-patterns in Android development.
sobes.tech AI
Answer from AI
- Using
AsyncTaskfor long operations (it's better to use Kotlin Coroutines or RxJava). - Passing View/Context into ViewModel (causes memory leaks).
- Inheriting directly from
ActivityorFragmentfor basic logic (it's better to use composition). - Hardcoding strings, numbers, colors instead of using resources.
- Blocking the main thread (
MainThread) with long synchronous operations. - Using static variables to store data that requires lifecycle awareness.
- Excessive nesting of Layouts, leading to performance issues (
ConstraintLayoutorLinearLayoutwith weight is better). - Not handling configuration changes (screen rotations, locale changes).
- Direct access to databases or network operations from the UI thread.
- Incorrect use of
SharedPreferencesfor storing large objects or sensitive data. - Lack of exception handling or improper implementation (e.g., empty
catchblocks). - Using implicit intents without checking for available activities (
resolveActivity). - Improper management of subscriptions (e.g., in RxJava), leading to leaks.
Example of excessive nested Layouts:
<!-- Bad -->
<LinearLayout>
<LinearLayout>
<TextView/>
</LinearLayout>
</LinearLayout>
<!-- Good -->
<LinearLayout>
<TextView/>
</LinearLayout>