NativeActivity

public class NativeActivity
extends Activity implements SurfaceHolder.Callback2, InputQueue.Callback, ViewTreeObserver.OnGlobalLayoutListener

java.lang.Object
   ↳ android.content.Context
     ↳ android.content.ContextWrapper
       ↳ android.view.ContextThemeWrapper
         ↳ android.app.Activity
           ↳ android.app.NativeActivity


Convenience for implementing an activity that will be implemented purely in native code. That is, a game (or game-like thing). There is no need to derive from this class; you can simply declare it in your manifest, and use the NDK APIs from there.

A sample native activity is available in the NDK samples.

Summary

Constants

String META_DATA_FUNC_NAME

Optional meta-that can be in the manifest for this component, specifying the name of the main entry point for this native activity in the META_DATA_LIB_NAME native code.

String META_DATA_LIB_NAME

Optional meta-that can be in the manifest for this component, specifying the name of the native shared library to load.

Inherited constants

Inherited fields

Public constructors

NativeActivity()

Public methods

void onConfigurationChanged(Configuration newConfig)

Called by the system when the device configuration changes while your activity is running.

void onGlobalLayout()

Callback method to be invoked when the global layout state or the visibility of views within the view tree changes

void onInputQueueCreated(InputQueue queue)

Called when the given InputQueue is now associated with the thread making this call, so it can start receiving events from it.

void onInputQueueDestroyed(InputQueue queue)

Called when the given InputQueue is no longer associated with the thread and thus not dispatching events.

void onLowMemory()

This is called when the overall system is running low on memory, and actively running processes should trim their memory usage.

void onWindowFocusChanged(boolean hasFocus)

Called when the current Window of the activity gains or loses focus.

void surfaceChanged(SurfaceHolder holder, int format, int width, int height)

This is called immediately after any structural changes (format or size) have been made to the surface.

void surfaceCreated(SurfaceHolder holder)

This is called immediately after the surface is first created.

void surfaceDestroyed(SurfaceHolder holder)

This is called immediately before a surface is being destroyed.

void surfaceRedrawNeeded(SurfaceHolder holder)

Called when the application needs to redraw the content of its surface, after it is resized or for some other reason.

Protected methods

void onCreate(Bundle savedInstanceState)

Called when the activity is starting.

void onDestroy()

Perform any final cleanup before an activity is destroyed.

void onPause()

Called as part of the activity lifecycle when the user no longer actively interacts with the activity, but it is still visible on screen.

void onResume()

Called after onRestoreInstanceState(Bundle), onRestart(), or onPause().

void onSaveInstanceState(Bundle outState)

Called to retrieve per-instance state from an activity before being killed so that the state can be restored in onCreate(Bundle) or onRestoreInstanceState(Bundle) (the Bundle populated by this method will be passed to both).

void onStart()

Called after onCreate(Bundle) — or after onRestart() when the activity had been stopped, but is now again being displayed to the user.

void onStop()

Called when you are no longer visible to the user.

Inherited methods

Constants

META_DATA_FUNC_NAME

Added in API level 9
public static final String META_DATA_FUNC_NAME

Optional meta-that can be in the manifest for this component, specifying the name of the main entry point for this native activity in the META_DATA_LIB_NAME native code. If not specified, "ANativeActivity_onCreate" is used.

Constant Value: "android.app.func_name"

META_DATA_LIB_NAME

Added in API level 9
public static final String META_DATA_LIB_NAME

Optional meta-that can be in the manifest for this component, specifying the name of the native shared library to load. If not specified, "main" is used.

Constant Value: "android.app.lib_name"

Public constructors

NativeActivity

public NativeActivity ()

Public methods

onConfigurationChanged

Added in API level 9
public void onConfigurationChanged (Configuration newConfig)

Called by the system when the device configuration changes while your activity is running. Note that this will only be called if you have selected configurations you would like to handle with the R.attr.configChanges attribute in your manifest. If any configuration change occurs that is not selected to be reported by that attribute, then instead of reporting it the system will stop and restart the activity (to have it launched with the new configuration). The only exception is if a size-based configuration is not large enough to be considered significant, in which case the system will not recreate the activity and will instead call this method. For details on this see the documentation on size-based config change.

At the time that this function has been called, your Resources object will have been updated to return resource values matching the new configuration.

Parameters
newConfig Configuration: The new device configuration. This value cannot be null.

onGlobalLayout

Added in API level 9
public void onGlobalLayout ()

Callback method to be invoked when the global layout state or the visibility of views within the view tree changes

onInputQueueCreated

Added in API level 9
public void onInputQueueCreated (InputQueue queue)

Called when the given InputQueue is now associated with the thread making this call, so it can start receiving events from it.

Parameters
queue InputQueue

onInputQueueDestroyed

Added in API level 9
public void onInputQueueDestroyed (InputQueue queue)

Called when the given InputQueue is no longer associated with the thread and thus not dispatching events.

Parameters
queue InputQueue

onLowMemory

Added in API level 9
public void onLowMemory ()

This is called when the overall system is running low on memory, and actively running processes should trim their memory usage. While the exact point at which this will be called is not defined, generally it will happen when all background process have been killed. That is, before reaching the point of killing processes hosting service and foreground UI that we would like to avoid killing.

You should implement this method to release any caches or other unnecessary resources you may be holding on to. The system will perform a garbage collection for you after returning from this method.

Preferably, you should implement ComponentCallbacks2#onTrimMemory from ComponentCallbacks2 to incrementally unload your resources based on various levels of memory demands. That API is available for API level 14 and higher, so you should only use this onLowMemory() method as a fallback for older versions, which can be treated the same as ComponentCallbacks2#onTrimMemory with the ComponentCallbacks2.TRIM_MEMORY_COMPLETE level.

onWindowFocusChanged

Added in API level 9
public void onWindowFocusChanged (boolean hasFocus)

Called when the current Window of the activity gains or loses focus. This is the best indicator of whether this activity is the entity with which the user actively interacts. The default implementation clears the key tracking state, so should always be called.

Note that this provides information about global focus state, which is managed independently of activity lifecycle. As such, while focus changes will generally have some relation to lifecycle changes (an activity that is stopped will not generally get window focus), you should not rely on any particular order between the callbacks here and those in the other lifecycle methods such as onResume().

As a general rule, however, a foreground activity will have window focus... unless it has displayed other dialogs or popups that take input focus, in which case the activity itself will not have focus when the other windows have it. Likewise, the system may display system-level windows (such as the status bar notification panel or a system alert) which will temporarily take window input focus without pausing the foreground activity.

Starting with Build.VERSION_CODES.Q there can be multiple resumed activities at the same time in multi-window mode, so resumed state does not guarantee window focus even if there are no overlays above.

If the intent is to know when an activity is the topmost active, the one the user interacted with last among all activities but not including non-activity windows like dialogs and popups, then onTopResumedActivityChanged(boolean) should be used. On platform versions prior to Build.VERSION_CODES.Q, onResume() is the best indicator.

Parameters
hasFocus boolean: Whether the window of this activity has focus.

surfaceChanged

Added in API level 9
public void surfaceChanged (SurfaceHolder holder, 
                int format, 
                int width, 
                int height)

This is called immediately after any structural changes (format or size) have been made to the surface. You should at this point update the imagery in the surface. This method is always called at least once, after surfaceCreated(SurfaceHolder).

Parameters
holder SurfaceHolder: The SurfaceHolder whose surface has changed. This value cannot be null.

format int: The new PixelFormat of the surface. Value is PixelFormat.RGBA_8888, PixelFormat.RGBX_8888, PixelFormat.RGBA_F16, PixelFormat.RGBA_1010102, PixelFormat.RGB_888, PixelFormat.RGB_565, or android.graphics.PixelFormat.R_8

width int: The new width of the surface. Value is 0 or greater

height int: The new height of the surface. Value is 0 or greater

surfaceCreated

Added in API level 9
public void surfaceCreated (SurfaceHolder holder)

This is called immediately after the surface is first created. Implementations of this should start up whatever rendering code they desire. Note that only one thread can ever draw into a Surface, so you should not draw into the Surface here if your normal rendering will be in another thread.

Parameters
holder SurfaceHolder: The SurfaceHolder whose surface is being created. This value cannot be null.

surfaceDestroyed

Added in API level 9
public void surfaceDestroyed (SurfaceHolder holder)

This is called immediately before a surface is being destroyed. After returning from this call, you should no longer try to access this surface. If you have a rendering thread that directly accesses the surface, you must ensure that thread is no longer touching the Surface before returning from this function.

Parameters
holder SurfaceHolder: The SurfaceHolder whose surface is being destroyed. This value cannot be null.

surfaceRedrawNeeded

Added in API level 9
public void surfaceRedrawNeeded (SurfaceHolder holder)

Called when the application needs to redraw the content of its surface, after it is resized or for some other reason. By not returning from here until the redraw is complete, you can ensure that the user will not see your surface in a bad state (at its new size before it has been correctly drawn that way). This will typically be preceeded by a call to SurfaceHolder.Callback.surfaceChanged(SurfaceHolder, int, int, int). As of O, surfaceRedrawNeededAsync(SurfaceHolder, Runnable) may be implemented to provide a non-blocking implementation. If surfaceRedrawNeededAsync(SurfaceHolder, Runnable) is not implemented, then this will be called instead.

Parameters
holder SurfaceHolder: The SurfaceHolder whose surface has changed. This value cannot be null.

Protected methods

onCreate

Added in API level 9
protected void onCreate (Bundle savedInstanceState)

Called when the activity is starting. This is where most initialization should go: calling setContentView(int) to inflate the activity's UI, using findViewById(int) to programmatically interact with widgets in the UI, calling managedQuery(android.net.Uri, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String) to retrieve cursors for data being displayed, etc.

You can call finish() from within this function, in which case onDestroy() will be immediately called after onCreate(Bundle) without any of the rest of the activity lifecycle (onStart(), onResume(), onPause(), etc) executing.

Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.


This method must be called from the main thread of your app.
If you override this method you must call through to the superclass implementation.

Parameters
savedInstanceState Bundle: If the activity is being re-initialized after previously being shut down then this Bundle contains the data it most recently supplied in onSaveInstanceState(Bundle). Note: Otherwise it is null.

onDestroy

Added in API level 9
protected void onDestroy ()

Perform any final cleanup before an activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it), or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.

Note: do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here. This method is usually implemented to free resources like threads that are associated with an activity, so that a destroyed activity does not leave such things around while the rest of its application is still running. There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.

Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.


If you override this method you must call through to the superclass implementation.

onPause

Added in API level 9
protected void onPause ()

Called as part of the activity lifecycle when the user no longer actively interacts with the activity, but it is still visible on screen. The counterpart to onResume().

When activity B is launched in front of activity A, this callback will be invoked on A. B will not be created until A's onPause() returns, so be sure to not do anything lengthy here.

This callback is mostly used for saving any persistent state the activity is editing, to present a "edit in place" model to the user and making sure nothing is lost if there are not enough resources to start the new activity without first killing this one. This is also a good place to stop things that consume a noticeable amount of CPU in order to make the switch to the next activity as fast as possible.

On platform versions prior to Build.VERSION_CODES.Q this is also a good place to try to close exclusive-access devices or to release access to singleton resources. Starting with Build.VERSION_CODES.Q there can be multiple resumed activities in the system at the same time, so onTopResumedActivityChanged(boolean) should be used for that purpose instead.

If an activity is launched on top, after receiving this call you will usually receive a following call to onStop() (after the next activity has been resumed and displayed above). However in some cases there will be a direct call back to onResume() without going through the stopped state. An activity can also rest in paused state in some cases when in multi-window mode, still visible to user.

Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.


If you override this method you must call through to the superclass implementation.

onResume

Added in API level 9
protected void onResume ()

Called after onRestoreInstanceState(Bundle), onRestart(), or onPause(). This is usually a hint for your activity to start interacting with the user, which is a good indicator that the activity became active and ready to receive input. This sometimes could also be a transit state toward another resting state. For instance, an activity may be relaunched to onPause() due to configuration changes and the activity was visible, but wasn't the top-most activity of an activity task. onResume() is guaranteed to be called before onPause() in this case which honors the activity lifecycle policy and the activity eventually rests in onPause().

On platform versions prior to Build.VERSION_CODES.Q this is also a good place to try to open exclusive-access devices or to get access to singleton resources. Starting with Build.VERSION_CODES.Q there can be multiple resumed activities in the system simultaneously, so onTopResumedActivityChanged(boolean) should be used for that purpose instead.

Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.


If you override this method you must call through to the superclass implementation.

onSaveInstanceState

Added in API level 9
protected void onSaveInstanceState (Bundle outState)

Called to retrieve per-instance state from an activity before being killed so that the state can be restored in onCreate(Bundle) or onRestoreInstanceState(Bundle) (the Bundle populated by this method will be passed to both).

This method is called before an activity may be killed so that when it comes back some time in the future it can restore its state. For example, if activity B is launched in front of activity A, and at some point activity A is killed to reclaim resources, activity A will have a chance to save the current state of its user interface via this method so that when the user returns to activity A, the state of the user interface can be restored via onCreate(Bundle) or onRestoreInstanceState(Bundle).

Do not confuse this method with activity lifecycle callbacks such as onPause(), which is always called when the user no longer actively interacts with an activity, or onStop() which is called when activity becomes invisible. One example of when onPause() and onStop() is called and not this method is when a user navigates back from activity B to activity A: there is no need to call onSaveInstanceState(Bundle) on B because that particular instance will never be restored, so the system avoids calling it. An example when onPause() is called and not onSaveInstanceState(Bundle) is when activity B is launched in front of activity A: the system may avoid calling onSaveInstanceState(Bundle) on activity A if it isn't killed during the lifetime of B since the state of the user interface of A will stay intact.

The default implementation takes care of most of the UI per-instance state for you by calling View.onSaveInstanceState() on each view in the hierarchy that has an id, and by saving the id of the currently focused view (all of which is restored by the default implementation of onRestoreInstanceState(Bundle)). If you override this method to save additional information not captured by each individual view, you will likely want to call through to the default implementation, otherwise be prepared to save all of the state of each view yourself.

If called, this method will occur after onStop() for applications targeting platforms starting with Build.VERSION_CODES.P. For applications targeting earlier platform versions this method will occur before onStop() and there are no guarantees about whether it will occur before or after onPause().

Parameters
outState Bundle: Bundle in which to place your saved state. This value cannot be null.

onStart

Added in API level 9
protected void onStart ()

Called after onCreate(Bundle) — or after onRestart() when the activity had been stopped, but is now again being displayed to the user. It will usually be followed by onResume(). This is a good place to begin drawing visual elements, running animations, etc.

You can call finish() from within this function, in which case onStop() will be immediately called after onStart() without the lifecycle transitions in-between (onResume(), onPause(), etc) executing.

Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.


If you override this method you must call through to the superclass implementation.

onStop

Added in API level 9
protected void onStop ()

Called when you are no longer visible to the user. You will next receive either onRestart(), onDestroy(), or nothing, depending on later user activity. This is a good place to stop refreshing UI, running animations and other visual things.

Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.


If you override this method you must call through to the superclass implementation.