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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:38:39+00:00 2026-06-01T18:38:39+00:00

I want to have a helper classes such as Convert or Utils in my

  • 0

I want to have a helper classes such as Convert or Utils in my android app. Typical issue I have is:

public class Convert {

    private Convert() {
        // I can't be instantiated
    }

    public static int pxToDp(float pixels) {
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, pixels, displayMetrics);
    }
}

The above fails of course because Convert has no idea what getResources() is.
So that leaves me two options:

  1. pass a context each time I want to use a helper method, which is rubbish
  2. subclass Application, modify my manifest, then create a reference, like this:

    public class App extends Application {
    
        private static Context mContext;
    
        @Override
        public void onCreate() {
            super.onCreate();
            mContext = this;
        }
    
        public static Context getContext() {
            return mContext;
        }
    }
    

then do App.getContext() everywhere I need it.

Question: this can’t be right, what’s the elegant way to proceed?

  • 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-06-01T18:38:40+00:00Added an answer on June 1, 2026 at 6:38 pm

    If you subllcass Application that will obviously work, but you may end up with an enormous, horrible class that does a whole bunch of stuff that’s unrelated – walking roughshod over the Single Responsibility Principle.

    Yes it’s a bit messy and annoying passing a Context into a utility class, but it means you can create classes for each type of utility (e.g. StringUtils, or whatever) and at least keep the responsibilities separate.

    I’d advise against using App.getContext everywhere as it means all your utility classes will be dependent on App, which makes it hard to reuse them. If you pass in a context, you can more easily reuse some of the utility classes in other apps.

    So I’d say use separate classes, and pass that Context in to the methods. It’s a little ugly, but in my opinion much less ugly than the alternatives.

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

Sidebar

Related Questions

Assume we have legacy classes, that can't be modified: class Foo { public void
I have a REST service and I want to have a helper class that
I have couple of helper methods added to the existing classes, and i want
I have some helper classes that I don't want in the global namespace, so
want to have a Hyperlink-Button in a gridView in which I can display a
I want to have a text box that the user can type in that
I want to have a class which implements an interface, which specifies the specific
I have a number of classes that look like this: class Foo(val:BasicData) extends Bar(val)
Say I have four sub-classes of 'Car'. One for each color. I want to
i have helper C functions in some Objective C classes. Just found out that

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.