Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7608231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:51:51+00:00 2026-05-31T00:51:51+00:00

If I got class A and class B, class A act as a menu

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T00:51:52+00:00Added an answer on May 31, 2026 at 12:51 am

    I have just finished a project like this 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 an base activity like this:

    public class WebServiceActivity extends Activity {
    
        private final WebService fWebService = WebService.getInstance();
    
        protected WebService ws() { return fWebService; }
    }
    

    Then, you have two activities, 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. The one above is mine. Hope it helps you 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a class somewhat like this: public class Test { private final List<ISomeType>
In a WPF application, I've got my events centralized in one class like this:
I have got class declared like this: internal private abstract class BoxGroup<TS> : IBoxGroup
I've got a class named BackgroundWorker that has a thread constantly running. To turn
Say I've got a class like this: class Test { int x; SomeClass s;
I've got a class that looks something like this: public class Parent { public
I've got a class like this: [XmlRoot(channel)] public class Channel { [XmlElement(title)] public String
I got class with template methods that looks at this: struct undefined {}; template<typename
Say I got: class X_ { public: void do() { } } class Y_
Let's say I've got class: class Bad_Date { private: const char* _my_msg; public: const

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.