Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Jaafar Mahdi
Jaafar Mahdi
4,925 Points

Close fragment after few seconds

Hi Treehouse Community :-)

I'm making some kind of a game in a beginner school project. The game is about guessing a hidden word. You have maximum 6 wrong guesses, at the 7th the game ends. Every time someone type in a wrong letter, a fragment appears with a picture that illustrates he is one step closer to losing. To continue playing, one has to press the "back" button physically on the phone, and that is not the intention.
My question is: How do I make this fragment, with the picture disappear after 3 seconds, all by itself.

I have tried this, since it is the only answer I could find on Google. But it only delays the picture by 3 seconds.

I hope someone can help, thanks in advance. :-)

new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

                Bundle antalForkert = getArguments();
                int wrong = antalForkert.getInt("forkert");
                if (wrong == 1 ) {
                    imgForkert.setImageResource(R.drawable.forkert1);
                }
                if (wrong == 2 ) {
                    imgForkert.setImageResource(R.drawable.forkert2);
                }
                if (wrong == 3 ) {
                    imgForkert.setImageResource(R.drawable.forkert3);
                }
                if (wrong == 4 ) {
                    imgForkert.setImageResource(R.drawable.forkert4);
                }
                if (wrong == 5 ) {
                    imgForkert.setImageResource(R.drawable.forkert5);
                }
                if (wrong == 6 ) {
                    imgForkert.setImageResource(R.drawable.forkert6);
                }


            }
        },3000);

1 Answer

Kourosh Raeen
Kourosh Raeen
23,733 Points

I'm not sure if you figured this out or not but take a look at this stackoverflow discussion and the answer that is having the activity implement a listener: http://stackoverflow.com/questions/5901298/how-to-get-a-fragment-to-remove-itself-i-e-its-equivalent-of-finish