0

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();
        }
    });
Shishi
  • 601
  • 2
  • 8
  • 27
  • Actually you should try to avoid running methods that can take a long time to finish in your Activities or your Fragments. Put them into seperate classes with an own thread. – Huxley Mar 20 '14 at 08:20
  • And by itself, nothing is actually "looping" and "updating" your UI. You have to take care of that yourself. In your example, after you change the value of the boolean, you need to notify the fragment to re-render itself. This can be as simple as calling `mFragment.render()`. If you post some of your code, it might be easier to help you... – hendrikbeck Mar 20 '14 at 08:26
  • I changed the question – Shishi Mar 20 '14 at 08:35
  • I'm not sure I understand what exactly you want to do. Which code do you want to execute and in which situation exactly? Can you try to be more precise in your description? – hendrikbeck Mar 20 '14 at 10:51

1 Answers1

1

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.

Fernando Gallego
  • 4,064
  • 31
  • 50