3 android android-appcompat android-authenticator android-support-library appcompatactivity
我正在按照教程制作验证器:http://blog.udinic.com/2013/04/24/write-your-own-android-authenticator/
登录Activity需要扩展AccountAuthenticatorActivity,问题从这里开始:AccountAuthenticatorActivity扩展常规Activity而不是AppCompatActivity.
Activity在AppCompat中使用常规会导致Activity无ActionBar.我想使用AccountAuthenticatorActivity和有一个ActionBar.
mar*_*RDZ 10
我认为这不是真正的解决方案。如果您正在开发带有支持库的应用程序,将 AppCompatActivities、Fragments 等与标准程序混合使用并不是一个好主意。
我创建了一个AccountAuthenticatorAppCompatActivity扩展AppCompatActivity,然后从 API 复制/粘贴代码AccountAuthenticatorActivity,它似乎可以正常工作。
public class AccountAuthenticatorAppCompatActivity extends AppCompatActivity {
private AccountAuthenticatorResponse mAccountAuthenticatorResponse = null;
private Bundle mResultBundle = null;
public final void setAccountAuthenticatorResult(Bundle result) {
mResultBundle = result;
}
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
mAccountAuthenticatorResponse =
getIntent().getParcelableExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE);
if (mAccountAuthenticatorResponse != null) {
mAccountAuthenticatorResponse.onRequestContinued();
}
}
public void finish() {
if (mAccountAuthenticatorResponse != null) {
// send the result bundle back if set, otherwise send an error.
if (mResultBundle != null) {
mAccountAuthenticatorResponse.onResult(mResultBundle);
} else {
mAccountAuthenticatorResponse.onError(AccountManager.ERROR_CODE_CANCELED,
"canceled");
}
mAccountAuthenticatorResponse = null;
}
super.finish();
}
Run Code Online (Sandbox Code Playgroud)
}
希望它对某人有所帮助。
小智 8
关键是AppCompatDelegate,我的代码基于AppCompatPreferenceActivityAndroid Studio生成的类:
@SuppressWarnings("unused")
public class AppCompatAuthActivity extends AccountAuthenticatorActivity {
private AppCompatDelegate mDelegate;
@Override
protected void onCreate(Bundle savedInstanceState) {
getDelegate().installViewFactory();
getDelegate().onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
getDelegate().onPostCreate(savedInstanceState);
}
public ActionBar getSupportActionBar() {
return getDelegate().getSupportActionBar();
}
public void setSupportActionBar(@Nullable Toolbar toolbar) {
getDelegate().setSupportActionBar(toolbar);
}
@Override
@NonNull
public MenuInflater getMenuInflater() {
return getDelegate().getMenuInflater();
}
@Override
public void setContentView(@LayoutRes int layoutResID) {
getDelegate().setContentView(layoutResID);
}
@Override
public void setContentView(View view) {
getDelegate().setContentView(view);
}
@Override
public void setContentView(View view, ViewGroup.LayoutParams params) {
getDelegate().setContentView(view, params);
}
@Override
public void addContentView(View view, ViewGroup.LayoutParams params) {
getDelegate().addContentView(view, params);
}
@Override
protected void onPostResume() {
super.onPostResume();
getDelegate().onPostResume();
}
@Override
protected void onTitleChanged(CharSequence title, int color) {
super.onTitleChanged(title, color);
getDelegate().setTitle(title);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
getDelegate().onConfigurationChanged(newConfig);
}
@Override
protected void onStop() {
super.onStop();
getDelegate().onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
getDelegate().onDestroy();
}
public void invalidateOptionsMenu() {
getDelegate().invalidateOptionsMenu();
}
private AppCompatDelegate getDelegate() {
if (mDelegate == null) {
mDelegate = AppCompatDelegate.create(this, null);
}
return mDelegate;
}
}
Run Code Online (Sandbox Code Playgroud)
这AppCompatDelegate是添加ActionBar到任何常规Activity(例如PreferenceActivity)的关键.
不要忘记你的活动必须延伸AppCompatAuthActivity.
| 归档时间: |
|
| 查看次数: |
1340 次 |
| 最近记录: |