`onCreate`方法没有被调用

时间:2019-06-14 09:54:54

标签: java android dagger-2

我正在使用 Dagger2.22 注入扩展了 BaseActivity MainActivity ,而 BaseActivity 扩展了 > DaggerAppCompatActivity 。好像两个活动的onCreate()方法都没有被调用,因为我在 BaseActivity onCreate()中调用了一个函数,但没有用,当我在其中放置 log 语句时,它们也不会被打印出来,并且当我将调试器放在 onStart() onCreate()<上时,然后发现这两个活动的/ em>代替了 onCreate()方法被称为 MainActivity onStart()方法,随后是<正在调用 BaseActivity 的em> onStart()。尽管正在显示 MainActivity ,并且如果创建了 onCreate()方法,但 onCreate()方法内部没有代码似乎工作,他们都没有在调试中被调用。我不明白到底发生了什么事?

BaseActivity

public abstract class BaseActivity extends DaggerAppCompatActivity {
    private static final String TAG = BaseActivity.class.getSimpleName();

    @Inject
    public SessionManager sessionManager;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);

        Log.d(TAG, "onCreate: ");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart: ");


    }

    private void subscribeObservers(){
        Log.d(TAG, "subscribeObservers: ");
        sessionManager.getAuthUser().observe(this, new Observer<AuthResource<User>>() {
            @Override
            public void onChanged(AuthResource<User> userAuthResource) {
                Log.d(TAG, "onChanged: auth resource ==> " + userAuthResource + "auth status ==> " + userAuthResource.status);
                if (userAuthResource != null) {
                    Log.d(TAG, "onChanged: auth status ==> " + userAuthResource.status);

                    switch (userAuthResource.status) {
                        case LOADING: {
                            break;
                        }
                        case AUTHENTICATED: {
                            Log.d(TAG, "onChanged: LOGIN SUCCESS: " + userAuthResource.data.getEmail());
                            break;
                        }
                        case ERROR: {
                            Toast.makeText(BaseActivity.this, userAuthResource.message +
                                            "\nDid you enter number between 1 and 10",
                                    Toast.LENGTH_SHORT).show();
                            break;
                        }
                        case NOT_AUTHENTICATED: {
                            navLoginScreen();
                            break;
                        }
                    }
                }
            }
        });
    }

    private void navLoginScreen(){
        Intent intent = new Intent(this, AuthActivity.class);
        startActivity(intent);
        finish();
    }
}

MainActivity

public class MainActivity extends BaseActivity {
    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        setContentView(R.layout.activity_main);

        Log.d(TAG, "onCreate: ");

    }

    @Override
    protected void onStart() {
        super.onStart();

        Log.d(TAG, "onStart: ");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case R.id.logout: {
                sessionManager.logout();
                return true;
            }
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

这是我在组件中注入的模块
ActivityBuildersModule


@Module
public abstract class ActivityBuildersModule {
    @ContributesAndroidInjector(modules = {
            AuthViewModelsModule.class,
            AuthModule.class
    })
    abstract AuthActivity contributeAuthActivity();

    @ContributesAndroidInjector
    abstract MainActivity contributeMainActivity();

}

1 个答案:

答案 0 :(得分:1)

要覆盖的onCreate()的签名是:

protected void onCreate(@Nullable Bundle savedInstanceState)

,而不是您在代码中覆盖的内容。