I want to pass a XMPPTCPConnection object with intent.putExtra(...);
but unfortunately it didn't work with casting my object to Serialize
or Parcelable
like: signInIntent.putExtra("connection", (Serialize) conn);
and reporting the error : "can not cast XMPPTCPConnection
to Serialize
or Parcelable
."
i see many similar questions and try to do like their answers.
so i tried to add the object to a list and then pass that list.
this is my first Activity :
Intent signInIntent = new Intent(LoginActivity.this, MainActivity.class);
ArrayList<XMPPTCPConnection> conn = new ArrayList<XMPPTCPConnection>();
conn.add(connection);
signInIntent.putExtra("connection", conn);
startActivity(signInIntent);
and this is my second Activity :
Bundle getData = getIntent().getExtras();
List<XMPPTCPConnection> listConn = new ArrayList<>();
listConn = (List<XMPPTCPConnection>) getData.get("connection");
XMPPTCPConnection connection = listConn.get(0);
but this error reported :
12-27 18:48:42.919 30358-30358/finalproject.ffisher.com.finalproject E/AndroidRuntime: FATAL EXCEPTION: main
12-27 18:48:42.919 30358-30358/finalproject.ffisher.com.finalproject E/AndroidRuntime: Process: finalproject.ffisher.com.finalproject, PID: 30358
12-27 18:48:42.919 30358-30358/finalproject.ffisher.com.finalproject E/AndroidRuntime: java.lang.RuntimeException: Parcel: unable to marshal value org.jivesoftware.smack.tcp.XMPPTCPConnection@3f8f8920
am I wrong in this code ? is there any other way to pass data between activities ? please help me. Thank you.
I'd suggest to keep your XMPPTCPConnection
object in in Application
's extension, as it's accessible from any place within the app.
public class MyApplication extends Application {
private XMPPTCPConnection connection;
@Override
public void onCreate() {
super.onCreate();
}
public void initializeXMPPTCPConnection() {
XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
.setServiceName(SERVER_HOST)
.setHost(SERVER_ADDRESS)
.setPort(PORT)
.setCompressionEnabled(false)
.setResource("test")
.setDebuggerEnabled(true)
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
.build();
connection = new XMPPTCPConnection(config);
}
public XMPPTCPConnection getXMPPTCPConnection() {
return connection;
}
}
And in AndroidManifest
add reference to it:
<application
android:name=".MyApplication" />
Then you can call ((MyApplication)getApplication()).initializeXMPPTCPConnection();
or ((MyApplication)getApplication()).getXMPPTCPConnection();
from any activity you want