RiddlerDev
RiddlerDev

Reputation: 7439

Cannot Find MainActivity for Intent Inside Cordova PlugIn

I am writing my own cordova plugin for Android. I have the plugin working inside a standalone Android project. I cannot seem to get my activity to start though from the plugin java file. It keeps firing errors that it cannot find MainActivity (cannot find symbol). I am hoping it is an easy fix that I seem to be missing.

Android Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myapp.testplugin" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:windowSoftInputMode="adjustResize"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>

Plug-In Java:

package com.myapp.testplugin;

import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaInterface;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.provider.Settings;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class TestPlugin extends CordovaPlugin {
    public static final String TAG = "Test Plugin";
    /**
    * Constructor.
    */
    public TestPlugin() {}
    /**
    * Sets the context of the Command. This can then be used to do things like
    * get file paths associated with the Activity.
    *
    * @param cordova The context of the main Activity.
    * @param webView The CordovaWebView Cordova is running in.
    */
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);
        Log.d(TAG,"Init TestPlugin");
    }

    @Override
    public boolean execute(final String action, JSONArray args, CallbackContext callbackContext) 
    throws JSONException {
        cordova.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Context context = cordova.getActivity().getApplicationContext();
                Intent intent = new Intent(context, MainActivity.class);
                cordova.getActivity().startActivity(intent);
            }
        });

        return true;
    }
}

Error is occuring here:

Intent intent = new Intent(context, MainActivity.class);

Upvotes: 2

Views: 3552

Answers (1)

Dali Tlili
Dali Tlili

Reputation: 66

I solved it this way:

    Class mainActivity;
    Context context = getApplicationContext();
    String  packageName = context.getPackageName();
    Intent  launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
    String  className = launchIntent.getComponent().getClassName();

    try {
        //loading the Main Activity to not import it in the plugin
        mainActivity = Class.forName(className);
    } catch (Exception e) {
        e.printStackTrace();
    }

    Intent openActivityIntent = new Intent(context, mainActivity);

Upvotes: 5

Related Questions