使用 LiveData 时无法进入 MainActivity
Cannot go to MainActivity when using LiveData
我的启动画面没有布局文件。这是我试过的:
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!isNotAuthenticated()) {
openLoginInActivity();
} else {
openMainActivity();
}
finish();
}
private void openMainActivity() {
viewModel.idLiveData.observe(this, new Observer<String>() {
@Override
public void onChanged(String id) {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
intent.putExtra("id", id);
startActivity(intent); //Go to MainActivity
}
});
}
}
使用这段代码,我得到了这个错误:
2019-09-03 12:03:06.615 1871-1934/? E/ViewRootImpl[myapp]: Could not unlock surface
java.lang.IllegalArgumentException
at android.view.Surface.nativeUnlockCanvasAndPost(Native Method)
at android.view.Surface.unlockSwCanvasAndPost(Surface.java:382)
at android.view.Surface.unlockCanvasAndPost(Surface.java:363)
at android.view.ViewRootImpl.drawSoftware(ViewRootImpl.java:3451)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:3339)
如果我在 onChanged()
以外的意图下创建,一切正常。我添加了一条日志语句,但 onChanged
甚至没有被触发。那么我怎样才能移动到下一个 activity 而不出现那个错误呢?
编辑:
public class SplashViewModel extends ViewModel {
private SplashRepository splashRepository;
MutableLiveData<String> idLiveData;
@Inject
SplashViewModel(SplashRepository splashRepository) {
this.splashRepository = splashRepository;
idLiveData = splashRepository.addIdToLiveData();
}
}
我如何在 Splash 中使用实时数据 Activity
class SplashActivity : BaseActivity<ActivitySplashBinding, SplashViewModel>() {
override val mViewModel: SplashViewModel by currentScope.inject()
override fun getLayoutResId(): Int {
return R.layout.activity_splash
}
override fun initialization() {
}
override fun initializeObserver() {
mViewModel.liveData.observe(this, Observer {
if (it) {
launchActivityWithFinish<LoginActivity>()
}
})
}}
这是我的 SplashViewModel
class SplashViewModel : BaseViewModel() {
val liveData = MutableLiveData<Boolean>()
init {
viewModelScope.launch {
delay(SPLASH_TIME_OUT)
liveData.postValue(true)
}
}}
我的飞溅Activity在java
public class SplashActivity1 extends BaseActivity<ActivitySplashBinding, SplashViewModel> {
@NotNull
@Override
protected SplashViewModel getMViewModel() {
return new SplashViewModel();
}
@Override
public int getLayoutResId() {
return R.layout.activity_splash;
}
@Override
public void initialization() {
}
@Override
public void initializeObserver() {
getMViewModel().getLiveData().observe(this, aBoolean -> {
if (aBoolean) {
Intent intent = new Intent(SplashActivity1.this, MainActivity.class);
intent.putExtra("id", "id");
startActivity(intent); //Go to MainActivity
finish();
}
});
}}
我的 SplashViewModel 在 Java
public class SplashViewModel extends BaseViewModel {
public MutableLiveData<Boolean> liveData = new MutableLiveData<>(false);
public SplashViewModel(){
new Handler().postDelayed(() -> liveData.postValue(true),SPLASH_TIME_OUT);
}}
它在 Kotlin 和 Java 中都非常适合我
我以前也遇到过类似的导航问题。我通常通过在 onChanged
中验证参数(在您的情况下为 String id)不为 null 并在内部调用将 id 更改为 null 的视图模型方法来修复它们。
viewModel.idLiveData.observe(this, new Observer<User>() {
@Override
public void onChanged(String id) {
if(id != null) {
viewModel.navigationDone();
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
intent.putExtra("id", id);
startActivity(intent); //Go to MainActivity
}
}
});
其中 navigationDone 是一个视图模型方法,例如:
void onNavigationDone() {
idLiveData.setValue(null);
}
我的启动画面没有布局文件。这是我试过的:
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!isNotAuthenticated()) {
openLoginInActivity();
} else {
openMainActivity();
}
finish();
}
private void openMainActivity() {
viewModel.idLiveData.observe(this, new Observer<String>() {
@Override
public void onChanged(String id) {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
intent.putExtra("id", id);
startActivity(intent); //Go to MainActivity
}
});
}
}
使用这段代码,我得到了这个错误:
2019-09-03 12:03:06.615 1871-1934/? E/ViewRootImpl[myapp]: Could not unlock surface
java.lang.IllegalArgumentException
at android.view.Surface.nativeUnlockCanvasAndPost(Native Method)
at android.view.Surface.unlockSwCanvasAndPost(Surface.java:382)
at android.view.Surface.unlockCanvasAndPost(Surface.java:363)
at android.view.ViewRootImpl.drawSoftware(ViewRootImpl.java:3451)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:3339)
如果我在 onChanged()
以外的意图下创建,一切正常。我添加了一条日志语句,但 onChanged
甚至没有被触发。那么我怎样才能移动到下一个 activity 而不出现那个错误呢?
编辑:
public class SplashViewModel extends ViewModel {
private SplashRepository splashRepository;
MutableLiveData<String> idLiveData;
@Inject
SplashViewModel(SplashRepository splashRepository) {
this.splashRepository = splashRepository;
idLiveData = splashRepository.addIdToLiveData();
}
}
我如何在 Splash 中使用实时数据 Activity
class SplashActivity : BaseActivity<ActivitySplashBinding, SplashViewModel>() {
override val mViewModel: SplashViewModel by currentScope.inject()
override fun getLayoutResId(): Int {
return R.layout.activity_splash
}
override fun initialization() {
}
override fun initializeObserver() {
mViewModel.liveData.observe(this, Observer {
if (it) {
launchActivityWithFinish<LoginActivity>()
}
})
}}
这是我的 SplashViewModel
class SplashViewModel : BaseViewModel() {
val liveData = MutableLiveData<Boolean>()
init {
viewModelScope.launch {
delay(SPLASH_TIME_OUT)
liveData.postValue(true)
}
}}
我的飞溅Activity在java
public class SplashActivity1 extends BaseActivity<ActivitySplashBinding, SplashViewModel> {
@NotNull
@Override
protected SplashViewModel getMViewModel() {
return new SplashViewModel();
}
@Override
public int getLayoutResId() {
return R.layout.activity_splash;
}
@Override
public void initialization() {
}
@Override
public void initializeObserver() {
getMViewModel().getLiveData().observe(this, aBoolean -> {
if (aBoolean) {
Intent intent = new Intent(SplashActivity1.this, MainActivity.class);
intent.putExtra("id", "id");
startActivity(intent); //Go to MainActivity
finish();
}
});
}}
我的 SplashViewModel 在 Java
public class SplashViewModel extends BaseViewModel {
public MutableLiveData<Boolean> liveData = new MutableLiveData<>(false);
public SplashViewModel(){
new Handler().postDelayed(() -> liveData.postValue(true),SPLASH_TIME_OUT);
}}
它在 Kotlin 和 Java 中都非常适合我
我以前也遇到过类似的导航问题。我通常通过在 onChanged
中验证参数(在您的情况下为 String id)不为 null 并在内部调用将 id 更改为 null 的视图模型方法来修复它们。
viewModel.idLiveData.observe(this, new Observer<User>() {
@Override
public void onChanged(String id) {
if(id != null) {
viewModel.navigationDone();
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
intent.putExtra("id", id);
startActivity(intent); //Go to MainActivity
}
}
});
其中 navigationDone 是一个视图模型方法,例如:
void onNavigationDone() {
idLiveData.setValue(null);
}