每个人都过得愉快!
我需要停止方法执行,直到另一个活动结束。 目前我正试图以这种方式做到这一点:
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:也许有办法阻止视图的绘制?因为我现在需要的是延迟调用方法,这将重新绘制此活动的视图。现在我在启动画面之前绘制了新世界,说明世界变化,显示出来,看起来不太好。
答案 0 :(得分:3)
我有点匆忙,所以这是一个通用的答案:
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以及各种解决方案的优势/成本。
答案 1 :(得分:1)
我猜你正试图做这样的事情:
public void showSplashWorldChangeAd(String oldWorldName, String newWorldName){
/* You have done some initialization work */
startActivityForResult(intent, RESULT_OK);
/* This is what you want to do after the activity returns */
afterActivityReturns();
}
那么为什么不这样做?
public void showSplashWorldChangeAd(String oldWorldName, String newWorldName){
/* You have done some initialization task here*/
startActivityForResult(intent, RESULT_OK);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
/* This is what you want to do after the activity finishes */
afterActivityReturns();
}
但是如果你真的想因某种原因停止showSplashWorldChangeAd
方法,你需要锁和密钥(正如Basic所说)。