Android - save/restore state of custom class
Posted
by
user1209216
on Stack Overflow
See other posts from Stack Overflow
or by user1209216
Published on 2014-05-27T21:20:05Z
Indexed on
2014/05/27
21:26 UTC
Read the original article
Hit count: 181
I have some class for ssh support - it uses jsch internally. I use this class on main activity, this way:
public class MainActivity extends Activity
{
SshSupport ssh = new SshSupport();
.....
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//Handle events for ssh
ssh.eventHandler = new ISshEvents()
{
@Override
public void SshCommandExecuted(SshCommandsEnum commandType, String result)
{
}
//other overrides here
}
//Ssh operations on gui item click
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3)
{
if (ssh.IsConnected() == false)
{
try
{
ssh.ConnectAsync(/*parameters*/);
}
catch (Exception e)
{
e.printStackTrace();
}
}
try
{
ssh.ExecuteCommandAsync(SshCommandsEnum.values()[position]);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
It works very well. My application connects to ssh, performs all needed operation in background thread and results are reported to gui, via events as shown above. But nothing works after user change device orientation. It's clear for me - activity is re-created and all state is lost. Unfortunately, my SshSupport class object is lost as well.
It's pretty easy to store gui state for dynamically changed/added objects (using put/get serializable etc methods). But I have no idea how to prevent my ssh object, ssh connected session being lost.
Since my class is not serializable, I can't save it to bundle. Also, even if I make my SshSupport class serializable, jsch objects it uses still are not serializable.
So what is the best way to solve this?
© Stack Overflow or respective owner