# Initialization
URL: /docs/a2a/initialization

Create the A2A client, initialize it and manage its lifecycle.

Before calling any operation you must create a client and call `initialize()`.
Initialization registers the broadcast receiver that listens for results coming
back from POS+. When you are done, call `dispose()` to release it.

<Callout type="warn">
  Always `initialize()` before the first request and `dispose()` when the
  screen/Activity is destroyed. Skipping `dispose()` leaks the receiver.
</Callout>

## Create and initialize

<CodeBlockTabs defaultValue="flutter" groupId="pp-platform" persist>
  <CodeBlockTabsList>
    <CodeBlockTabsTrigger value="flutter">
      Flutter
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="kotlin">
      Kotlin
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="java">
      Java
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="react-native">
      React Native
    </CodeBlockTabsTrigger>
  </CodeBlockTabsList>

  <CodeBlockTab value="flutter">
    ```dart
    class _CheckoutState extends State<Checkout> {
      final _pluspay = PPA2AClient();

      @override
      void initState() {
        super.initState();
        _pluspay.initialize();
      }

      @override
      void dispose() {
        _pluspay.dispose();
        super.dispose();
      }
    }
    ```
  </CodeBlockTab>

  <CodeBlockTab value="kotlin">
    ```kotlin
    class CheckoutActivity : AppCompatActivity() {
        private lateinit var client: PPA2AClient

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            client = PPA2AClient(this)
            client.initialize()
        }

        override fun onDestroy() {
            client.dispose()
            super.onDestroy()
        }
    }
    ```
  </CodeBlockTab>

  <CodeBlockTab value="java">
    ```java
    public class CheckoutActivity extends AppCompatActivity {
        private PPA2AClient client;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            client = new PPA2AClient(this);
            client.initialize();
        }

        @Override
        protected void onDestroy() {
            client.dispose();
            super.onDestroy();
        }
    }
    ```
  </CodeBlockTab>

  <CodeBlockTab value="react-native">
    ```tsx
    function Checkout() {
      const clientRef = useRef<PPA2AClient | null>(null);

      useEffect(() => {
        const client = new PPA2AClient();
        clientRef.current = client;
        client.initialize();
        return () => {
          client.dispose();
        };
      }, []);

      // ...
    }
    ```
  </CodeBlockTab>
</CodeBlockTabs>

## Resolving the POS+ application

When launching POS+, the client resolves the target application automatically.
On Flutter you can optionally provide a fallback package/activity name in case
auto-resolution fails on a particular device:

<CodeBlockTabs defaultValue="flutter" groupId="pp-platform" persist>
  <CodeBlockTabsList>
    <CodeBlockTabsTrigger value="flutter">
      Flutter
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="kotlin">
      Kotlin
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="java">
      Java
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="react-native">
      React Native
    </CodeBlockTabsTrigger>
  </CodeBlockTabsList>

  <CodeBlockTab value="flutter">
    ```dart
    await _pluspay.initialize(
      fallbackPackageName: 'com.pluspay.pos',
      fallbackActivityName: 'com.pluspay.pos.MainActivity',
    );
    ```
  </CodeBlockTab>

  <CodeBlockTab value="kotlin">
    ```kotlin
    // The native client resolves POS+ internally.
    client.initialize()

    // You can verify state before sending requests:
    if (client.isInitialized()) {
        // ready
    }
    ```
  </CodeBlockTab>

  <CodeBlockTab value="java">
    ```java
    // The native client resolves POS+ internally.
    client.initialize();

    // You can verify state before sending requests:
    if (client.isInitialized()) {
        // ready
    }
    ```
  </CodeBlockTab>

  <CodeBlockTab value="react-native">
    ```ts
    // The client resolves POS+ internally; just initialize once.
    await client.initialize();
    ```
  </CodeBlockTab>
</CodeBlockTabs>

## Language

POS+ can show its screens in a different language while it handles your
request. Send the language in the optional `locale` header field. When the
response has been delivered, POS+ switches back to the language it had before.

* Supported values: `tr`, `en`, `ar`, `de`, `fr`, `it`, `es`, `ru`, `zh`.
  Region variants such as `en_US` or `en-US` are also accepted.
* If `locale` is missing or not supported, POS+ stays in its current language.
* Kiosk devices ignore the field.
* `error_message` texts are always Turkish, whatever the `locale`. See
  [Error handling](/docs/a2a/error-handling#error-codes).

Set a default for every request in `initialize`, or pass `locale` to a single
request's `toRequest`. A per-request value wins over the default. Available
from Flutter 0.8.0 and React Native 0.4.0. The supported values are exported
as `ppSupportedLocales` (Flutter) and `PPSupportedLocales` (React Native).

<CodeBlockTabs defaultValue="flutter" groupId="pp-platform" persist>
  <CodeBlockTabsList>
    <CodeBlockTabsTrigger value="flutter">
      Flutter
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="kotlin">
      Kotlin
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="java">
      Java
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="react-native">
      React Native
    </CodeBlockTabsTrigger>
  </CodeBlockTabsList>

  <CodeBlockTab value="flutter">
    ```dart
    // Default for every request
    await _pluspay.initialize(locale: 'en');

    // Override for one request
    await _pluspay.startOrderPayment(
      PPOrderPaymentRequestModel.toRequest(orderCode: 'ORD001', locale: 'de'),
    );
    ```
  </CodeBlockTab>

  <CodeBlockTab value="kotlin">
    ```kotlin
    // Not available in pluspay_a2a_native yet.
    ```
  </CodeBlockTab>

  <CodeBlockTab value="java">
    ```java
    // Not available in pluspay_a2a_native yet.
    ```
  </CodeBlockTab>

  <CodeBlockTab value="react-native">
    ```ts
    // Default for every request
    await client.initialize({ locale: 'en' });

    // Override for one request
    await client.startOrderPayment(
      PPOrderPaymentRequestModel.toRequest({ orderCode: 'ORD001', locale: 'de' })
    );
    ```
  </CodeBlockTab>
</CodeBlockTabs>

## Two API styles (native)

The native package exposes **every** operation in two equivalent forms:

* **Callback** — pass a `PPA2ACallback<T>` with `onSuccess(T)` and
  `onError(PlusPayException)`.
* **Coroutine** — `suspend` functions that return the typed result directly
  (Kotlin only).

The Flutter package exposes a single `Future`-based API. All examples in the
following pages show the idiomatic form for each language.

Continue to [Payments](/docs/a2a/payments).