public final class

WalletFragment

extends Fragment
java.lang.Object
   ↳ android.app.Fragment
     ↳ com.google.android.gms.wallet.fragment.WalletFragment

Class Overview

This fragment is the simplest way to place a Wallet buy button or selection details UI in an application for the InstantBuy API. It automatically handles life cycle and user events, producing a MaskedWallet in the end. Being a fragment, the component can be added to an activity's layout with the XML below. Buy Button mode:

 <fragment
    android:name="com.google.android.gms.wallet.fragment.WalletFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    wallet:environment="sandbox"
    wallet:fragmentMode="buyButton"/>
Selection Details mode:
 <fragment
    android:name="com.google.android.gms.wallet.fragment.WalletFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    wallet:environment="sandbox"
    wallet:fragmentMode="selectionDetails"/>
Alternatively it may also be created programmatically with newInstance(WalletFragmentOptions) and added to the activity's fragment manager.

The fragment must be initialized exactly once by calling initialize(WalletFragmentInitParams). This sets a MaskedWalletRequest for buy button mode or MaskedWallet for selection details mode. For buy button mode the request may be modified with updateMaskedWalletRequest(MaskedWalletRequest). When the button is clicked, the masked payment credentials of the user will be retrieved and returned in your activity's onActivityResult callback with a request code specified in WalletFragmentInitParams.

The fragment may be in one of four states at any time. It starts out in UNINITIALIZED. UI components are disabled in this state. Once initialize(WalletFragmentInitParams) is called, the fragment transitions into READY, where it is ready for user interaction. When the buy/change button is clicked, it transitions into PROCESSING. The button will be disabled to prevent further user interaction. Finally when the masked wallet result comes back, the fragment goes back into READY. Sometimes the fragment can also get into WALLET_UNAVAILABLE when the Wallet service is temporarily not available. UI components will be disabled in this state. You may receive state transition updates by setting a WalletFragment.OnStateChangedListener via setOnStateChangedListener(OnStateChangedListener).

Use this class only if you are targeting API 12 and above. Otherwise, use SupportWalletFragment.

Summary

Nested Classes
interface WalletFragment.OnStateChangedListener  
[Expand]
Inherited Constants
From interface android.content.ComponentCallbacks2
Public Constructors
WalletFragment()
Public Methods
int getState()
Returns the current state of the fragment.
void initialize(WalletFragmentInitParams initParams)
Initializes the fragment.
static WalletFragment newInstance(WalletFragmentOptions options)
Creates a Wallet fragment with the given options.
void onActivityResult(int requestCode, int resultCode, Intent data)
void onCreate(Bundle savedInstanceState)
View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
void onDestroy()
void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState)
void onPause()
void onResume()
void onSaveInstanceState(Bundle outState)
void onStart()
void onStop()
void setEnabled(boolean enabled)
Sets a boolean that will enable or disable the fragment's UI components when it's in READY.
void setOnStateChangedListener(WalletFragment.OnStateChangedListener listener)
Sets a listener to receive state transition callbacks.
void updateMaskedWallet(MaskedWallet maskedWallet)
Modifies the MaskedWallet.
void updateMaskedWalletRequest(MaskedWalletRequest request)
Modifies the MaskedWalletRequest.
[Expand]
Inherited Methods
From class android.app.Fragment
From class java.lang.Object
From interface android.content.ComponentCallbacks2
From interface android.view.View.OnCreateContextMenuListener
From interface android.content.ComponentCallbacks

Public Constructors

public WalletFragment ()

Public Methods

public int getState ()

Returns the current state of the fragment. See WalletFragmentState for list of possible values. Note that UNKNOWN may be returned under the following circumstances:

  • before onStart() is executed
  • when Google Play services is unavailable or requires update

public void initialize (WalletFragmentInitParams initParams)

Initializes the fragment. This must be called exactly once. Any further invocations after the first will be ignored.

public static WalletFragment newInstance (WalletFragmentOptions options)

Creates a Wallet fragment with the given options.

public void onActivityResult (int requestCode, int resultCode, Intent data)

public void onCreate (Bundle savedInstanceState)

public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

public void onDestroy ()

public void onInflate (Activity activity, AttributeSet attrs, Bundle savedInstanceState)

public void onPause ()

public void onResume ()

public void onSaveInstanceState (Bundle outState)

public void onStart ()

public void onStop ()

public void setEnabled (boolean enabled)

Sets a boolean that will enable or disable the fragment's UI components when it's in READY. The UI components can not be disabled in other states and the boolean has no effect. This method is meant for temporarily disabling the buy button while you load data or update UI in your app.

public void setOnStateChangedListener (WalletFragment.OnStateChangedListener listener)

Sets a listener to receive state transition callbacks.

public void updateMaskedWallet (MaskedWallet maskedWallet)

Modifies the MaskedWallet. This should be called after initialize(WalletFragmentInitParams). Any non-null maskedWallet passed in here takes precedence over the MaskedWallet in WalletFragmentInitParams passed in initialize(WalletFragmentInitParams).

public void updateMaskedWalletRequest (MaskedWalletRequest request)

Modifies the MaskedWalletRequest. This should be called after initialize(WalletFragmentInitParams). Any non-null request passed in here takes precedence over the MaskedWalletRequest in WalletFragmentInitParams passed in initialize(WalletFragmentInitParams).

Note that any user buy button click event before this method call would load a masked wallet using whatever MaskedWalletRequest available at that time.