Reputation: 143
Good day to everyone!
I need to stop method execution until another activity will end. At the moment I'm trying to do it in this way:
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;
}
But it's not working.
Can you help me?
Thanks!
UPD: Maybe there is any way to prevent view from drawing? Because all what I need right now is delay calling of methods, which will redraw view of this activity. Now I have the new world drawn before the splash screen, saying about the world change, is shown, which is not looking good.
Upvotes: 4
Views: 4074
Reputation: 26766
I'm a little rushed for time so here's a generic answer:
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();
}
}
}
I'll come back and get you a working example this afternoon if you haven't got a solution by then...
This article should get you started
EDIT: This article demonstrates other approaches, all of which should be an improvement on your current solution. It talks you through how to update the UI from events in different threads and the benefits/costs of the various solutions.
Upvotes: 3
Reputation: 4600
I guess you are trying to do something like this:
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();
}
Then why not proceed this way?
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();
}
But if you really want to stop the showSplashWorldChangeAd
method for some reason, you need lock and key (as Basic says).
Upvotes: 1