Rana Maninder Singh
Lesson 4 of 8Android App Development

State Management and Data Persistence

Lesson opener

In one line: UseState for in-memory UI state (same as React web).

Why it matters: This shows up at work when you need to useState for in-memory UI state (same as React web).

Before this lesson: Navigation

Before you start

ResourceLink
PreviousNavigation
NextNumber guessing game capstone
Time~45 minutes

What you will learn

  • useState for in-memory UI state (same as React web)
  • AsyncStorage for data that survives app restarts
  • When to use each — and the offline expectation on mobile

Section 1 — Web vs mobile data expectations

Plain English:

In a website, data can live on a server and be fetched on each page load. In a mobile app, users expect data to work offline. Your app must store data locally and sync when connected (sync comes later — local first).

StorageWebReact Native
Session UI stateuseStateuseState — same
Persist after closelocalStorage (sync)AsyncStorage (async)
Server datafetch()fetch() — same

Section 2 — useState (unchanged from React)

import { useState } from 'react';
import { View, Text, Button } from 'react-native';

export default function Counter() {
  const [count, setCount] = useState(0);

  return (
    <View>
      <Text>Count: {count}</Text>
      <Button title="Add one" onPress={() => setCount(count + 1)} />
    </View>
  );
}

When count resets: app reload in dev, or OS kills the process. Not when navigating between screens (component stays mounted) unless you unmount it.


Section 3 — AsyncStorage

Install:

npx expo install @react-native-async-storage/async-storage
import AsyncStorage from '@react-native-async-storage/async-storage';

// Save data (must be a string)
await AsyncStorage.setItem('user_score', JSON.stringify(score));

// Load data
const saved = await AsyncStorage.getItem('user_score');
const score = saved ? JSON.parse(saved) : 0;

// Remove
await AsyncStorage.removeItem('user_score');

Breaking it down:

Use this when: high scores, user preferences, draft form text before lifecycle pause.


Section 4 — Load on mount pattern

import { useState, useEffect } from 'react';
import AsyncStorage from '@react-native-async-storage/async-storage';

const KEY = 'user_score';

export default function ScoreDisplay() {
  const [score, setScore] = useState(null);

  useEffect(() => {
    (async () => {
      const saved = await AsyncStorage.getItem(KEY);
      setScore(saved ? JSON.parse(saved) : 0);
    })();
  }, []);

  async function saveScore(newScore) {
    setScore(newScore);
    await AsyncStorage.setItem(KEY, JSON.stringify(newScore));
  }

  if (score === null) {
    return <Text>Loading...</Text>;
  }

  return <Text>Score: {score}</Text>;
}

Lifecycle link: When the user switches apps (android1), in-memory useState may be lost if the OS destroys your process. AsyncStorage survives — that is why the capstone game persists high scores.


Practice

Do this now: Build a counter that persists across app restarts using AsyncStorage. Force-close Expo Go and reopen — count should remain.

Checklist

  • [ ] Can you explain when to use useState vs AsyncStorage?
  • [ ] Can you save and load JSON with await?
  • [ ] Do you know why AsyncStorage is async?

What's next

Number guessing game — capstone using state, storage, input, and navigation together.