If I got class A and class B, class A act as a menu with 2 buttons one to connect one to login. When I press connect i run this method:
private void connect(){
Thread t1 = new Thread(){
public void run(){
connection_class = new ConnectionClass();
connection_class.run();
}
};t1.start();
}
which calls my ConnectionClass which does this in the constructor:
public ConnectionClass(){
socket = new Socket("address", port);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
}
works great im connected to the server and press login which does (without the onClick stuff):
connection_class.MethodToWriteToServer("CommandThatLogsMeIn");
Intent i = new Intent().setClass(v.getContext(), Class.class);
startActivity(i);
This works fine but when im in Class B I want to use the same instance of it. I could just do a new thread and instance of the class but that would defeat the purpose of the start menu and require me to log in once more.
Is it somehow possible to pass the instance as a parameter to the activity when starting it or whats the android way of doing it?
As a sidenote I dont really NEED the menu but ive got some spare time before the assignment is due and thought I might aswell try it.
I have just finished a project like this yesterday.
For example you have this connection manager, called
WebService:Then you can put it in an base activity like this:
Then, you have two activities,
LoginActivityandWorkingActivity:Anyway, there are many approaches. The one above is mine. Hope it helps you 🙂