How correctly to inherit, change and start activity from aar?


#1

I have an aar android plug-in with my Activity.
com.example.unity_player.UnityPlayerActivity
I want to override onCreate through a proxy.

import {AndroidActivityCallbacks, setActivityCallbacks} from 'tns-core-modules/ui/frame';
@JavaProxy('com.example.unity_player.UnityPlayerActivity')
export class Activity extends android.app.Activity {
    private _callbacks: AndroidActivityCallbacks;
    protected onCreate(savedInstanceState: android.os.Bundle): void {
        if (!this._callbacks) {
            setActivityCallbacks(this);
        }
        console.log('Unity start!');
        this._callbacks.onCreate(this, savedInstanceState, super.onCreate);
    }

And then run the overridden Activity

import { Activity } from '../unity/activity';
...
    runUnity(): void{
        this.appContext = androidApp.context;
        const intent: android.content.Intent = new android.content.Intent(
            application.android.foregroundActivity,
            Activity['class'],
        );
        intent.setFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
        application.android.foregroundActivity.startActivity(intent);
    }

But when you start the unit in the console, or nothing. Running the source Activity.
How correctly to inherit, change and start activity from aar?


#2

Did you register your activity in manifest file? Also I don’t think Activity['class'] gives you the class, you might have to pass it as com.example.unity_player.UnityPlayerActivity (which should be available at run time), to avoid typescript errors just use declare var com; at top of the file.


#3
runUnity(): void{
        this.appContext = androidApp.context;
        const intent: android.content.Intent = new android.content.Intent(
            application.android.foregroundActivity,
            com.example.unity_player.UnityPlayerActivity['class'],
        );
        intent.setFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
        application.android.foregroundActivity.startActivity(intent);
    }

This option also starts the Activity from aar, and not overridden.

AndroidManifest.xml

<application
		android:name="com.tns.NativeScriptApplication"
		android:debuggable="true"
		android:networkSecurityConfig="@xml/network_security_config"
		android:allowBackup="true"
		android:icon="@drawable/icon"
		android:label="@string/app_name"
		android:theme="@style/AppTheme"
		android:hardwareAccelerated="true"
		android:largeHeap="true">

		<activity
			android:name="com.tns.NativeScriptActivity"
			android:label="@string/title_activity_kimera"
			android:configChanges="keyboardHidden|orientation|screenSize"
			android:theme="@style/LaunchScreenTheme">

			<meta-data android:name="SET_THEME_ON_LAUNCH" android:resource="@style/AppTheme" />

			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>
		<activity android:name="com.tns.ErrorReportActivity"/>
		<activity android:name="com.example.unity_player.UnityPlayerActivity"/>
	</application>

#4

What you mean by overridde unity activity, I don’t see that you are actually overriding / extending anything from your aar but just the android activity.

If you want to override something replace extends android.app.Activity with the right activity name from your aar.


#5

I want to add my code to UnityPlayerActivity in onCreate


#6

I’m not sure what you need exactly, as I see in your code you are one creating UnityPlayerActivity so why you would want to override it again?


#7

Java code from aar.

public class UnityPlayerActivity extends Activity
{
    protected UnityPlayer mUnityPlayer; 

    // Setup activity layout
    @Override protected void onCreate(Bundle savedInstanceState)
    {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);

        mUnityPlayer = new UnityPlayer(this);
        setContentView(mUnityPlayer);
        mUnityPlayer.requestFocus();
    }

I need to add lines to the end of the onCreate method.

String baseUrl = this.getIntent().getStringExtra("LINK");
mUnityPlayer.UnitySendMessage('Base', 'SetUrl', baseUrl)

But already through Nativescript.


#8

So back to my previous answer again, you suppose to extend the activity like

@JavaProxy('com.example.unity_player.UnityPlayerActivity')
export class Activity extends com.whatever.package.name.UnityPlayerActivity {
    private _callbacks: AndroidActivityCallbacks;
    protected onCreate(savedInstanceState: android.os.Bundle): void {
        if (!this._callbacks) {
            setActivityCallbacks(this);
        }
        console.log('Unity start!');
        this._callbacks.onCreate(this, savedInstanceState, super.onCreate);
    }

Whatever you are passing inside JavaProxy decorator (here com.example.unity_player.UnityPlayerActivity) should not be the class name you want to extend / override. It should be the new unique name of your new class.


#9

Also, setActivityCallbacks / this._callbacks.onCreate are valid only if you extend NativeScriptActivity, those will be invalid here. You can just call super.onCreate() which will execute whatever is there in the unity activity and below you can write whatever you want to execute.


#10

Can you give an example?


#11

Thank you! Understood.