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

  • Home
  • SEARCH
  • 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 6743623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:59:00+00:00 2026-05-26T11:59:00+00:00

Since Android introduced library projects, I’ve been converting my app into a library so

  • 0

Since Android introduced library projects, I’ve been converting my app into a library so that I can make several versions with appropriate tweaks (for example, a free and pro version using the same code base, but changing a few things).

I initially had trouble allowing the library project’s code access to fields in my sub-projects. In other words, my free and pro versions each had a class with a few constants in them, which the library project would use to distinguish certain features.

In the sub-project, I extended the library’s main activity and added a static initialisation block which uses reflection to change the values of fields in the library.

public class MyMainActivityProVersion extends MyMainActivity {

    public static final String TAG = Constants.APP_NAME + "/SubClass";

    static {
        try {
            ConstantsHelper.setConstants(Constants.class);
        } catch (Exception e) {
            Log.d(TAG, "--- Constants not initialised! ---");
            e.printStackTrace();
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}

In this code, ConstantsHelper is in the library, and I am providing Constants.class from my sub-project. This initialises the constants in the library project.

My approach works great, except for one particular use case. When the app hasn’t been used in a while and it is ‘stopped’ by the OS, the static fields in ConstantsHelper are forgotten.

The constants are supposed to be reset by the main activity (as shown above), but the main activity isn’t even launched because the OS resumes a different activity. The result of this is that the initialisation of the constants is forgotten and I cannot re-initialise them because the resumed activity is in the library (which has no knowledge of the sub-project).

How can I ‘tell’ other activities in the library to call code from sub-projects on resuming? Alternatively, is there a way to ensure that some code in my sub-project is called on every resume?

  • 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-26T11:59:01+00:00Added an answer on May 26, 2026 at 11:59 am

    I’m afraid I never found a good answer to this question. I’ll probably continue with my terrible use of reflection and figure out some hacky workaround.

    I felt I should come back and at least point out that I didn’t solve this for the benefit of others who come to this page.

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

Sidebar

Related Questions

For retrieving the accounts (information) in Android versions since 2.0 you can use the
So I'm making an Android 2.2 app that uses GLSurfaceView. My question is, since
We have been getting a new error in our Android app since beginning of
My app uses a method that was deprecated since Android 2.1 with a different
I am looking into using the new Backup API that available since Android 2.2,
I'm new to Android development and it's been a long time since I've done
I am new to android programming and it's been some years since I have
Since the latest android update came out, I haven't been very active with the
I've been intrigued by all the android world since I first learned about it
I'm new to publishing Android apps. Our app's name and the string that users

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.