Question

I have a spinner item.When I click on position 2. I want the following code to execute.

if (pos == 2) {
    Log.e("TEST", "YOU CLICKED" + pos);
    for (int i = 0; i < 2; i++) {
        Log.e("TEST INSIDE FOR", "YOU CLICKED" + pos);
        if (i % 2 == 0) {
            // showFragOne();
            showFragOne();
            Log.e("TEST INSIDE FOR " + i, "YOU CLICKED" + pos);

            try {

                Thread.sleep(20000);

            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else if (i %2 == 1) {
            // showFragTwo();
            showFragTwo();
            Log.e("TEST INSIDE FOR " + i, "YOU CLICKED" + pos);

            try {
                Thread.sleep(20000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    Toast.makeText(mCoreContext,"One Fragment added from feature app",Toast.LENGTH_LONG).show();


}

What i want this code to do is change the Frame layout in 20s. But only the second fragment I am getting at last. Here showFragTwo() function:-

private void showFragTwo() {
                // TODO Auto-generated method stub
                FragmentTransaction ft = 
                        .getFragmentManager().beginTransaction();
                if (fragtwo.isAdded()) {
                    if (fragtwo.isHidden())
                        ft.show(fragtwo);
                    else
                        ft.hide(fragtwo);

                    ft.commit();
                } else {
                    ft.replace(com.example.coreapp.R.id.container, fragtwo,
                            "ADDFRAGTWO").commit();
                    ft.show(fragtwo);
                }
            }

Here showFragOne() function:-

private void showFragOne() {
        // TODO Auto-generated method stub
        FragmentTransaction ft =
                .getFragmentManager().beginTransaction();

        if (frag.isAdded()) {
            if (frag.isHidden())
                ft.show(frag);
            else
                ft.hide(frag);

            ft.commit();
        } else {

            ft.replace(com.example.coreapp.R.id.container, frag, "ADDFRAG")
                    .commit();
            ft.show(frag);
        }
    }

where i initialize frag and fragtwo at top to following to fragment class :-

frag = new AddFragmentOne();
fragtwo = new AddFragmentTwo();

I get the following as logcat o/p.

**02-28 04:56:34.250: E/TEST INSIDE FOR: YOU CLICKED2

02-28 04:56:34.290: E/TEST INSIDE FOR 0: YOU CLICKED2

02-28 04:56:54.350: E/TEST INSIDE FOR: YOU CLICKED2

02-28 04:56:54.350: E/TEST INSIDE FOR 1: YOU CLICKED2**

I am getting the logs message in o/p BUT WHY THE FRAGMENT IS NOT GETTING DISPLAYED?

ONLY THE SECOND FRAGMENT I AM GETTING

If i comment second if statemnt I AM GETTING FIRST FRAGMENT

Can anybody tell me where i am getting wrong?

Was it helpful?

Solution

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top