Table of Contents

Class AndroidAvaloniaMainActivity<TUi>

Namespace
VpnHood.AppUi.Hosting.Avalonia.Android
Assembly
VpnHood.AppUi.Hosting.Avalonia.Android.dll

Represents the Avalonia UI's main activity on Android: Avalonia's main activity, raising the app's activity events itself and leaving the rest to the app's main activity handler.

public class AndroidAvaloniaMainActivity<TUi> : AvaloniaMainActivity, IComponentCallbacks2, IComponentCallbacks, KeyEvent.ICallback, LayoutInflater.IFactory2, LayoutInflater.IFactory, View.IOnCreateContextMenuListener, Window.ICallback, KeyEventDispatcher.IComponent, IFullyDrawnReporterOwner, IOnBackPressedDispatcherOwner, IContextAware, IActivityResultCaller, IActivityResultRegistryOwner, IOnMultiWindowModeChangedProvider, IOnNewIntentProvider, IOnPictureInPictureModeChangedProvider, IOnUserLeaveHintProvider, IOnConfigurationChangedProvider, IOnTrimMemoryProvider, IMenuHost, IHasDefaultViewModelProviderFactory, IViewModelStoreOwner, INavigationEventDispatcherOwner, ISavedStateRegistryOwner, ILifecycleOwner, ActivityCompat.IOnRequestPermissionsResultCallback, ActivityCompat.IRequestPermissionsRequestCodeValidator, ActionBarDrawerToggle.IDelegateProvider, IAppCompatCallback, TaskStackBuilder.ISupportParentable, IJavaObject, IJavaPeerable, IDisposable, IAvaloniaActivity, IActivityResultHandler, IActivityNavigationService, IActivityEvent where TUi : Application, IAvaloniaUi

Type Parameters

TUi

The UI's application.

Inheritance
AvaloniaActivity
AvaloniaMainActivity
AndroidAvaloniaMainActivity<TUi>
Implements
IAvaloniaActivity
IActivityResultHandler
IActivityNavigationService
Inherited Members
AvaloniaActivity.OnBackPressed()
AvaloniaActivity.OnStop()
AvaloniaActivity.OnStart()
AvaloniaActivity.OnBackInvoked()
AvaloniaActivity.ActivityResult
AvaloniaActivity.RequestPermissionsResult
AvaloniaActivity.Content
AvaloniaActivity.BackRequested

Remarks

The handler gives the app the UI context it needs to ask for VPN permission, and takes the access-key intents and the activity results. The Back key, a remote's or a phone's, reaches Avalonia, which the UI answers. On a phone the activity requests portrait, as the UI's wide layout is for tablets, TVs and windows; it asks at run time, not in the manifest, whose portrait would make Google Play hide the app from TVs.

Properties

Activity

Gets the activity.

public Activity Activity { get; }

Property Value

Activity

Methods

CreateActivityOptions()

Creates the activity's options: the access-key schemes and file types a head takes. The default takes none.

protected virtual AndroidMainActivityOptions CreateActivityOptions()

Returns

AndroidMainActivityOptions

The options.

OnActivityResult(int, Result, Intent?)

Called when an activity you launched exits, giving you the requestCode you started it with, the resultCode it returned, and any additional data from it.

protected override void OnActivityResult(int requestCode, Result resultCode, Intent? data)

Parameters

requestCode int

The integer request code originally supplied to startActivityForResult(), allowing you to identify who this result came from.

resultCode Result

The integer result code returned by the child activity through its setResult().

data Intent

An Intent, which can return result data to the caller (various data can be attached to Intent "extras").

Remarks

Called when an activity you launched exits, giving you the requestCode you started it with, the resultCode it returned, and any additional data from it. The resultCode will be RESULT_CANCELED if the activity explicitly returned that, didn't return any result, or crashed during its operation. An activity can never receive a result in the resumed state. You can count on onResume() being called after this method, though not necessarily immediately after. If the activity was resumed, it will be paused and the result will be delivered, followed by onResume(). If the activity wasn't in the resumed state, then the result will be delivered, with onResume() called sometime later when the activity becomes active again. This method is never invoked if your activity sets noHistory to true.

See also:

Android reference for

    <pre><code class="lang-csharp">android.app.Activity.onActivityResult</code></pre>

    .</a>
</format>

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

OnConfigurationChanged(Configuration)

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

public override void OnConfigurationChanged(Configuration newConfig)

Parameters

newConfig Configuration

The new device configuration.

Remarks

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.

Android reference for

    <pre><code class="lang-csharp">android.app.Activity.onConfigurationChanged</code></pre>

    .</a>
</format>

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

OnCreate(Bundle?)

Called when the activity is starting.

protected override void OnCreate(Bundle? savedInstanceState)

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. <b>Note: Otherwise it is null.</b>

Remarks

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,String[],String,String[],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.

See also:

Android reference for

    <pre><code class="lang-csharp">android.app.Activity.onCreate</code></pre>

    .</a>
</format>

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Exceptions

InvalidOperationException

The app's options name no UI content.

OnDestroy()

Perform any final cleanup before an activity is destroyed.

protected override void OnDestroy()

Remarks

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.

See also:

Android reference for

    <pre><code class="lang-csharp">android.app.Activity.onDestroy</code></pre>

    .</a>
</format>

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

OnKeyDown(Keycode, KeyEvent?)

Called when a key was pressed down and not handled by any of the views inside of the activity.

public override bool OnKeyDown(Keycode keyCode, KeyEvent? e)

Parameters

keyCode Keycode

int: The value in event.getKeyCode().

e KeyEvent

To be added.

Returns

bool

Return true to prevent this event from being propagated further, or false to indicate that you have not handled this event and it should continue to be propagated.

Remarks

Called when a key was pressed down and not handled by any of the views inside of the activity. So, for example, key presses while the cursor is inside a TextView will not trigger the event (unless it is a navigation to another object) because TextView handles its own key presses. If the focused view didn't want this event, this method is called. The default implementation takes care of KeyEvent.KEYCODE_BACK by calling onBackPressed(), though the behavior varies based on the application compatibility mode: for Build.VERSION_CODES.ECLAIR or later applications, it will set up the dispatch to call onKeyUp(int, KeyEvent) where the action will be performed; for earlier applications, it will perform the action immediately in on-down, as those versions of the platform behaved. This implementation will also take care of KeyEvent.KEYCODE_ESCAPE by finishing the activity if it would be closed by touching outside of it. Other additional default key handling may be performed if configured with setDefaultKeyMode(int).

See also:

Android reference for

    <pre><code class="lang-csharp">android.app.Activity.onKeyDown</code></pre>

    .</a>
</format>

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

OnNewIntent(Intent?)

This is called for activities that set launchMode to "singleTop" in their package, or if a client used the Intent#FLAG_ACTIVITY_SINGLE_TOP flag when calling #startActivity.

protected override void OnNewIntent(Intent? intent)

Parameters

intent Intent

The new intent that was used to start the activity

Remarks

This is called for activities that set launchMode to "singleTop" in their package, or if a client used the Intent.FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity(Intent). In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it. An activity can never receive a new intent in the resumed state. You can count on onResume() being called after this method, though not necessarily immediately after the completion of this callback. If the activity was resumed, it will be paused and new intent will be delivered, followed by onResume(). If the activity wasn't in the resumed state, then new intent can be delivered immediately, with onResume() called sometime later when activity becomes active again. Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent.

See also:

Android reference for

    <pre><code class="lang-csharp">android.app.Activity.onNewIntent</code></pre>

    .</a>
</format>

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

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.

protected override void OnPause()

Remarks

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.

See also:

Android reference for

    <pre><code class="lang-csharp">android.app.Activity.onPause</code></pre>

    .</a>
</format>

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

OnRequestPermissionsResult(int, string[], Permission[])

Callback for the result from requesting permissions.

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)

Parameters

requestCode int

The request code passed in #requestPermissions.

permissions string[]

The requested permissions. Never null.

grantResults Permission[]

The grant results for the corresponding permissions which is either android.content.pm.PackageManager#PERMISSION_GRANTED or android.content.pm.PackageManager#PERMISSION_DENIED. Never null.

Remarks

Callback for the result from requesting permissions. This method is invoked for every call on requestPermissions(String, int) Note: It is possible that the permissions request interaction with the user is interrupted. In this case you will receive empty permissions and results arrays which should be treated as a cancellation.

See also:

Android reference for

    <pre><code class="lang-csharp">android.app.Activity.onRequestPermissionsResult</code></pre>

    .</a>
</format>

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

OnResume()

Called after #onRestoreInstanceState, #onRestart, or #onPause.

protected override void OnResume()

Remarks

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.

See also:

Android reference for

    <pre><code class="lang-csharp">android.app.Activity.onResume</code></pre>

    .</a>
</format>

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Events

ActivityResultEvent

Occurs when an activity this one started returns its result.

public event EventHandler<ActivityResultEventArgs>? ActivityResultEvent

Event Type

EventHandler<ActivityResultEventArgs>

ConfigurationChangedEvent

Occurs when the device's configuration has changed, with the new one.

public event EventHandler<Configuration>? ConfigurationChangedEvent

Event Type

EventHandler<Configuration>

CreateEvent

Occurs when the activity is created.

public event EventHandler<CreateEventArgs>? CreateEvent

Event Type

EventHandler<CreateEventArgs>

DestroyEvent

Occurs when the activity is about to be destroyed.

public event EventHandler? DestroyEvent

Event Type

EventHandler

KeyDownEvent

Occurs when a key is pressed, before the activity handles it.

public event EventHandler<KeyDownArgs>? KeyDownEvent

Event Type

EventHandler<KeyDownArgs>

NewIntentEvent

Occurs when the activity, already running, is started again with a new intent.

public event EventHandler<NewIntentEventArgs>? NewIntentEvent

Event Type

EventHandler<NewIntentEventArgs>

PauseEvent

Occurs when the activity is paused.

public event EventHandler? PauseEvent

Event Type

EventHandler

RequestPermissionsResultEvent

Occurs when the person has answered a request for permissions.

public event EventHandler<RequestPermissionsResultArgs>? RequestPermissionsResultEvent

Event Type

EventHandler<RequestPermissionsResultArgs>

ResumeEvent

Occurs when the activity is resumed.

public event EventHandler? ResumeEvent

Event Type

EventHandler