Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7723857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:32:51+00:00 2026-06-01T04:32:51+00:00

Each of my Activities needs a correspoding singleton View implementation. What’s the best strategy

  • 0

Each of my Activities needs a correspoding singleton View implementation. What’s the best strategy to inject them into activities?

  1. constructor injection Activity constructor is called from an ActivityMapper’s getActivity(). The ctor already has a parameter (a Place object). I would have to create the ActivityMapper with all possible views injected. Not good…

  2. method injection – “A function so annotated is automatically executed after the constructor has been executed.” (GWT in Action, 2nd Ed.) Well, “after the ctor has been executed” is apparently not fast enough because the view (or an RPC service injected this way) is still not initialized when the Activity’s start() method is called and I get a NPE.

  3. constructing the injector with GWT.create in Activity’s ctor. Useless, as they would not longer be singletons.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:32:53+00:00Added an answer on June 1, 2026 at 4:32 am

    What worked best for us was to use Assisted Inject.

    Depending on the case, we defined activity factories either in the activity itself, in a package (for building all the activities in that package), or in the ActivityMapper.

    public class MyActivity extends AbstractActivity {
       private final MyView view;
    
       @Inject
       MyActivity(MyView view, @Assisted MyPlace place) {
          this.view = view;
          ...
       }
       ...
    }
    
    public class MyActivityMapper implements ActivityMapper {
       public interface Factory {
         MyActivity my(MyPlace place);
    
         FooActivity foo(FooPlace place);
    
         ...
       }
    
       // using field injection here, feel free to replace by constructor injection
       @Inject
       private Factory factory;
    
       @Overrides
       public Activity getActivity(Place place) {
          if (place instance MyPlace) {
             return factory.my((MyPlace) place);
          } else if (place instance FooPlace) {
             return factory.foo((FooPlace) place);
          }
          ...
       }
    }
    
    // in the GinModule:
    install(new GinFactoryModuleBuilder().build(MyActivityMapper.Factory.class));
    

    BTW, for method injection to work, you still have to create your activities through GIN, so you’d have the same issues as with constructor injection. There’s no magic, GIN won’t magically inject classes that it doesn’t know about and doesn’t even know when they’ve been instantiated. You can trigger method injection explicitly by adding methods to your Ginjector, but I wouldn’t recommend it (your code would depend on the Ginjector, which is something you should avoid if you can):

    interface MyGinjector extends Ginjector {
       // This will construct a Foo instance and inject its constructors, fields and methods
       Foo foo();
    
       // This will inject methods and (non-final) fields of an existing Bar instance
       void whatever(Bar bar);
    }
    
    ...
    
    Bar bar = new Bar("some", "arguments");
    myGinjector.whatever(bar);
    ...
    

    A last word: I wouldn’t pass the place object directly to the activity. Try to decouple places and activities, that allows you to move things around (e.g. build a mobile or tablet version, where you switch between master and detail views, instead of displaying them side by side) just by changing your “shell” layout and your activity mappers. To really decouple them, you have to build some kind of navigator though, that’ll abstract your placeController.goTo() calls, so that your activities never ever deal with places.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a tabActivity which has 3 tabs and 3 different activities in each
I have 40 activities that do not depend on each other. I want to
Does switching activities in Android start a fresh JVM? It seems like each activity
Assume that we have 3 different activities. Each activity has its own XML file
This is my code: -@activities.each do |a| -if @i%3 == 0 %ul %li=link_to a.name,
I have a database that will store events. Each event has different activities. The
I have a TabActivity and inside 5 tabs with 5 activities each one. I
I have 2 activities. Each extends Activity. I tried all ways I found here,
so I know that the TabWidget does not create new activities on each tab
I am having two activities (activity1 and activiy2) and each activity is having one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.