This happens because you make the Thread sleep 20 seconds and its likely the Ui Thread.
You should replace this Thread.sleep()
statement by a Timer
that shows the appropriate fragment :
if(pos == 2) {
showFragOne();
new Timer().schedule(new TimerTask() {
@Override
public void run() {
showFragTwo();
}
}, 20000);
}
To complete the answer based on commentaries : I suggest you make one method
/** This method shows the fragment whose 'number' is {@code fragmentNumber}. */
public void showFrag(int fragmentNumber) {
Fragment f = fragments.get(fragmentNumber); // fragments can be a List for example
// Then use the code you already have to show fragment 'f'
}
Then, you can modify the preceding code like this :
if(pos == 2) {
final AtomicInteger count = new AtomicInteger(0); // Because use inside TimerTask requires a final variable
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if(count.get() < MAX_FRAG_INDEX)
showFrag(count.getAndIncrement);
else
cancel();
}
}, 0, 2000);
}