1

I have created 2 android applications (Application A and Application B) that share same process and user ID. I have defined a Generic Activity in both the applications. The name space and class name of GenericActivity is same in both the applications but the content is different.

So Application A has

package com.company.ui;
public class GenericActivity extends Activity{

//some content

}

and Application B has

package com.company.ui;
public class GenericActivity extends Activity{

//some different content

}

First I launch Application A and then application B is launched from application A. I know that since the classloaders of both the applications are different, when both the applications are launched, then there will be 2 copies of GenericActivity class one associated with each classloader which in turn is associated with each application. So far so good.

Now I background both the app and I forcibly kill Application A. Now I relaunch my application A. In my application A I have a fragment which has a code like this

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = (GenericActivity) getActivity();
}

Now here is the problem. I get a class cast exception here. After debugging I found out that it is trying to cast it to GenericActivity of application B and since it is different class loaded by different class loader, it gives class cast exception. My understanding is that when I forcibly killed Application A and relaunch it, then it should again load its copy of GenericActivity through its classloader. Why is it trying to use the GenericActivity of Application B?

Ankit Mittal
  • 179
  • 5
  • Please share your observation why its loading the Activity of Application B. Curiously waiting for your response. – Roll no1 Sep 09 '14 at 13:35
  • Thats what I want to know... Why is it using the activity of Application B instead of launching its own activity. – Ankit Mittal Sep 09 '14 at 13:57
  • It seems to me that you´ve got kind of mess of imports or dependencies not noticeable in compile time but they´re killing your app as bytecode executes. Looks like the code is expecting the class it was compiled with (there you should look for the error) and gets the other. – eduyayo Sep 09 '14 at 14:13
  • btw... are you running from eclipse? maybe it´s a thing with importing a same lib project into two different applications?? – eduyayo Sep 09 '14 at 14:14
  • @eduyayo The namespace and class name for both the files are same. so there is no question of imports creating mess. And as mentioned in my question, it is me who is manually killing one of the app. Before killing the app, both the apps work fine and have their own copy of GenericActivity. I am using android studio with gradle plugin. – Ankit Mittal Sep 09 '14 at 14:25

0 Answers0