大家好!
我需要停止方法执行,直到另一个 Activity 结束。 目前我正在尝试以这种方式进行:
private boolean isPausedWhileSplash;
public void showSplashWorldChangeAd(String oldWorldName, String newWorldName) {
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
Intent intent = new Intent(this, SplashScreen.class);
intent.putExtra(SplashScreen.MSG_STRING_KEY, oldWorldName + " -> "
+ newWorldName);
intent.putExtra(SplashScreen.OLD_WORLD, oldWorldName);
intent.putExtra(SplashScreen.NEW_WORLD, newWorldName);
startActivityForResult(intent, RESULT_OK);
isPausedWhileSplash = true;
while (isPausedWhileSplash) {
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
isPausedWhileSplash = false;
}
但它不起作用。
你能帮帮我吗?
谢谢!
UPD:也许有什么方法可以防止绘制 View ?因为我现在需要的只是延迟调用方法,这将重绘此 Activity 的 View 。现在我在启动画面之前绘制了新世界,显示了关于世界变化的信息,这看起来不太好。
最佳答案
我有点赶时间,所以这是一个通用的答案:
public class MonitorObject{
}
public class MyWaitNotify{
MonitorObject myMonitorObject = new MonitorObject(); //To be used for synch
public void doWait(){
synchronized(myMonitorObject){
try{
myMonitorObject.wait(); // Wait until the notify method is called by another thread
} catch(InterruptedException e){...}
}
}
public void doNotify(){ //Notify waiting threads that they can continue
synchronized(myMonitorObject){
myMonitorObject.notify();
}
}
}
如果到那时您还没有找到解决方案,我会在今天下午回来给您提供一个工作示例...
This article应该让你开始
编辑: This article演示了其他方法,所有这些方法都应该是对当前解决方案的改进。它向您介绍了如何根据不同线程中的事件更新 UI 以及各种解决方案的 yield /成本。
关于java - 停止方法执行直到另一个 Activity 完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12034123/