代码之家  ›  专栏  ›  技术社区  ›  user5395084

AppCompat的AccountAuthenticatorActivity

  •  2
  • user5395084  · 技术社区  · 8 年前

    http://blog.udinic.com/2013/04/24/write-your-own-android-authenticator/

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

    使用常规 在AppCompat中,结果为 活动 ActionBar AccountAuthenticator活动 还有一个 操作栏 .

    2 回复  |  直到 8 年前
        1
  •  11
  •   eoinzy    4 年前

    我认为这不是真正的解决办法。如果你在做一个支持库的应用程序,混合AppCompatActivities、Fragments&c用标准的不是个好主意。

    我创造了一个 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();
        }
    }
    

    希望对别人有帮助。

        2
  •  8
  •   user4400167 user4400167    8 年前

    AppCompatDelegate ,我的代码基于 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 .