androider
androider

Reputation: 41

call activity in another package

I've been reading through some of the other posts about calling an activity in another package and it looks like I'm doing it right but still getting this error from my main activity:

AndroidTestProj1/src/com/testing/androidtest/TestProj1Activity.java:7: error: package com.testing.androidtest2 does not exist

I've declared the other activity in my manifest but it still cannot find it.

====================================

Here's AndroidTestProj1: AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.testing.androidtest"
  android:versionCode="1"
  android:versionName="1.0">
<application android:label="@string/app_name" >
   <activity android:name="TestProj1Activity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
<activity
        android:name="com.testing.androidtest2.TestProj2Activity"
        android:label="@string/app2_name" >
    </activity>
</application>

and AndroidTestProj1/src/com/testing/androidtest/TestProj1Activity.java:

    package com.testing.androidtest;

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;

import com.testing.androidtest2.TestProj2Activity;

public class TestProj1Activity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    // do some stuff here then call other package activity

    Intent i = new Intent(this, TestProj2Activity.class);
    startActivity(i);
    }
}

Here's AndroidTestProj2: AndroidManifest.xml:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.testing.androidtest2"
  android:versionCode="1"
  android:versionName="1.0">
<application android:label="@string/app_name" >
    <activity android:name="TestProj2Activity"
              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>

and AndroidTestProj2/src/com/testing/androidtest2/TestProj2Activity.java:

package com.testing.androidtest2;

import android.app.Activity;
import android.os.Bundle;

import com.testing.androidtest2.Helper;

public class TestProj2Activity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    private boolean somekeyvalue = Helper.getSomeKeyValue();

    // do stuff with keyvalue

}

And AndroidTestProj2/src/com/testing/androidtest2/Helper.java:

package com.testing.androidtest2;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class Helper {

    private static final String SOMEKEY = "somekey";

    private static SharedPreferences prefs;

        public static void init(Context context)
        {
                prefs = PreferenceManager.getDefaultSharedPreferences(context);
                initialize();
        }

        private static void initialize()
        {
                SharedPreferences.Editor editor = prefs.edit();

                if (!prefs.contains(SOMEKEY)) editor.putBoolean(SOMEKEY, false);

                editor.commit();
        }

        public static boolean getSomeKeyValue()
        {
                return prefs.getBoolean(SOMEKEY, true);
        }

}

Upvotes: 1

Views: 3582

Answers (2)

Rohan Kadu
Rohan Kadu

Reputation: 1399

It is the same thing which you do in normal scenario but with a little tweak in it. In FirstActivity where ever you want to call other activity from other package, put following code

code

Intent i = new Intent(); 

i.setClassName("com.CodeArt.finalactivity", "com.CodeArt.finalactivity.FinalActivity");

startActivity(i);

Here com.CodeArt.finalactivity is the package name and com.CodeArt.finalactivity.FinalActivity is full class name.

Now go to the AndroidManifest.xml of the FirstActivity and Add following line activity android:name=”com.CodeArt.finalactivity.FinalActivity” in application tag.

It will work fine.

Upvotes: 1

yahya
yahya

Reputation: 4860

Just an assumption, but have you already add your second project into your current one? Otherwise you cannot reach out its class even though you declare them in your manifest. To know how to achieve that you can check here

Upvotes: 0

Related Questions