Passing instances of classes between activities

If I have class A and class B, class A acts like a menu with 2 buttons to connect to login. When I click connect, I run this method:

    private void connect(){
    Thread t1 = new Thread(){
        public void run(){
    connection_class = new ConnectionClass();;


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 fine with server and pushes login which does (no content onClick):

        Intent i = new Intent().setClass(v.getContext(), Class.class);          


This works great, but when im in class B I want to use the same instance. I could just create a new thread and an instance of the class, but that might defeat the purpose of the Start menu and require me to log back in.

Is there any way to pass an instance as an activity parameter on startup, or some way to its android?

As a side item, I DO NOT NEED the menu, but I did get some free time before the task was scheduled and thought I might try it.


source to share

2 answers

I just finished a project like yesterday.

For example, you have this connection manager called WebService


// singleton class
public class WebService {

    private static WebService instance;

    private WebService() {}

    public static WebService getInstance() {
        if (instance == null)
            instance = new WebService();
        return instance;
    }// getInstance()

    public void login() {};
    public void getFeeds() {};
    public void logout() {};


Then you can put it in a basic operation like this:

public class WebServiceActivity extends Activity {

    private final WebService fWebService = WebService.getInstance();

    protected WebService ws() { return fWebService; }


Then you have two actions: LoginActivity

and WorkingActivity


public class LoginActivity extends WebServiceActivity {

    buttonLogin.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            try {

                // start WorkingActivity if logging in ok
            } catch (...) { ... }

public class WorkingActivity extends WebServiceActivity {

    buttonGetFeeds.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

    buttonLogout.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {


Anyway, there are many approaches. One of them is mine. Hope this helps you :-)



I'm still not sure if this is correct or not. But I prefer to use a static instance of the class like this:

// Create this class just once
public class MediaManager {
    public static MediaManager instance;

    public MediaManager() {
        instance = this;

    public void addNewImage(Bitmap bitmap) {



public class AnotherClass {
      public void doSomething() {


If anyone knows a better way to use the manager classes, please comment.



All Articles