• SherlockActivity也可以用依赖注入的方法:


    场景:

        一个Activity必须继承RoboActivity才可以使用依赖注入。

    若一个Activity已经继承了别的Activity了。比如SherlockActivity 如何才能使用依赖注入呢?
    Roboguice提供了以下方法:
     
    public class LabaRoboActivity extends SherlockActivity implements RoboContext {
    
      protected EventManager eventManager;
      protected HashMap<Key<?>, Object> scopedObjects = new HashMap<Key<?>, Object>();
    
      @Inject
      ContentViewListener ignored; // BUG find a better place to put this
    
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        final RoboInjector injector = RoboGuice.getInjector(this);
        eventManager = injector.getInstance(EventManager.class);
        injector.injectMembersWithoutViews(this);
        super.onCreate(savedInstanceState);
        eventManager.fire(new OnCreateEvent(this,savedInstanceState));
      }
    
      protected void onRestart() {
        super.onRestart();
        eventManager.fire(new OnRestartEvent(this));
      }
    
      @Override
      protected void onStart() {
        super.onStart();
        eventManager.fire(new OnStartEvent(this));
      }
    
      @Override
      protected void onResume() {
        super.onResume();
        eventManager.fire(new OnResumeEvent(this));
      }
    
      @Override
      protected void onPause() {
        super.onPause();
        eventManager.fire(new OnPauseEvent(this));
      }
    
      @Override
      protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        eventManager.fire(new OnNewIntentEvent(this));
      }
    
      @Override
      protected void onStop() {
        try {
          eventManager.fire(new OnStopEvent(this));
        } finally {
          super.onStop();
        }
      }
    
      @Override
      protected void onDestroy() {
        try {
          eventManager.fire(new OnDestroyEvent(this));
        } finally {
          try {
            RoboGuice.destroyInjector(this);
          } finally {
            super.onDestroy();
          }
        }
      }
    
      @Override
      public void onConfigurationChanged(Configuration newConfig) {
        final Configuration currentConfig = getResources().getConfiguration();
        super.onConfigurationChanged(newConfig);
        eventManager.fire(new OnConfigurationChangedEvent(this,currentConfig, newConfig));
      }
    
      @Override
      public void onContentChanged() {
        super.onContentChanged();
        RoboGuice.getInjector(this).injectViewMembers(this);
        eventManager.fire(new OnContentChangedEvent(this));
      }
    
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        eventManager.fire(new OnActivityResultEvent(this,requestCode,resultCode,data));
      }
    
      @Override
      public Map<Key<?>, Object> getScopedObjectMap() {
        return scopedObjects;
      }
    }
    

      

     





    附件列表

  • 相关阅读:
    如何使网站不出现从客户端(:)中检测到有潜在危险的 Request.Path 值这样类似的提示
    layui下select下拉框不显示或没有效果
    video
    各个平台的mysql重启命令
    微信开放平台 redirect_uri参数错误
    JS中setTimeout()的用法详解
    验证视图状态MAC失败的解决办法
    The 'microsoft.jet.oledb.4.0' provider is not registered on the local machin
    一个Nginx部署多个vue前端项目总结
    Java之HttpClient调用WebService接口发送短信源码实战
  • 原文地址:https://www.cnblogs.com/lixiaodaoaaa/p/5175172.html
Copyright © 2020-2023  润新知