Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

In many Android applications which I have found online or written myself, there is an application class like this:

public class MyApp extends Application {
    private static MyApp instance;

    public MyApp() {
        super();
        instance = this;
    }

    public static MyApp getInstance() {
        return instance;
    }
}

This pattern provides an easy access to application context and all application data from every point of our code. Because this pattern is widespread why don't Android framework developers make Application class a singleton by default?

share|improve this question
    
Hi, this should really be posted on Programmers or StackOverflow. It doesn't fit the format for CodeReview (see the FAQ –  Trevor Pilley Nov 23 '12 at 13:04
add comment

closed as off topic by Jeff Vanzella, Brian Reichle, Trevor Pilley, Glenn Rogers, svick Nov 24 '12 at 0:34

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

up vote 2 down vote accepted

Application cannot be a singleton because there are more than one applications running at the same time. Even if you have access to only one Application instance, it doesn't mean there is only one.

EDIT: On Android OS, every application is running in its own VM instance - true, but there is a difference between API architecture and OS architecture. While Android OS designers decided that every application will be running in its own Dalvik instance for security (or other) reasons, why should API designers rely on that? What if some future system version changes this behavior?

The API architecture should always follow logic. android.app.Application class represents an application on the device. Logic says there won't be only one application, therefore singleton pattern is not used.

share|improve this answer
1  
Could you explain in more details what do you mean under "there are more than one applications running at the same time"? Because every application runs in their own Dalvik instance, therefore inside this JVM it is a singleton –  Dmitriy Tarasov Nov 23 '12 at 12:26
    
@DmitriyTarasov edited the answer. –  Sulthan Nov 23 '12 at 14:55
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.