Concurrency / UI update problem when starting an activity
I have an activity, which when starting (onCreate or onStart) needs to manipulate some views, and then maybe immediately starts another activity (a interstitial ad in its own activity).
There seems to be a problem with the order/concurrency: The ad activity is opened, and then the manipulated view R.id.productlist_filterbar from the previous activity is drawn on top of the interstitial ad activity.
(Simplified) code:
@Override
public void onCreate(Bundle savedInstanceState开发者_运维百科) {
super.onCreate(savedInstanceState);
setContentView(R.layout.productlist);
// Do some init stuff ..
// Manipulate the GUI
findViewById(R.id.productlist_filterbar).setVisibility(View.VISIBLE);
// If executed, then a new intent is fired in here to open the interstitial ad
if (..we have an ad..) {
Intent i = new Intent(this, AdViewActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(i);
overridePendingTransition(R.anim.fadein, 0);
}
}
What happens when I call startActivity() while there are still pending GUI modifications?
And, what would be the best solution for this case?
Thanks for any help!
I would have a flag set in onAttachedToWindow()
callback and then check for this flag in onWindowFocusChanged()
if set then start the ad activity and reset the flag.
void onAttachedToWindow() {
mFlag = true;
}
void onWindowFocusChanged(boolean hasFocus) {
if (hasFocus && mFlag) {
// start ad activity.
mFlag = false;
}
}
精彩评论