# Class CarContext

java.lang.Object
com.codename1.car.CarContext

public final class CarContext extends Object

The live handle to a connected head unit. Created by the framework when a car connects and handed to `CarApplication#onCreateRootScreen(CarContext)` and to every `CarActionListener`. Use it to drive the head-unit back stack (`pushScreen(CarScreen)`, `popScreen()`), surface transient messages (`showToast(String)`) and query driver-distraction limits.

The context delegates rendering to a platform `com.codename1.car.spi.CarBridge`. On the simulator and on ports without in-car projection the bridge is absent, so every method here is a harmless no-op and `isConnected()` returns false -- application code never needs platform `if`s.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `finish()`
  Tears down the in-car experience and dismisses the app from the head unit.
  `int`
  `getGridItemLimit()`
  Returns the maximum items the head unit will show in a `CarGridTemplate`, or 0 if unknown.
  `int`
  `getListRowLimit()`
  Returns the maximum rows the head unit will show in a `CarListTemplate`, or 0 if unknown.
  `CarScreen`
  `getTopScreen()`
  Returns the screen currently at the top of the in-car back stack, or null if none.
  `boolean`
  `isConnected()`
  Returns true while a head unit is connected and rendering.
  `void`
  `popScreen()`
  Pops the top screen, returning to the one beneath it.
  `void`
  `pushScreen(CarScreen screen)`
  Pushes a screen onto the head-unit back stack and renders it.
  `void`
  `showToast(String message)`
  Shows a short transient message on the head unit.
  `void`
  `showToast(String message, int durationSeconds)`
  Shows a transient message on the head unit for a requested duration.

  ### Methods inherited from class Object

  `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### isConnected

    public boolean isConnected()
    Returns true while a head unit is connected and rendering.

  - ### getTopScreen

    public CarScreen getTopScreen()
    Returns the screen currently at the top of the in-car back stack, or null if none.

  - ### pushScreen

    public void pushScreen(CarScreen screen)

    Pushes a screen onto the head-unit back stack and renders it. The previous screen is paused.

    ###### Parameters

    - `screen`: the screen to show

  - ### popScreen

    public void popScreen()
    Pops the top screen, returning to the one beneath it. No effect when only the root remains.

  - ### finish

    public void finish()
    Tears down the in-car experience and dismisses the app from the head unit.

  - ### showToast

    public void showToast(String message)

    Shows a short transient message on the head unit.

    ###### Parameters

    - `message`: the text to display

  - ### showToast

    public void showToast(String message, int durationSeconds)

    Shows a transient message on the head unit for a requested duration.

    ###### Parameters

    - `message`: the text to display

    - `durationSeconds`: requested duration in seconds (the head unit may clamp it)

  - ### getListRowLimit

    public int getListRowLimit()

    Returns the maximum rows the head unit will show in a `CarListTemplate`, or 0 if unknown. Honour this when populating lists -- rows beyond the limit are dropped by the platform.

    ###### Returns

    the row limit, or 0 if unknown

  - ### getGridItemLimit

    public int getGridItemLimit()

    Returns the maximum items the head unit will show in a `CarGridTemplate`, or 0 if unknown.

    ###### Returns

    the grid item limit, or 0 if unknown