{% setvar book_path %}/reference/kotlin/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

androidx.fragment.app

{% setvar page_path %}androidx/fragment/app/package-summary.html{% endsetvar %} {% setvar can_switch %}1{% endsetvar %} {% include "reference/_kotlin_switcher2.md" %}

Room is a Database Object Mapping library that makes it easy to access database on Android applications.

Rather than hiding the details of SQLite, Room tries to embrace them by providing convenient APIs to query the database and also verify such queries at compile time. This allows you to access the full power of SQLite while having the type safety provided by Java SQL query builders.

There are 3 major components in Room.

Below is a sample of a simple database.
// File: Song.java
{@literal @}Entity
public class User {
  {@literal @}PrimaryKey
  private int id;
  private String name;
  {@literal @}ColumnInfo(name = "release_year")
  private int releaseYear;
  // getters and setters are ignored for brevity but they are required for Room to work.
}
// File: SongDao.java
{@literal @}Dao
public interface SongDao {
  {@literal @}Query("SELECT * FROM song")
  List<Song> loadAll();
  {@literal @}Query("SELECT * FROM song WHERE id IN (:songIds)")
  List<Song> loadAllBySongId(int... songIds);
  {@literal @}Query("SELECT * FROM song WHERE name LIKE :name AND release_year = :year LIMIT 1")
  Song loadOneByNameAndReleaseYear(String first, int year);
  {@literal @}Insert
  void insertAll(Song... songs);
  {@literal @}Delete
  void delete(Song song);
}
// File: MusicDatabase.java
{@literal @}Database(entities = {Song.java})
public abstract class MusicDatabase extends RoomDatabase {
  public abstract SongDao userDao();
}
You can create an instance of {@code MusicDatabase} as follows:
MusicDatabase db = Room
    .databaseBuilder(getApplicationContext(), MusicDatabase.class, "database-name")
    .build();
Since Room verifies your queries at compile time, it also detects information about which tables are accessed by the query or what columns are present in the response.

You can observe a particular table for changes using the InvalidationTracker class which you can acquire via RoomDatabase.getInvalidationTracker.

For convenience, Room allows you to return LiveData from Query methods. It will automatically observe the related tables as long as the {@code LiveData} has active observers.

// This live data will automatically dispatch changes as the database changes.
{@literal @}Query("SELECT * FROM song ORDER BY name LIMIT 5")
LiveData<Song> loadFirstFiveSongs();

You can also return arbitrary data objects from your query results as long as the fields in the object match the list of columns in the query response. This makes it very easy to write applications that drive the UI from persistent storage.

class IdAndSongHeader {
  int id;
  {@literal @}ColumnInfo(name = "header")
  String header;
}
// DAO
{@literal @}Query("SELECT id, name || '-' || release_year AS header FROM user")
public IdAndSongHeader[] loadSongHeaders();
If there is a mismatch between the query result and the POJO, Room will print a warning during compilation.

Please see the documentation of individual classes for details.

Interfaces

FragmentManager.BackStackEntry

Representation of an entry on the fragment back stack, as created with FragmentTransaction.addToBackStack().

FragmentManager.OnBackStackChangedListener

Interface to watch for changes to the back stack.

FragmentOnAttachListener

Listener for receiving a callback immediately following Fragment#onAttach(Context).

FragmentResultListener

Listener for handling fragment results.

FragmentResultOwner

A class that manages passing data between fragments.

Classes

DialogFragment

Static library support version of the framework's android.app.DialogFragment.

Fragment

Static library support version of the framework's android.app.Fragment.

Fragment.SavedState

State information that has been retrieved from a fragment instance through FragmentManager.saveFragmentInstanceState.

FragmentActivity

Base class for activities that want to use the support-based Fragments.

FragmentContainer

Callbacks to a Fragment's container.

FragmentContainerView

FragmentContainerView is a customized Layout designed specifically for Fragments.

FragmentController

Provides integration points with a FragmentManager for a fragment host.

FragmentFactory

Interface used to control the instantiation of Fragment instances.

FragmentHostCallback

Integration points with the Fragment host.

FragmentManager

Static library support version of the framework's android.app.FragmentManager.

FragmentManager.FragmentLifecycleCallbacks

Callback interface for listening to fragment state changes that happen within a given FragmentManager.

FragmentManagerNonConfig

This class is deprecated.

Have your FragmentHostCallback implement androidx.lifecycle.ViewModelStoreOwner to automatically retain the Fragment's non configuration state.

FragmentPagerAdapter

This class is deprecated.

Switch to androidx.viewpager2.widget.ViewPager2 and use androidx.viewpager2.adapter.FragmentStateAdapter instead.

FragmentStatePagerAdapter

This class is deprecated.

Switch to androidx.viewpager2.widget.ViewPager2 and use androidx.viewpager2.adapter.FragmentStateAdapter instead.

FragmentTabHost

This class is deprecated.

Use TabLayout and ViewPager instead.

FragmentTransaction

Static library support version of the framework's android.app.FragmentTransaction.

ListFragment

Static library support version of the framework's android.app.ListFragment.

Exceptions

Fragment.InstantiationException

Thrown by FragmentFactory#instantiate(ClassLoader, String) when there is an instantiation failure.

Annotations

FragmentStateManagerControl