gpt4 book ai didi

java - `onCreate` 方法未被调用

转载 作者:行者123 更新时间:2023-12-02 09:46:41 24 4
gpt4 key购买 nike

我正在使用Dagger2.22注入(inject)MainActivity,它扩展了BaseActivityBaseActivity扩展了>DaggerAppCompatActivity。似乎两个 Activity 的 onCreate() 方法都没有被调用,因为我在 BaseActivityonCreate() 内部调用了一个函数,并且这不起作用,当我将 log 语句放入其中时,它们也没有被打印,并且当我将调试器放在 onStart()两个 Activity 的 >onCreate() 然后结果是调用 MainActivityonStart() 方法,而不是 onCreate() 方法em> 后跟 BaseActivityonStart() 被调用。虽然 MainActivity 正在显示,并且如果它被创建,那么 onCreate() 方法将被调用,但 onCreate() 方法内没有代码似乎不起作用,他们也没有在调试中被调用。我不明白到底发生了什么?

基本 Activity

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();
}
}

主要 Activity

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

这是我在组件中注入(inject)的模块
ActivityBuilders模块


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

@ContributesAndroidInjector
abstract MainActivity contributeMainActivity();

}

最佳答案

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

protected void onCreate(@Nullable Bundle savedInstanceState)

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

关于java - `onCreate` 方法未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56595752/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com