其他分享
首页 > 其他分享> > android – AppCompat的AccountAuthenticatorActivity

android – AppCompat的AccountAuthenticatorActivity

作者:互联网

我正在按照教程制作验证器:http://blog.udinic.com/2013/04/24/write-your-own-android-authenticator/

登录Activity需要扩展AccountAuthenticatorActivity,问题从这里开始:AccountAuthenticatorActivity扩展常规Activity而不是AppCompatActivity.

使用AppCompat中的常规Activity会导致没有ActionBar的Activity.我想使用AccountAuthenticatorActivity并拥有一个ActionBar.

解决方法:

关键是AppCompatDelegate,我的代码基于Android Studio生成的AppCompatPreferenceActivity类:

@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)的关键.

不要忘记您的活动必须扩展AppCompatAuthActivity.

标签:android,appcompat,android-support-library,appcompatactivity,android-authenticato
来源: https://codeday.me/bug/20190716/1474423.html