Skip to content
edolganov edited this page May 1, 2013 · 10 revisions

Common Cases

Logger

  • Setup
static {
    LogFactory.setTagPrefix("SOME-TAG");
}
  • Use
Logger log = LogFactory.getLog(getClass());
log.info("some info msg");
log.error("ups error", e);

Activity safe functons

No need of super calls anymore:

import easydroid.core.BaseActivity;

public class YourActivity extends BaseActivity { 

    @Override
    protected void onCreateSafe(Bundle bundle) {
        //no need of super.onCreate(bundle)
    }
    
    @Override
    protected void onDestroySafe() {
        //no need of super.onDestroy()
    }

}

Views Injection

Use @InjectView annotation:

import easydroid.core.BaseActivity;

public class YourActivity extends BaseActivity { 

	@InjectView(R.id.browser_back)
	View backButton;
	
	@InjectView(R.id.browser_home)
	View homeButton;

        @Override
	protected void onCreateSafe(Bundle savedInstanceState) {
            setContentView(R.layout.browser);
            //access to backButton and homeButton
        }

}

Content View Injection

Use @ContentView instead of setContentView function:

import easydroid.core.BaseActivity;

@ContentView(R.layout.browser)
public class YourActivity extends BaseActivity { 

    @InjectView(R.id.browser_back)
    View backButton;
    
    @InjectView(R.id.browser_home)
    View homeButton;

    @Override
    protected void onCreateSafe(Bundle savedInstanceState) {
        //access to backButton and homeButton
    }

}

Singleton Injection

  • Create Singleton class:
import easydroid.core.Singleton;
import android.content.Context;

public class BrowserState extends Singleton {
    
    //you can inject android context
    //and other singletons
    @Inject
    Context context;
    
    @Inject
    OtherSingleton otherSingleton;

}
  • Use @Inject annotation in Activity:
public class YourActivity extends BaseActivity { 

    @Inject
    BrowserState singletonObj;

    @Override
    protected void onCreateSafe(Bundle savedInstanceState) {
        //access to singletonObj
    }

}

BaseAsyncTask for Activity

BaseAsyncTask has flexible API for async work:

public class YourActivity extends BaseActivity { 


    @Override
    protected void onCreateSafe(Bundle savedInstanceState) {
        
        //call execute method
        execute(new BaseAsyncTask<UserState>() {
            
            @Override
            protected void onPreExecute() {
                showLoadindDialog("Loading...");
            }
            

            @Override
            protected UserState doInBackground() throws Throwable {
                return someLongService.getUserState();
            }
            
            @Override
            protected void beforeResult() {
                dismissCreatedDialog();
            }
            
            @Override
            protected void onSuccess(UserState result) {
                showUserState(result);
            }
            
            @Override
            protected void onFail(Throwable t) {
                log.error("ups", t);
            }
            
        });
    
    }
    
    private void showUserState(UserState result) { ... }

}

Clone this wiki locally