1
votes

this question is intended to ask to the community if the approach i've taken for my app is correct or may have some side effect:

I've created: - an Activity, called MasterAcitity, extended from every activity in my app. The application tag in the manifest is declared as follow

<application
        android:name="my.package.name.MyApplication"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@style/ApplicationStyle" >
  • A class, called MyApplication, that extends android.App.Application, which has the following code

    private static Context _context;
    
    public static Context getContext() {
        return _context;
    }
    
    public static void setContext(Context context) {
        _context = context;
    }
    
  • In the manifest the application tag is declared as follow

    <application
        android:name="my.package.name.MyApplication"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@style/ApplicationStyle" >
    

MasterActivity executes this code in OnResume and OnCreate methods

MyApplication.setContext(this);

Every activity of the app extends MasterActivity.

There is a class in the app, called DialogHelper which has a static method

public static void showDialog(String message)

which uses android.app.AlertDialog.Builder to create and show a dialog using as context MyApplication.getContext()

so from everywhere in my app i can use

DialogHelper.showDialog("my message");

Is this approach going to work? or i need to pay attention to something?

My doubt is on the static context...

Thanks

2
you can use getaplicationcontext(). did you tried that ? - itsrajesh4uguys
you have to pass the context when needed. This may cause memory leaks. - Anis BEN NSIR
@itsrajesh4uguys, i have some classes with static methods which should show dialogs Anis, can you explain me why it should cause memory leaks? - Apperside
@SimonVeloper : "i have some classes with static methods which should show dialogs" - Out of interest what do those 'classes' extend? - Squonk
just pass the context as the arguments while accessing those classes - itsrajesh4uguys

2 Answers

1
votes

Is this approach going to work?

Using an Application for UI work has a history of causing problems. Either use an Activity, or a specialized Context for a given set of circumstances (e.g., getThemedContext() on ActionBar, getContext() on a Presentation).

0
votes

You should also have a onDestroy handler that resets the context to null if the context belongs to the activity beeing destroyed.

Instead of a global static context i would prefer a api like this

 DialogHelper.showDialog(this.getContext(),"my message");

[Updated 5.5.2013]

Every Activity, Service , BroadcastReceiver is indirectly derived from Context via ContextWrapper and other classes like Views save and use Context. they normally expose it through a getContext() function. So the context should be available where neccessary.