question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Only Application Scope

See original GitHub issue

I’m newbie in DI, and decide to start learning it with toothpick. I try to refactor my sample application and add DI. My data repository is application singleton. Then I use it inside fragment, I inject it in fragment’s onCreate:

public class MyFragment extends BaseFragment {
    ........
    @Inject
    MyRepository repository;
    .........

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        Toothpick.inject(this, ((App)getActivity().getApplication()).getAppScope());
        super.onCreate(savedInstanceState);
    }

App.java

public class App extends Application {
    private Scope appScope;
    @Override
    public void onCreate() {
        super.onCreate();
        initToothpick();
    }

    private void initToothpick() {
        appScope = Toothpick.openScope(this);
        appScope.installModules(new SmoothieApplicationModule(this, Preference.NAME), new MyApplicationModule());
        Toothpick.inject(this, appScope);
    }

    public Scope getAppScope() {
        return appScope;
    }
}

Is it fine?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
stephanenicolascommented, Oct 10, 2017

You don’t have a fragment scope, you use the activity scope in the fragment. Which is what you have to do, and the activity closes it properly already.

I close the issue. Please use stack overflow for any new question. feel free to ping us here if no one answers.

0reactions
pdapnzcommented, Oct 10, 2017

Thank you, I refactored scopes as you recommends.


public class BaseViewModel extends AndroidViewModel {

    private Scope scope;

    public BaseViewModel(Application application) {
        super(application);
        scope = Toothpick.openScopes(application, this);
        scope.installModules(new ViewModelModule());
        Toothpick.inject(this, scope);
    }
    ...........

    @Override
    protected void onCleared() {
        super.onCleared();
        Toothpick.closeScope(scope);
    }
}

public class MainActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Scope scope = Toothpick.openScopes(getApplication(), this);
        scope.installModules(new ActivityModule());
        super.onCreate(savedInstanceState);
        Toothpick.inject(this, scope);
   ......

    @Override
    public void onDestroy() {
        Toothpick.closeScope(this);
        super.onDestroy();
    }
}

As I understand Activity and ViewModel now fine. App and activity used as scopes and scopes is closing. ViewModel has own scope independent of activity.

But should I close fragment scope? Now it is something like this:

public class BaseFragment extends LifecycleFragment {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        Scope scope = Toothpick.openScopes(getActivity().getApplication(), getActivity());
        scope.installModules(new FragmentModule());
        super.onCreate(savedInstanceState);
        Toothpick.inject(this, scope);
    }

}

Read more comments on GitHub >

github_iconTop Results From Across the Web

Application scope - ServiceNow Docs
Application scoping protects applications by identifying and restricting access to application files and data.
Read more >
Scopes - Auth0
When an app requests permission to access a resource through an authorization server , it uses the scope parameter to specify what access...
Read more >
Overview of permissions and consent in the Microsoft identity ...
App -only access uses app roles instead of delegated scopes. When granted through consent, app roles may also be called applications permissions.
Read more >
Application Scopes - Box Support
These scopes will largely apply when creating a "Custom Application" type with only minor differences between the different Authentication ...
Read more >
Excel Application Scope - UiPath Documentation Portal
UiPath.Excel.Activities.ExcelApplicationScope Opens an Excel workbook and provides a scope for Excel Activities. When the execution of this activity ends, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found