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?