StateType
- The type of the states, which are stored by the decoratorpublic abstract class StatefulTabSwitcherDecorator<StateType> extends TabSwitcherDecorator
TabSwitcherDecorator
, which allows to store any arbitrary state for each tab of the
associated TabSwitcher
. The state is kept even if the tab is currently not shown.
However, it is possible that states are vanished, if few memory is available, because they are
stored using SoftReference
s. Unlike the parameters, which can be set for an individual
Tab
using the method Tab.setParameters(Bundle)
, these states are not restored
when restoring the state of a TabSwitcher
, e.g. after orientation changes. Instead the
state is vanished and will be created from scratch when the tab is shown for the next time.
If it is necessary to store some data from a state, it can be put into the bundle, which is
passed to the onSaveInstanceState(View, Tab, int, int, Object, Bundle)
method. The data
can later be retrieved from the bundle when the method
onCreateState(Context, TabSwitcher, View, Tab, int, int, Bundle)
is invoked for the next
time.
It is recommended to use the class AbstractState
as a base class for implementing states.
This is not necessary though. Said class stores a reference to the tab, it corresponds to and
implements the interface Restorable
to be able to store and restore its state. By
implementing the methods of this interface and calling them in the decorator's onSaveInstanceState(View, Tab, int, int, Object, Bundle)
, respectively onCreateState(Context, TabSwitcher, View, Tab, int, int, Bundle)
method, the selected
properties of the state can be stored and restored.
If TabSwitcher.areSavedStatesClearedWhenRemovingTabs()
returns true, the state of tabs is
cleared when a tab has been removed from the TabSwitcher
. This can be prevented by using
the method TabSwitcher.clearSavedStatesWhenRemovingTabs(boolean)
. To manually remove the
state of a specific tab, the method clearState(Tab)
can be used. The method clearAllStates()
allows to remove the states of all tabs accordingly.
IMPORTANT: States must not store references to views, which have been inflated in the decorator's
TabSwitcherDecorator.onInflateView(LayoutInflater, ViewGroup, int)
method, because these views can be reused
across all tabs of the TabSwitcher
and therefore are not guaranteed to belong to the same
tab for the whole lifetime of the decorator.
Constructor and Description |
---|
StatefulTabSwitcherDecorator() |
Modifier and Type | Method and Description |
---|---|
void |
clearAllStates()
Removes the states of all tabs.
|
void |
clearState(Tab tab)
Removes the state of a specific tab.
|
StateType |
getState(Tab tab)
Returns the state of a specific tab.
|
protected void |
onClearState(StateType state)
The method, which is invoked on subclasses, when a state is cleared using the
clearState(Tab) or clearAllStates() method. |
protected abstract StateType |
onCreateState(android.content.Context context,
TabSwitcher tabSwitcher,
android.view.View view,
Tab tab,
int index,
int viewType,
android.os.Bundle savedInstanceState)
The method, which is invoked on subclasses in order to create the state for a specific tab.
|
void |
onSaveInstanceState(android.view.View view,
Tab tab,
int index,
int viewType,
android.os.Bundle outState)
The method, which is invoked, when the view, which is used to visualize a tab, is removed.
|
protected void |
onSaveInstanceState(android.view.View view,
Tab tab,
int index,
int viewType,
StateType state,
android.os.Bundle outState)
The method, which is invoked, when the view, which is used to visualize a tab, is removed.
|
void |
onShowTab(android.content.Context context,
TabSwitcher tabSwitcher,
android.view.View view,
Tab tab,
int index,
int viewType,
android.os.Bundle savedInstanceState)
The method which is invoked, when the view, which is used to visualize a tab, should be
shown, respectively when it should be refreshed.
|
protected abstract void |
onShowTab(android.content.Context context,
TabSwitcher tabSwitcher,
android.view.View view,
Tab tab,
int index,
int viewType,
StateType state,
android.os.Bundle savedInstanceState)
The method which is invoked, when the view, which is used to visualize a tab, should be
shown, respectively when it should be refreshed.
|
applyDecorator, getViewType, getViewTypeCount, inflateView, onInflateView, saveInstanceState
@Nullable protected abstract StateType onCreateState(@NonNull android.content.Context context, @NonNull TabSwitcher tabSwitcher, @NonNull android.view.View view, @NonNull Tab tab, int index, int viewType, @Nullable android.os.Bundle savedInstanceState)
context
- The context, the tab switcher belongs to, as an instance of the class Context
. The context may not be nulltabSwitcher
- The tab switcher, whose tabs are visualized by the decorator, as an instance of the
type TabSwitcher
. The tab switcher may not be nullview
- The view, which is used to visualize the tab, as an instance of the class View
. The view may not be nulltab
- The tab, for which a state should be created, as an instance of the class Tab
. The tab may not be nullindex
- The index of the tab, for which a state should be created, as an Integer
valueviewType
- The view type of the tab, for which a state should be created, as an Integer
valuesavedInstanceState
- The bundle, which has previously been used to save the state of the tab as an
instance of the class Bundle
or null, if no saved state is availableStateType
or null, if no state has been createdprotected void onClearState(@NonNull StateType state)
clearState(Tab)
or clearAllStates()
method.state
- The state, which is cleared, as an instance of the generic type StateType
.
The state may not be nullprotected abstract void onShowTab(@NonNull android.content.Context context, @NonNull TabSwitcher tabSwitcher, @NonNull android.view.View view, @NonNull Tab tab, int index, int viewType, @Nullable StateType state, @Nullable android.os.Bundle savedInstanceState)
context
- The context, the tab switcher belongs to, as an instance of the class Context
. The context may not be nulltabSwitcher
- The tab switcher, whose tabs are visualized by the decorator, as an instance of the
type TabSwitcher
. The tab switcher may not be nullview
- The view, which is used to visualize the tab, as an instance of the class View
. The view may not be nulltab
- The tab, which should be visualized, as an instance of the class Tab
. The tab
may not be nullindex
- The index of the tab, which should be visualized, as an Integer
valueviewType
- The view type of the tab, which should be visualized, as an Integer
valuestate
- The state of the tab, which should be visualized, as an instance of the generic type
StateType
or null, if no state has been created yetsavedInstanceState
- The bundle, which has previously been used to save the state of the view as an
instance of the class Bundle
or null, if no saved state is availableonShowTab(Context, TabSwitcher, View, Tab, int, int, Bundle)
protected void onSaveInstanceState(@NonNull android.view.View view, @NonNull Tab tab, int index, int viewType, @Nullable StateType state, @NonNull android.os.Bundle outState)
view
- The view, which is used to visualize the tab, as an instance of the class View
tab
- The tab, whose state should be saved, as an instance of the class Tab
. The
tab may not be nullindex
- The index of the tab, whose state should be saved, as an Integer
valueviewType
- The view type of the tab, whose state should be saved, as an Integer
valuestate
- The state of tab, whose state should be saved, as an instance of the generic type
StateType
or null, if no state has been created yetoutState
- The bundle, the state of the tab should be saved to, as an instance of the class
Bundle
. The bundle may not be nullonSaveInstanceState(View, Tab, int, int, Bundle)
@Nullable public final StateType getState(@NonNull Tab tab)
tab
- The tab, whose state should be returned, as an instance of the class Tab
. The
tab may not be nullStateType
or
null, if no state has been created yet or if it was removedpublic final void clearState(@NonNull Tab tab)
tab
- The tab, whose state should be removed, as an instance of the class Tab
. The
tab may not be nullpublic final void clearAllStates()
public final void onShowTab(@NonNull android.content.Context context, @NonNull TabSwitcher tabSwitcher, @NonNull android.view.View view, @NonNull Tab tab, int index, int viewType, @Nullable android.os.Bundle savedInstanceState)
TabSwitcherDecorator
onShowTab
in class TabSwitcherDecorator
context
- The context, the tab switcher belongs to, as an instance of the class Context
. The context may not be nulltabSwitcher
- The tab switcher, whose tabs are visualized by the decorator, as an instance of the
type TabSwitcher
. The tab switcher may not be nullview
- The view, which is used to visualize the tab, as an instance of the class View
. The view may not be nulltab
- The tab, which should be visualized, as an instance of the class Tab
. The tab
may not be nullindex
- The index of the tab, which should be visualized, as an Integer
valueviewType
- The view type of the tab, which should be visualized, as an Integer
valuesavedInstanceState
- The bundle, which has previously been used to save the state of the view as an
instance of the class Bundle
or null, if no saved state is availablepublic final void onSaveInstanceState(@NonNull android.view.View view, @NonNull Tab tab, int index, int viewType, @NonNull android.os.Bundle outState)
TabSwitcherDecorator
onSaveInstanceState
in class TabSwitcherDecorator
view
- The view, which is used to visualize the tab, as an instance of the class View
tab
- The tab, whose state should be saved, as an instance of the class Tab
. The
tab may not be nullindex
- The index of the tab, whose state should be saved, as an Integer
valueviewType
- The view type of the tab, whose state should be saved, as an Integer
valueoutState
- The bundle, the state of the tab should be saved to, as an instance of the class
Bundle
. The bundle may not be null