0

Following issue:

In my Android-App I am using the Application-Component to instantiate a class called AppController. This class is used to instantiate some other so called "Manager"-classes. E.g a DatabaseManager used for accessing the SQLite Database.

In my Activity I am calling the following in the "onCreate" method:

mApplication = (MyApplication) getApplication();
mAppController = mApplication.getAppController();

Now when I am creating a fragment that needs access to this AppController because the view of the fragment needs some information from the DatabaseManager.

Currently I am using the following approach:

Get the AppController in onAttach(Activity activity) from the parent Activity and then using mAppController.getDatabaseManager() in the fragments onCreateViev(...).

At the moment this seems to work. BUT I already read that the activity-Object in onAttach(...) can sometimes be null.

This is the problem:

When the activity is null in onAttach I would not be able to get the DatabaseManager and my application would crash when setting up the view.

My sort of nasty solution at the moment:

Currently the only thing I could think of is checking whether the activity is null in onAttach(...) and if it is null to create the view of the layout not in onCreateView(...) but in the onActivityCreated(...) lifecycle method because there it would be 100% sure that the activity is not null anymore.

What I don't like about this approach is that I would create the view of the fragment not in the "correct" lifecycle method and I am not sure if this has any side-effects...

I appreciate every opinion and comment :)

Siggy

3 Answers 3

2
+50

Personally I am using a static variable for the Application for this.

public class CustomApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        ApplicationHolder.INSTANCE.setCustomApplication(this);
    }
}

And then I can have the variable in a singleton enum pattern:

public enum ApplicationHolder {
    INSTANCE;

    private CustomApplication customApplication = null;

    public CustomApplication getCustomApplication() {
        return customApplication;
    }

    public void setCustomApplication(CustomApplication customApplication) {
        this.customApplication = customApplication;
    }
}

That way you can always access application through

ApplicationHolder.INSTANCE.getCustomApplication();

However, you can turn this up a notch if you don't need to obfuscate using ProGuard (because Dagger 1.2.2 doesn't work with ProGuard :(... )

Because then you can create a module with a @Provides method that returns this application

@Module(complete = false, library = true)
public class ContextModule {
    @Provides
    public CustomApplication providesApplication() {
        return ApplicationHolder.INSTANCE.getCustomApplication();
    }
}

Because with that, you can create a @Provides method for your AppController in its own module

@Module(complete = false, library = true)
public class AppControllerModule {
    @Provides
    @Singleton
    public AppController providesAppController(CustomApplication customApplication) {
        return new AppController(customApplication);
    }
}

Where AppController as thus needs a constructor that takes CustomApplication for a parameter to initialize itself.

In order to make this work, you need to use Dagger like so

//CustomApplication
@Override
public void onCreate() {
    super.onCreate();
    ApplicationHolder.INSTANCE.setCustomApplication(this);
    Injector.INSTANCE.init(new RootModule());
}

Where RootModule is

@Module(
    includes = {
        ApplicationModule.class,
        AppControllerModule.class
    },
    injects = {
        YourFragment.class
    }
)
public class RootModule {
}

And Injector is

public enum Injector
{
    INSTANCE;

    private ObjectGraph objectGraph = null;

    public void init(final Object rootModule)
    {

        if(objectGraph == null)
        {
            objectGraph = ObjectGraph.create(rootModule);
        }
        else
        {
            objectGraph = objectGraph.plus(rootModule);
        }

        // Inject statics
        objectGraph.injectStatics();

    }

    public void init(final Object rootModule, final Object target)
    {
        init(rootModule);
        inject(target);
    }

    public void inject(final Object target)
    {
        objectGraph.inject(target);
    }

    public <T> T resolve(Class<T> type)
    {
        return objectGraph.get(type);
    }
}

Then you can do

public class YourFragment extends Fragment {
    ...
    @Inject
    AppController appController;

    @Override
    protected void onActivityCreated(Bundle bundle) {
         super.onActivityCreated(bundle);
         Injector.INSTANCE.inject(this);
    }
}

More info on Dagger can be found here: https://stackoverflow.com/a/27036934/2413303

Although I think onCreateView can work with the Injector too.

2
  • 1
    Thank you for this answer. Although I didn't look into Dagger yet the first part already helped me to implement the functionality I was looking for. I really like the Singleton-Enum-Pattern
    – Siggy
    Mar 19, 2015 at 8:54
  • I agree on the enum pattern, it was primarily described in the book Effective Java (2nd edition) which I wholeheartedly recommend. Glad I could help! Mar 19, 2015 at 9:01
0

Why don't you just get the reference for the DatabaseManager in the onCreateView(...)?

1
  • Because I can not be 100% sure that the activity is created and not NULL in the onCreateView(...) method (see the documentation). The earliest time it is guaranteed that the activity is not NULL is in the onActivityCreated(...) method.
    – Siggy
    Mar 17, 2015 at 9:38
0

At first approach, you should create your views inside onCreate() method, and fill it with data when activity (and all stuff what you need from it) is become available. Also, you can try to avoid processing data inside Fragment. You can define interface for loading necessary data, make your activity implement it, and ask activity to do all work when user performs an action. Your fragment only need to show result of it.

1
  • Isn't this like 1 of the reasons to use fragments? Not to implement everything in 1 activity? Because I use a lot of fragments and some of them are really complex and do a lot of stuff (like showing a selfmade mapview). For me there is no reason to manage everything from the activity containing all the fragments.
    – Siggy
    Mar 17, 2015 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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