问题描述
我正在按照该教程制作身份验证器:
I'm making an authenticator following the tutorial: http://blog.udinic.com/2013/04/24/write-your-own-android-authenticator/
登录活动需要扩展 AccountAuthenticatorActivity
,则问题从这里开始: AccountAuthenticatorActivity
扩展常规的 Activity
而不是 AppCompatActivity
。
The login Activity requires to extend AccountAuthenticatorActivity
, the issue starts here: AccountAuthenticatorActivity
extends the regular Activity
and not AppCompatActivity
.
使用常规的 Activity $ c AppCompat中的$ c>导致
活动
而没有 ActionBar
。我想使用 AccountAuthenticatorActivity
并使用 ActionBar
。
Using the regular Activity
in AppCompat results in a Activity
without ActionBar
. I want to use AccountAuthenticatorActivity
AND having an ActionBar
.
推荐答案
键是 AppCompatDelegate
,我的代码基于 AppCompatPreferenceActivity
类由Android Studio生成:
The key is AppCompatDelegate
, my code is based on the AppCompatPreferenceActivity
class generated by Android 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;
}
}
AppCompatDelegate
是将 ActionBar
添加到任何常规 Activity
(例如 PreferenceActivity
)。
The AppCompatDelegate
is the key to add ActionBar
to ANY regular Activity
(for example PreferenceActivity
).
别忘了您的活动必须扩展 AppCompatAuthActivity
Don't forget your activity must extend AppCompatAuthActivity
.
这篇关于AppCompat的AccountAuthenticatorActivity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!