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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:40:20+00:00 2026-05-23T08:40:20+00:00

Background I’m trying to keep an app which is as modular as possible. The

  • 0

Background

I’m trying to keep an app which is as modular as possible.
The app will have have tasks which it performs on different intervals. My goal is to make it as easy possible to add new tasks with minimal understanding of the underlying architecture and without having to modify other files but at the same time not over complicating the code.

It would be perfect if all you needed to do to add a new task is to create the file and that’s it.
This will require loading the tasks in runtime which I don’t really like, I could live with a single place where all the registration is done (this also enabled toggling of the task)

Right now I’ve have an abstract task class which has a piece of static code which registers all tasks (basically adds them to a list).

Problem

Each task will have their own set of preferences and possibly resources.
Dividing strings and array is pretty easy by using prefixes for the names but the main issue comes with the preferences.

Right now I’m using a PreferenceActivity to display my preferences.
The general settings are loaded from an XML file. Each task’s preferences are located in a separate PreferenceScreen. All tasks have only one thing common and that is an “Enabled” checkbox.

I don’t want to store all the preferences in one file as it has the possibility to get quite messy that way.

Current solution

Right now each task have a method setupPreferences(PreferenceScreen) in which they can add whatever options they want. This however has the downside of being programmatically which is not all that bad but I’d like to avoid that if possible.

Desired solution

The optimal solution would be if each task could have their own XML file which is loaded and added to the root PreferenceScreen, as far as I know however this is not possible, the only way to load it is into a PreferenceActivity.

Other notes

If anyone has any other suggestions on dividing resources in android feel free to share them 🙂

Thanks
Nicklas


Clarification

The tasks I’m talking about are never third party, they will internal only. This is more of a way to early on get a good structure of this app.

  • 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-23T08:40:21+00:00Added an answer on May 23, 2026 at 8:40 am

    By using reflection I’m calling PreferenceManager.inflateFromResource(Context, int, PreferenceScreen) to create a PreferenceScreen from my XML files.

    String resources are separated in separate files and prefixed with taskname_

    Here is the code for inflating the PreferenceScreen, it should be placed in a PreferenceActivity:

    /**
     * Inflates a {@link android.preference.PreferenceScreen PreferenceScreen} from the specified
     * resource.<br>
     * <br>
     * The resource should come from {@code R.xml}
     * 
     * @param resId The ID of the XML file
     * @return The preference screen or null on failure.
     */
    private PreferenceScreen inflatePreferenceScreenFromResource(int resId) {
        try {
            Class<PreferenceManager> cls = PreferenceManager.class;
            Method method = cls.getDeclaredMethod("inflateFromResource", Context.class, int.class, PreferenceScreen.class);
            return (PreferenceScreen) method.invoke(getPreferenceManager(), this, resId, null);         
        } catch(Exception e) {
            Log.w(LOG_TAG, "Could not inflate preference screen from XML", e);
        }
    
        return null;
    }
    

    Here is an example of how to use it:

    package com.example;
    
    import java.lang.reflect.Method;
    
    import com.example.R;
    
    import android.os.Bundle;
    import android.preference.PreferenceActivity;
    import android.preference.PreferenceManager;
    import android.preference.PreferenceScreen;
    import android.util.Log;
    
    
    public class ExamplePreferenceActivity extends PreferenceActivity {
        public static final String PREFERENCE_NAME = "ExamplePreferences";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            // Sets the preference name
            PreferenceManager pm = getPreferenceManager();
            pm.setSharedPreferencesName(PREFERENCE_NAME);
    
            // Adds default values and the root preference screen
            PreferenceManager.setDefaultValues(this, PREFERENCE_NAME, MODE_PRIVATE, R.xml.preferences_layout, false);
            addPreferencesFromResource(R.xml.preferences_layout);
    
            PreferenceScreen root = getPreferenceScreen();
    
            // Includes R.xml.other_preferences_layout and adds it to the bottom of the root preference screen
            PreferenceScreen otherPreferenceScreen = inflatePreferenceScreenFromResource(R.xml.other_preferences_layout);
            root.addPreference(otherPreferenceScreen);
            PreferenceManager.setDefaultValues(this, PREFERENCE_NAME, MODE_PRIVATE, R.xml.other_preferences_layout, false);
        }
    
        /**
         * Inflates a {@link android.preference.PreferenceScreen PreferenceScreen} from the specified
         * resource.<br>
         * <br>
         * The resource should come from {@code R.xml}
         * 
         * @param resId The ID of the XML file
         * @return The preference screen or null on failure.
         */
        private PreferenceScreen inflatePreferenceScreenFromResource(int resId) {
            try {
                Class<PreferenceManager> cls = PreferenceManager.class;
                Method method = cls.getDeclaredMethod("inflateFromResource", Context.class, int.class, PreferenceScreen.class);
                return (PreferenceScreen) method.invoke(getPreferenceManager(), this, resId, null);         
            } catch(Exception e) {
                Log.w(LOG_TAG, "Could not inflate preference screen from XML", e);
            }
    
            return null;
        }  
    }
    

    This example would use res/xml/preferences_layout.xml as the root and then add res/xml/other_preferences_layout.xml to the bottom of the root.

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

Sidebar

Related Questions

Background I have a web app that will create an image from user input.
Background: I have a little video playing app with a UI inspired by the
Background I am trying to create a copy of a business object I have
Background : I currently have a Web Forms, ASP.NET 3.5/C# application which I'm interested
Background: I have a MainTest class that has many buttons, each of which instantiate
Background: I am using Eclipse to develop an Android app. I have an xml
Background: I have a servlet in which I am dynamically generating javascript and putting
Background I have a massive db for a SharePoint site collection. It is 130GB
Background I work for a large organization which has thousands of MS Access applications
Background I have been asked by a client to create a picture of the

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.