Skip to content

Activity

levching edited this page Apr 15, 2020 · 3 revisions

An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI.

With Android Native plugin you may interact with Android Activites. Frist of all you can refer to the main / launcher activity. For example:

using SA.Android.App;
using SA.Android.Content;
...

AN_Intent sendIntent = new AN_Intent();
sendIntent.SetAction(AN_Intent.ACTION_SEND);
sendIntent.PutExtra(AN_Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.SetType("text/plain");
AN_MainActivity.Instance.StartActivity(sendIntent);

Or you may use AN_ProxyActivity activity, to create a new activity on top of the Unity launcher Activity. And for example, to start for result some AN_Intent. See the example.

using SA.Android.App;
using SA.Android.Content;
...

AN_Intent sendIntent = new AN_Intent();
sendIntent.SetAction(AN_Intent.ACTION_SEND);
sendIntent.PutExtra(AN_Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.SetType("text/plain");

// Instaed AN_MainActivity
// AN_MainActivity.Instance.StartActivity(sendIntent);

AN_ProxyActivity proxy = new AN_ProxyActivity();
proxy.StartActivityForResult(sendIntent, (result) => {
    Debug.Log("Unity: Activity Result: " + result.ResultCode.ToString());
    proxy.Finish();
});

The AN_Activity class is base on Google Activity object. Let us know if you need more API related to Activity. Feel free to contact us at https://stansassets.com/

Available Activity methods

Finish

Call this when your activity is done and should be closed.
The ActivityResult is propagated back to whoever launched the activity.

MoveTaskToBack

Move the task containing this activity to the back of the activity stack. The activity's order within the task is unchanged

nonRoot param: If false then this only works if the activity is the root of a task; if true it will work for any activity in a task.

Resturns: If the task was moved (or it was already at the back) true is returned, else false.

And here is a small example how to Send Application to background:

using SA.Android.App;
...

AN_MainActivity.Instance.MoveTaskToBack(true);
Clone this wiki locally