您可以扩展BaseActivity
实现所有检查代码的 a,然后Activity
您使用的任何新代码都将内置它。
我在一个无需登录即可访问 50% 的活动的应用程序中使用它,并显示一个提示,startActivityForResult(...)
用于捕捉登录尝试的成功或失败。
它工作得非常好,并且把所有东西都整齐地封装起来:)
编辑
这是一个可以根据需要修改的代码片段:
/**
* This is an abstract class that all Activities will be based on.
*/
public abstract class BaseActivity extends ActionBarActivity
{
protected static final int SHOW_WALLET = 100;
protected static final int SHOW_OFFER_DETAIL = 101;
protected static final int REGISTER = 102;
protected static final int SHOW_ACCOUNT = 103;
protected static final int LOGGED_IN_OK = 103;
protected static final int REGISTERED_OK = 104;
protected static final int DIALOG_DELAY = 300;
// for passing data in extras
protected static final String TEXT_SEARCH = "text_search";
protected static final String SEARCH_TERM = "search_term";
protected boolean mAlternateTitle = false;
private Dialog loading;
private Runnable showDialog;
protected final void openAccount()
{
Intent i;
if ( LoginManager.getInstance().isLoggedIn() )
{
i = new Intent( this, MyAccountActivity.class );
startActivity( i );
}
else
{
i = new Intent( this, LoginActivity.class );
startActivityForResult( i, SHOW_ACCOUNT );
}
}
}