Shishi
Shishi

Reputation: 611

How to re-attach the fragment to the activity?

When I rotate my screen the fragment will detach from the activity. However I have a listener in my fragment which runs the asynctask after a button is pressed. That works fine. So I'm searching way to do the same without the button so directly when the page is changed.

In my MainActivity I have a onPageChangeListener looking like this :

    @Override
    public void onPageSelected(int position)
    {
        // TODO Auto-generated method stub
        currentSelectedFragmentPosition = position;
        frag.onPageVisible(currentSelectedFragmentPosition);
                    frag2.onPageVisible(currentSelectedFragmentPosition);
    }

Then in my fragment the onPageVisible function is this :

public void onPageVisible(int position)
{
    tabstrippos = position;

    if (MainActivity.selected[0])
    {
        if (tabstrippos == pageNumber)
        {
            mainActivity = getContext();
            if (totalItems < 3)
            {
                if (isAdded())
                {
                    startNewAsyncTask();
                }
                else
                {
                    Log.w("tag", "its not added");
                }
            }
        }
    }
}

This goes to the else after rotate because the activity is detached

The listener I was talking about comes from a gitHub class I use gitHub and looks like this inside my fragment :

onStart()
{
....

    ((PullToRefreshListView) getListView()).setOnRefreshListener(new OnRefreshListener()
    {
        @Override
        public void onRefresh()
        {
            // Do work to refresh the list here.
            startNewAsyncTask();
        }
    });

Upvotes: 0

Views: 644

Answers (1)

Fernando Gallego
Fernando Gallego

Reputation: 4116

There is no looping like in other systems, here, as soon as you change the boolean, you have to call some method to react to it.

Upvotes: 1

Related Questions