Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error while extending the main activity #1662

Open
dpdragnev opened this issue Apr 14, 2021 · 4 comments
Open

Error while extending the main activity #1662

dpdragnev opened this issue Apr 14, 2021 · 4 comments

Comments

@dpdragnev
Copy link

I am using NS 6.5 Core and I am following the example from https://v6.docs.nativescript.org/core-concepts/android-runtime/advanced-topics/extend-application-activity#extending-activity.

I have cleaned up my project, deleting the platforms, hooks, and node_modules folders. When I build the app, I get the following error: Class "org.myApp.MainActivity" not found.

Here is my code:

activity.android.ts:

import { setActivityCallbacks, AndroidActivityCallbacks } from "tns-core-modules/ui/frame";

@JavaProxy("org.myApp.MainActivity")
class Activity extends androidx.appcompat.app.AppCompatActivity {
    public isNativeScriptActivity;

    private _callbacks: AndroidActivityCallbacks;

    public onCreate(savedInstanceState: android.os.Bundle): void {
        // Set the isNativeScriptActivity in onCreate (as done in the original NativeScript activity code)
        // The JS constructor might not be called because the activity is created from Android.
        this.isNativeScriptActivity = true;
        if (!this._callbacks) {
            setActivityCallbacks(this);
        }

        this._callbacks.onCreate(this, savedInstanceState, this.getIntent(), super.onCreate);
    }

    public onSaveInstanceState(outState: android.os.Bundle): void {
        this._callbacks.onSaveInstanceState(this, outState, super.onSaveInstanceState);
    }

    public onStart(): void {
        this._callbacks.onStart(this, super.onStart);
    }

    public onStop(): void {
        this._callbacks.onStop(this, super.onStop);
    }

    public onDestroy(): void {
        this._callbacks.onDestroy(this, super.onDestroy);
    }

    public onBackPressed(): void {
        this._callbacks.onBackPressed(this, super.onBackPressed);
    }

    public onRequestPermissionsResult(requestCode: number, permissions: Array<string>, grantResults: Array<number>): void {
        this._callbacks.onRequestPermissionsResult(this, requestCode, permissions, grantResults, undefined /*TODO: Enable if needed*/);
    }

    public onActivityResult(requestCode: number, resultCode: number, data: android.content.Intent): void {
        this._callbacks.onActivityResult(this, requestCode, resultCode, data, super.onActivityResult);
    }
}

webpack.config.js:

const appComponents = env.appComponents || [];
    appComponents.push(...[
        "tns-core-modules/ui/frame",
        "tns-core-modules/ui/frame/activity",
        resolve(__dirname, "app/activity.android.ts"),
    ]);

AndroidManifest.xml:

<activity android:name="org.myApp.MainActivity" android:label="@string/title_activity_kimera" android:configChanges="keyboard|keyboardHidden|orientation|screenSize|smallestScreenSize|screenLayout|locale|uiMode" android:theme="@style/LaunchScreenTheme">
...
</activity>
<activity android:name="com.tns.ErrorReportActivity" />

Any suggestions on how to proceed?

@m59peacemaker
Copy link

I have the same problem. The v7 docs also seem to be incorrect / outdated.

@m59peacemaker
Copy link

tns clean was the main solution for me. It seems like there are other issues I needed to workaround.

Here is my webpack.config.js:

const webpack = require('@nativescript/webpack')
const { resolve } = require('node:path')

module.exports = env => {
	env.appComponents = [
		'@nativescript/core/ui/frame',
		'@nativescript/core/ui/frame/activity',
		resolve(__dirname, 'app/activity.android')
	]

	webpack.init(env)

	// Learn how to customize:
	// https://docs.nativescript.org/webpack

	const config = webpack.resolveConfig()

	config.entry = {
		...config.entry,
		application: resolve(__dirname, 'app/application.android')
	}

	return config
}

NativeClass() should possibly be added above the @JavaProxy line in the activity and application .android.ts files.

And there were some type issues with the activity.android.ts code in the docs. Here are the changes I made:

    public isNativeScriptActivity: boolean = false;

    private _callbacks!: AndroidActivityCallbacks;

        // I changed `undefined` to `() => {}` here
        this._callbacks.onRequestPermissionsResult(this, requestCode, permissions, grantResults, () => {}/*TODO: Enable if needed*/);

@m59peacemaker
Copy link

Not sure if you are looking at the latest docs, but this is documented here: https://docs.nativescript.org/guide/extending-classes-and-implementing-interfaces-android#extending-android-application

and here: https://docs.nativescript.org/guide/extending-classes-and-implementing-interfaces-android#extending-android-activity

You're right! It seems like I was looking at old docs after all. I thought I had specifically checked this and clicked to view the latest, but I must have messed it up somehow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants