added in [version 1.0.0](https://developer.android.com/topic/libraries/support-library/revisions)   
belongs to Maven artifact android.arch.lifecycle:livedata-core:1.1.1 Summary: [Ctors](https://developer.android.com/reference/android/arch/lifecycle/LiveData#pubctors) \| [Methods](https://developer.android.com/reference/android/arch/lifecycle/LiveData#pubmethods) \| [Protected Methods](https://developer.android.com/reference/android/arch/lifecycle/LiveData#promethods) \| [Inherited Methods](https://developer.android.com/reference/android/arch/lifecycle/LiveData#inhmethods)

# LiveData


`
public


abstract
class
LiveData
`


`

extends Object


`

`


`

|---|---|
| java.lang.Object ||
| ↳ | android.arch.lifecycle.LiveData\<T\> |

Known direct subclasses [MutableLiveData](https://developer.android.com/reference/android/arch/lifecycle/MutableLiveData)\<T\> [MutableLiveData](https://developer.android.com/reference/android/arch/lifecycle/MutableLiveData)\<T\> `https://developer.android.com/reference/android/arch/lifecycle/LiveData` which publicly exposes `https://developer.android.com/reference/android/arch/lifecycle/MutableLiveData#setValue(T)` and `https://developer.android.com/reference/android/arch/lifecycle/MutableLiveData#postValue(T)` method. Known indirect subclasses [MediatorLiveData](https://developer.android.com/reference/android/arch/lifecycle/MediatorLiveData)\<T\> [MediatorLiveData](https://developer.android.com/reference/android/arch/lifecycle/MediatorLiveData)\<T\> `https://developer.android.com/reference/android/arch/lifecycle/LiveData` subclass which may observe other `LiveData` objects and react on `OnChanged` events from them.   

*** ** * ** ***

LiveData is a data holder class that can be observed within a given lifecycle.
This means that an `https://developer.android.com/reference/android/arch/lifecycle/Observer` can be added in a pair with a `https://developer.android.com/reference/android/arch/lifecycle/LifecycleOwner`, and
this observer will be notified about modifications of the wrapped data only if the paired
LifecycleOwner is in active state. LifecycleOwner is considered as active, if its state is
`https://developer.android.com/reference/android/arch/lifecycle/Lifecycle.State#STARTED` or `https://developer.android.com/reference/android/arch/lifecycle/Lifecycle.State#RESUMED`. An observer added via
`https://developer.android.com/reference/android/arch/lifecycle/LiveData#observeForever(android.arch.lifecycle.Observer<T>)` is considered as always active and thus will be always notified
about modifications. For those observers, you should manually call
`https://developer.android.com/reference/android/arch/lifecycle/LiveData#removeObserver(android.arch.lifecycle.Observer<T>)`.

An observer added with a Lifecycle will be automatically removed if the corresponding
Lifecycle moves to `https://developer.android.com/reference/android/arch/lifecycle/Lifecycle.State#DESTROYED` state. This is especially useful for
activities and fragments where they can safely observe LiveData and not worry about leaks:
they will be instantly unsubscribed when they are destroyed.


In addition, LiveData has `https://developer.android.com/reference/android/arch/lifecycle/LiveData#onActive()` and `https://developer.android.com/reference/android/arch/lifecycle/LiveData#onInactive()` methods
to get notified when number of active `https://developer.android.com/reference/android/arch/lifecycle/Observer`s change between 0 and 1.
This allows LiveData to release any heavy resources when it does not have any Observers that
are actively observing.


This class is designed to hold individual data fields of `https://developer.android.com/reference/android/arch/lifecycle/ViewModel`,
but can also be used for sharing data between different modules in your application
in a decoupled fashion.
**See also:**

- `https://developer.android.com/reference/android/arch/lifecycle/ViewModel`

## Summary

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/android/arch/lifecycle/LiveData#LiveData()() ` |

| ### Public methods ||
|---|---|
| ` T` | ` https://developer.android.com/reference/android/arch/lifecycle/LiveData#getValue()() ` Returns the current value. |
| ` boolean` | ` https://developer.android.com/reference/android/arch/lifecycle/LiveData#hasActiveObservers()() ` Returns true if this LiveData has active observers. |
| ` boolean` | ` https://developer.android.com/reference/android/arch/lifecycle/LiveData#hasObservers()() ` Returns true if this LiveData has observers. |
| ` void` | ` https://developer.android.com/reference/android/arch/lifecycle/LiveData#observe(android.arch.lifecycle.LifecycleOwner, android.arch.lifecycle.Observer<T>)(https://developer.android.com/reference/android/arch/lifecycle/LifecycleOwner owner, https://developer.android.com/reference/android/arch/lifecycle/Observer<T> observer) ` Adds the given observer to the observers list within the lifespan of the given owner. |
| ` void` | ` https://developer.android.com/reference/android/arch/lifecycle/LiveData#observeForever(android.arch.lifecycle.Observer<T>)(https://developer.android.com/reference/android/arch/lifecycle/Observer<T> observer) ` Adds the given observer to the observers list. |
| ` void` | ` https://developer.android.com/reference/android/arch/lifecycle/LiveData#removeObserver(android.arch.lifecycle.Observer<T>)(https://developer.android.com/reference/android/arch/lifecycle/Observer<T> observer) ` Removes the given observer from the observers list. |
| ` void` | ` https://developer.android.com/reference/android/arch/lifecycle/LiveData#removeObservers(android.arch.lifecycle.LifecycleOwner)(https://developer.android.com/reference/android/arch/lifecycle/LifecycleOwner owner) ` Removes all observers that are tied to the given `https://developer.android.com/reference/android/arch/lifecycle/LifecycleOwner`. |

| ### Protected methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/android/arch/lifecycle/LiveData#onActive()() ` Called when the number of active observers change to 1 from 0. |
| ` void` | ` https://developer.android.com/reference/android/arch/lifecycle/LiveData#onInactive()() ` Called when the number of active observers change from 1 to 0. |
| ` void` | ` https://developer.android.com/reference/android/arch/lifecycle/LiveData#postValue(T)(T value) ` Posts a task to a main thread to set the given value. |
| ` void` | ` https://developer.android.com/reference/android/arch/lifecycle/LiveData#setValue(T)(T value) ` Sets the value. |

### Inherited methods

From class `

java.lang.Object

` `


Object` `
clone()
` `


boolean` `
equals(Object arg0)
` `


void` `
finalize()
` `


final

Class<?>` `
getClass()
` `


int` `
hashCode()
` `


final

void` `
notify()
` `


final

void` `
notifyAll()
` `


String` `
toString()
` `


final

void` `
wait(long arg0, int arg1)
` `


final

void` `
wait(long arg0)
` `


final

void` `
wait()
`

## Public constructors

### LiveData

added in [version 1.0.0](https://developer.android.com/topic/libraries/support-library/revisions)

```
LiveData ()
```

<br />

<br />

## Public methods

### getValue

added in [version 1.0.0](https://developer.android.com/topic/libraries/support-library/revisions)

```
T getValue ()
```

Returns the current value.
Note that calling this method on a background thread does not guarantee that the latest
value set will be received.

<br />

| Returns ||
|---|---|
| `T` | the current value <br /> |

### hasActiveObservers

added in [version 1.0.0](https://developer.android.com/topic/libraries/support-library/revisions)

```
boolean hasActiveObservers ()
```

Returns true if this LiveData has active observers.

<br />

| Returns ||
|---|---|
| `boolean` | true if this LiveData has active observers <br /> |

### hasObservers

added in [version 1.0.0](https://developer.android.com/topic/libraries/support-library/revisions)

```
boolean hasObservers ()
```

Returns true if this LiveData has observers.

<br />

| Returns ||
|---|---|
| `boolean` | true if this LiveData has observers <br /> |

### observe

added in [version 1.0.0](https://developer.android.com/topic/libraries/support-library/revisions)

```
void observe (LifecycleOwner owner, 
                Observer<T> observer)
```

Adds the given observer to the observers list within the lifespan of the given
owner. The events are dispatched on the main thread. If LiveData already has data
set, it will be delivered to the observer.


The observer will only receive events if the owner is in `https://developer.android.com/reference/android/arch/lifecycle/Lifecycle.State#STARTED`
or `https://developer.android.com/reference/android/arch/lifecycle/Lifecycle.State#RESUMED` state (active).


If the owner moves to the `https://developer.android.com/reference/android/arch/lifecycle/Lifecycle.State#DESTROYED` state, the observer will
automatically be removed.


When data changes while the `owner` is not active, it will not receive any updates.
If it becomes active again, it will receive the last available data automatically.


LiveData keeps a strong reference to the observer and the owner as long as the
given LifecycleOwner is not destroyed. When it is destroyed, LiveData removes references to
the observer \& the owner.


If the given owner is already in `https://developer.android.com/reference/android/arch/lifecycle/Lifecycle.State#DESTROYED` state, LiveData
ignores the call.


If the given owner, observer tuple is already in the list, the call is ignored.
If the observer is already in the list with another owner, LiveData throws an
`https://developer.android.com/reference/java/lang/IllegalArgumentException`.

<br />

| Parameters ||
|---|---|
| `owner` | `LifecycleOwner`: The LifecycleOwner which controls the observer <br /> |
| `observer` | `Observer`: The observer that will receive the events <br /> |

### observeForever

added in [version 1.0.0](https://developer.android.com/topic/libraries/support-library/revisions)

```
void observeForever (Observer<T> observer)
```

Adds the given observer to the observers list. This call is similar to
`https://developer.android.com/reference/android/arch/lifecycle/LiveData#observe(android.arch.lifecycle.LifecycleOwner, android.arch.lifecycle.Observer<T>)` with a LifecycleOwner, which
is always active. This means that the given observer will receive all events and will never
be automatically removed. You should manually call `https://developer.android.com/reference/android/arch/lifecycle/LiveData#removeObserver(android.arch.lifecycle.Observer<T>)` to stop
observing this LiveData.
While LiveData has one of such observers, it will be considered
as active.


If the observer was already added with an owner to this LiveData, LiveData throws an
`https://developer.android.com/reference/java/lang/IllegalArgumentException`.

<br />

| Parameters ||
|---|---|
| `observer` | `Observer`: The observer that will receive the events <br /> |

### removeObserver

added in [version 1.0.0](https://developer.android.com/topic/libraries/support-library/revisions)

```
void removeObserver (Observer<T> observer)
```

Removes the given observer from the observers list.

<br />

| Parameters ||
|---|---|
| `observer` | `Observer`: The Observer to receive events. <br /> |

### removeObservers

added in [version 1.0.0](https://developer.android.com/topic/libraries/support-library/revisions)

```
void removeObservers (LifecycleOwner owner)
```

Removes all observers that are tied to the given `https://developer.android.com/reference/android/arch/lifecycle/LifecycleOwner`.

<br />

| Parameters ||
|---|---|
| `owner` | `LifecycleOwner`: The `LifecycleOwner` scope for the observers to be removed. <br /> |

## Protected methods

### onActive

added in [version 1.0.0](https://developer.android.com/topic/libraries/support-library/revisions)

```
void onActive ()
```

Called when the number of active observers change to 1 from 0.


This callback can be used to know that this LiveData is being used thus should be kept
up to date.

<br />

### onInactive

added in [version 1.0.0](https://developer.android.com/topic/libraries/support-library/revisions)

```
void onInactive ()
```

Called when the number of active observers change from 1 to 0.


This does not mean that there are no observers left, there may still be observers but their
lifecycle states aren't `https://developer.android.com/reference/android/arch/lifecycle/Lifecycle.State#STARTED` or `https://developer.android.com/reference/android/arch/lifecycle/Lifecycle.State#RESUMED`
(like an Activity in the back stack).


You can check if there are observers via `https://developer.android.com/reference/android/arch/lifecycle/LiveData#hasObservers()`.

<br />

### postValue

added in [version 1.0.0](https://developer.android.com/topic/libraries/support-library/revisions)

```
void postValue (T value)
```

Posts a task to a main thread to set the given value. So if you have a following code
executed in the main thread:

```
 liveData.postValue("a");
 liveData.setValue("b");
 
```
The value "b" would be set at first and later the main thread would override it with the value "a".


If you called this method multiple times before a main thread executed a posted task, only
the last value would be dispatched.

<br />

| Parameters ||
|---|---|
| `value` | `T`: The new value <br /> |

### setValue

added in [version 1.0.0](https://developer.android.com/topic/libraries/support-library/revisions)

```
void setValue (T value)
```

Sets the value. If there are active observers, the value will be dispatched to them.


This method must be called from the main thread. If you need set a value from a background
thread, you can use `https://developer.android.com/reference/android/arch/lifecycle/LiveData#postValue(T)`

<br />

| Parameters ||
|---|---|
| `value` | `T`: The new value <br /> |

-

  ## Annotations

  - [OnLifecycleEvent](https://developer.android.com/reference/android/arch/lifecycle/OnLifecycleEvent)
-

  ## Interfaces

  - [DefaultLifecycleObserver](https://developer.android.com/reference/android/arch/lifecycle/DefaultLifecycleObserver)
  - [LifecycleObserver](https://developer.android.com/reference/android/arch/lifecycle/LifecycleObserver)
  - [LifecycleOwner](https://developer.android.com/reference/android/arch/lifecycle/LifecycleOwner)
  - [LifecycleRegistryOwner](https://developer.android.com/reference/android/arch/lifecycle/LifecycleRegistryOwner)
  - [Observer](https://developer.android.com/reference/android/arch/lifecycle/Observer)
  - [ViewModelProvider.Factory](https://developer.android.com/reference/android/arch/lifecycle/ViewModelProvider.Factory)
  - [ViewModelStoreOwner](https://developer.android.com/reference/android/arch/lifecycle/ViewModelStoreOwner)
-

  ## Classes

  - [AndroidViewModel](https://developer.android.com/reference/android/arch/lifecycle/AndroidViewModel)
  - [Lifecycle](https://developer.android.com/reference/android/arch/lifecycle/Lifecycle)
  - [LifecycleRegistry](https://developer.android.com/reference/android/arch/lifecycle/LifecycleRegistry)
  - [LifecycleService](https://developer.android.com/reference/android/arch/lifecycle/LifecycleService)
  - [LiveData](https://developer.android.com/reference/android/arch/lifecycle/LiveData)
  - [LiveDataReactiveStreams](https://developer.android.com/reference/android/arch/lifecycle/LiveDataReactiveStreams)
  - [MediatorLiveData](https://developer.android.com/reference/android/arch/lifecycle/MediatorLiveData)
  - [MutableLiveData](https://developer.android.com/reference/android/arch/lifecycle/MutableLiveData)
  - [ProcessLifecycleOwner](https://developer.android.com/reference/android/arch/lifecycle/ProcessLifecycleOwner)
  - [ServiceLifecycleDispatcher](https://developer.android.com/reference/android/arch/lifecycle/ServiceLifecycleDispatcher)
  - [Transformations](https://developer.android.com/reference/android/arch/lifecycle/Transformations)
  - [ViewModel](https://developer.android.com/reference/android/arch/lifecycle/ViewModel)
  - [ViewModelProvider](https://developer.android.com/reference/android/arch/lifecycle/ViewModelProvider)
  - [ViewModelProvider.AndroidViewModelFactory](https://developer.android.com/reference/android/arch/lifecycle/ViewModelProvider.AndroidViewModelFactory)
  - [ViewModelProvider.NewInstanceFactory](https://developer.android.com/reference/android/arch/lifecycle/ViewModelProvider.NewInstanceFactory)
  - [ViewModelProviders](https://developer.android.com/reference/android/arch/lifecycle/ViewModelProviders)
  - [ViewModelProviders.DefaultFactory](https://developer.android.com/reference/android/arch/lifecycle/ViewModelProviders.DefaultFactory)
  - [ViewModelStore](https://developer.android.com/reference/android/arch/lifecycle/ViewModelStore)
  - [ViewModelStores](https://developer.android.com/reference/android/arch/lifecycle/ViewModelStores)
-

  ## Enums

  - [Lifecycle.Event](https://developer.android.com/reference/android/arch/lifecycle/Lifecycle.Event)
  - [Lifecycle.State](https://developer.android.com/reference/android/arch/lifecycle/Lifecycle.State)