I’m trying to pass a java.util.Stack object from an Activity to another in an android app.
The way I do it is to use the fact that the Stack class implements the Serializable interface and pass the object via an Intent object.
The strange thing is that when I use getSerializableExtra(...) in my other Activity, the class of the object has changed from Stack to ArrayList.
I did not find anything about that on the internet.
Do you have any idea of the reason it behaves like that ?
Thanks
Here is a simple code that reproduce the thing (I tried on android 2.3.3 and 4.0.3):
Class A:
package my.test;
import java.util.Stack;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class A extends Activity {
Stack<Integer> stack;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
stack = new Stack<Integer>();
stack.push(42);
stack.push(7);
Log.i("Class of stack in A (1)",stack.getClass().getName());
Intent i = new Intent(this,B.class);
i.putExtra("stack", stack);
Log.i("Class of stack in A (2)",i.getSerializableExtra("stack").getClass().getName());
startActivity(i);
}
}
Class B:
package my.test;
import java.io.Serializable;
import java.util.Stack;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class B extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i = getIntent();
Serializable s = i.getSerializableExtra("stack");
Log.i("class of stack in B",s.getClass().getName());
Stack<Integer> stack = (Stack<Integer>) s;
}
}
Output of Log.i(...):
04-20 15:36:56.596: I/Class of stack in A (1)(748): java.util.Stack
04-20 15:36:56.596: I/Class of stack in A (2)(748): java.util.Stack
04-20 15:36:56.736: I/class of stack in B(748): java.util.ArrayList
Thanks Aidanc. It seems that it’s triggering exactly this bug : http://code.google.com/p/android/issues/detail?id=3847