I would like to navigate between activities and pass an object. The object is a Tcp connection between the client (the phone) and the server (a computer). When the user presses a button, the new activity is started and the user is redirected to a new page.
I want to be able to send certain commands to the server from different activities.
However, I am having issues passing the object between activities. Whenever I try to go to the next activity, I get the following error:
java.lang.RuntimeException: Parcelable encountered IOException writing serializable object Caused by: java.io.NotSerializableException: com.example.user.myapp.HomePage$ConnectTask$1
The Asynctask is throwing the error, I believe it not to be a duplicate of this question. All classes already implement Serializable.
In my onCreate
connecttask = new ConnectTask();
connecttask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
final Intent i = new Intent(getBaseContext(),Menu.class);
final ArrayList<TcpClient> testing = new ArrayList<TcpClient>();
In my setOnClickListener
public void onClick(View view) {
if(Username.getText().toString().equals("admin") &&
Password.getText().toString().equals("admin")) {
Toast.makeText(getApplicationContext(),
"Redirecting...",Toast.LENGTH_SHORT).show();
testing.add(mTcpClient);
i.putParcelableArrayListExtra("extra",testing);
startActivity(i);
}
The AsyncTask:
public class ConnectTask extends AsyncTask<String, String, TcpClient> implements Serializable {
@Override
protected TcpClient doInBackground(String... message) {
System.out.println("Executed call");
mTcpClient = new TcpClient(new TcpClient.OnMessageReceived() {
@Override
public void messageReceived(String message) {
try {
publishProgress(message);
if (message != null) {
System.out.println("Returned message from socket::::: >>>>>>" + message);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}, ipAddressOfServerDevice);
if (mTcpClient != null) {
mTcpClient.sendMessage("Initial message when connected with Socket Server");
}
delay();
return mTcpClient;
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
arrayList.add(values[0]);
mAdapter.notifyDataSetChanged();
}
}