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

        connection_class.MethodToWriteToServer("CommandThatLogsMeIn");     
        Intent i = new Intent().setClass(v.getContext(), Class.class);          
        startActivity(i);

      

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.

+3


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() {

        @Override
        public void onClick(View v) {
            try {
                ws().login();

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

public class WorkingActivity extends WebServiceActivity {

    buttonGetFeeds.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            ws().getFeeds();
        }
    });

    buttonLogout.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            ws().logout();
            finish();
        }
    });
}

      

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

+2


source


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() {
          MediaManager.instance.addNewImage(..);
      }
}

      

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

+1


source







All Articles