Search code examples
androidandroid-intentparameter-passing

How to pass a value to an activity from another WITHOUT starting it?


I want to pass a value from activity A to activity B without actually starting the activity B (therefore this rules out the use of Intents and putExtra). The activity B may or may not be started but when it does it needs to display the value passed to it by activity A.

I searched high and low but couldn't find any relevant solution to this seemingly simple question. Any help will be appreciated!


Solution

  • Use global class like :

    public class AppConfig {
    
        Context context;
        public AppConfig(Context mxt) {
    
            context = mxt;
        }
    
        public static String filepath = null;
    

    Then, in activity A before onCreate() :

    AppConfig config;
    Context context;
    

    and in onCreate() method put this :

    context = getApplicationContext();
    config = new AppConfig(context);
    

    And, in second Activity B before onCreate() :

    AppConfig config;
    Context context;
    

    And, in onCreate() method put this :

    context = getApplicationContext();
    config = new AppConfig(context);
    

    And set the value where you want. Hope this will help you.