Sobes.tech
Middle+

Tell me about anti-patterns in Android development.

sobes.tech AI

Answer from AI

  • Using AsyncTask for long operations (it's better to use Kotlin Coroutines or RxJava).
  • Passing View/Context into ViewModel (causes memory leaks).
  • Inheriting directly from Activity or Fragment for 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 (ConstraintLayout or LinearLayout with 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 SharedPreferences for storing large objects or sensitive data.
  • Lack of exception handling or improper implementation (e.g., empty catch blocks).
  • 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>