Sobes.tech
Middle

What is the specific feature of changing positions on the Android platform?

sobes.tech AI

Answer from AI

When changing positions (screen rotation or other configuration changes) on Android, the current Activity is destroyed and a new one is created.

What happens:

  • The lifecycle methods of the old Activity are called: onPause(), onStop(), onDestroy().
  • The old Activity is removed from memory.
  • A new Activity is created.
  • The lifecycle methods of the new Activity are called: onCreate(), onStart(), onResume().

Specifics for QA:

  • Data loss: Unsaved data in input fields, selected list items, etc., may be lost if not properly saved.
  • UI state: Visibility of elements, scroll positions, selected tabs, and other UI states may reset to default.
  • Background operations: Ongoing background tasks (like data loading) may be interrupted or improperly completed.
  • Performance: Multiple configuration changes can slow down the app due to constant recreation of Activities.
  • Memory leaks: Improper resource management (e.g., listeners not unregistered in onDestroy()) can lead to memory leaks.

How developers handle it (and what QA should check first):

  • State saving: Using onSaveInstanceState(), onRestoreInstanceState(), ViewModel, Android Architecture Components (AAC).
    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("saved_text", myEditText.getText().toString());
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        if (savedInstanceState != null) {
            String savedText = savedInstanceState.getString("saved_text");
            if (savedText != null) {
                myEditText.setText(savedText);
            }
        }
    }
    
  • Handling Configuration Changes: Declaring android:configChanges in AndroidManifest.xml for cases where recreation of Activity is not needed (less common for orientation changes, but used for other configuration changes).
  • ViewModel lifecycle: ViewModel retains its state during configuration changes and is not destroyed with the Activity.
  • Fragments: Proper management of Fragment lifecycle.

Testing position change specifics:

  • Simulating screen rotations on different devices and emulators.
  • Testing data saving and restoring after position change.
  • Checking UI state (visibility, text, scroll, selected items).
  • Testing interruption/resumption of background tasks.
  • Checking for memory leaks (using Android Profiler).
  • Stress testing: multiple rapid position changes.

Tools for QA:

  • Android Studio (for debugging, log viewing, Profiler usage).
  • adb commands to simulate rotations.
  • Various emulators and real devices with different resolutions and Android versions.