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 7655675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:34:24+00:00 2026-05-31T12:34:24+00:00

I am trying to keep my code tidy and move most of the functionality

  • 0

I am trying to keep my code tidy and move most of the functionality to external classes where possible so I can re-use it easily.

I have a contact picker which I am currently using and want to move it into its own class to aid reusability. Is it possible and if so how do I move the bottom two methods out into their own class?

public class mainDashView extends Activity {


    public static final int PICK_CONTACT = 1;


    /**
     * @see android.app.Activity#onCreate(Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dashboard);

    showContactSelector();
    }


    // How do I move the bottom two Methods into an external class?
    private void showContactSelector() {


            Intent contactIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
            startActivityForResult(contactIntent, PICK_CONTACT);


    }


    @Override
    protected void onActivityResult(int reqCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(reqCode, resultCode, data);

        switch (reqCode) {
        case (PICK_CONTACT) :
          if (resultCode == Activity.RESULT_OK) {
            Uri contactData = data.getData();
            Cursor c =  managedQuery(contactData, null, null, null, null);
            if (c.moveToFirst()) {
              String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
              // TODO Whatever you want to do with the selected contact name.
            }
          }
          break;
    }

}
}
  • 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-31T12:34:26+00:00Added an answer on May 31, 2026 at 12:34 pm

    like so:

    public class mainDashView extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.dashboard);
            Something.showContactSelector(this);
        }
    
        @Override
        protected void onActivityResult(int reqCode, int resultCode, Intent data) {
            super.onActivityResult(reqCode, resultCode, data);
            Something.onActivityResult(this, reqCode, resultCode, data);
        }
    }
    
    class Something {
        private static final int PICK_CONTACT = 1;
    
        public static void showContactSelector(Activity activity) {
            Intent contactIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
            activity.startActivityForResult(contactIntent, PICK_CONTACT);
        }
    
        public static void onActivityResult(Activity activity, int reqCode, int resultCode, Intent data) {
            switch (reqCode) {
                case (PICK_CONTACT):
                    if (resultCode == Activity.RESULT_OK) {
                        Uri contactData = data.getData();
                        Cursor c = activity.managedQuery(contactData, null, null, null, null);
                        if (c.moveToFirst()) {
                            String name = c.getString(c
                                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                            // TODO Whatever you want to do with the selected
                            // contact name.
                        }
                    }
                    break;
            }
        }
    }
    

    You need to pass the calling Activity for some of the methods to work. For example within your Activity class you can call startActivityForResult(contactIntent, PICK_CONTACT); directly but that is only a shortened version of this.startActivityForResult(contactIntent, PICK_CONTACT);. In a different class you need to replace the invisible this by a reference to the class.

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

Sidebar

Related Questions

I am trying to keep my code as secure/clean as possible. If I have
Ever since reading Clean Code I have been trying to keep my code descriptive
When developing, I can use SVN or Git to keep code in sync between
Trying to keep my C# code optimized, I found that if I have a
I have the following code. I am trying to keep track of selected items
I'm trying to keep my code clean and keep the number of files down.
Im really trying to keep my code clean by using the switch statement but
I'm trying to adopt some best practices to keep my python code efficient. I've
I'm trying to learn about this feature of javascript I keep seeing in code,
I'm trying to code some stuff for a game but I keep getting error

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.