androidandroid-support-libraryandroid-appcompatappcompatactivityandroid-authenticator

AccountAuthenticatorActivity for AppCompat


I'm making an authenticator following the tutorial: http://blog.udinic.com/2013/04/24/write-your-own-android-authenticator/

The login Activity requires to extend AccountAuthenticatorActivity, the issue starts here: AccountAuthenticatorActivity extends the regular Activity and not AppCompatActivity.

Using the regular Activity in AppCompat results in a Activity without ActionBar. I want to use AccountAuthenticatorActivity AND having an ActionBar.


Solution

  • 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;
        }
    
    }
    

    The AppCompatDelegate is the key to add ActionBar to ANY regular Activity (for example PreferenceActivity).

    Don't forget your activity must extend AppCompatAuthActivity.