Murillo Ferreira
Murillo Ferreira

Reputation: 1433

Access view from anywhere without using static declaration

I have a bit experience in java for android development but I have some questions that is not very clear to me.

I have a ViewPager for screen slide, inside my fragment I have buttons that can switch from one screen to another.

What I did is, in my MainActivity, I declared my ViewPager as static, so I was allowed to use the setCurrentItem(int position) from anywhere.

But I learned that declaring a View as static is not a good practice since it would exist "forever" in the memory (as soon as the app is alive). But I though a new approach and I would like to know if it is better or not than using static Views.

Lets say I have the following:

public void MainActivity extends Activity{

    public static ViewPager pager;

    //code...
}

Then, on my Fragment class:

public class MyFragment extends Fragment{

        //code...

        private void changePage(int position){
            MainActivity.pager.setCurrentItem(position);
        }
    }

I though to create a class that extends the MainActivity, then I don't need the ViewPager to be static but only public, then I would call this class inside MyFragment and change the page. See:

public class PageControl extends MainActivity{

    private void setCurrentPage(int position){
        pager.setCurrentItem(position);
    }

}

Then, my changePage(int position) method inside MyFragment would be:

private void changePage(int position){
    PageControl mPageControl = new PageControl(); //Of course this line would be on onCreate()
    mPageControl.setCurrentPage(position);
}

I'm asking this because I'm afraid to have a lot of "information" inside my PageControl class since it is extending the MainActivity, but this way looks better because I could even create a listener for page changes inside PageControl and easily handle what I want.

So: keep my ViewPager as static or I should create the PageControl to manage my page changes? Any suggestions how I could do this, instead of these options I said?

Thanks in advance.

Upvotes: 0

Views: 102

Answers (2)

Md. Arafat Al Mahmud
Md. Arafat Al Mahmud

Reputation: 3214

You can simply define a callback Interface which your MainActivity implements. Just pass the MainActivity instance to the Fragment either as constructor parameter or some other defined method which takes as argument the Interface type. Then inside your Fragment call the implemented method when you need to slide to another page. This way you dont have to get the reference of the ViewPager inside the Fragment.

You can also do this:

public class MyFragment extends Fragment{

    private ArrayList<ChangePageListener> listeners = new ArrayList<ChangePageListener>();

    public addChangePageListener(ChangePageListener listener){
         if(!listeners.contains(listener){
             listeners.add(listener);
         }
    }

    public removeChangePageListener(ChangePageListener listener){
         if(listeners.contains(listener){
             listeners.remove(listener);
         }
    }

    private void changePage(int position)
    {
         for(ChangePageListener listener: listeners){
              listener.changePage(position);
         }
    }

}

public interface ChangePageListener{
    public void changePage(int position);
}

public MainActivity extends Activity implements ChangePageListener{

    public void changePage(int position){
        this.pager.setCurrentItem(position);
    }

}

Upvotes: 2

Simon
Simon

Reputation: 14472

Another approach is to make a base class for your activities, add a method to update the instance view, then extend the base class. Something like this:

public class ActvityBase extends Activity{

    ...

    public void changePagerPosition(int position){
         this.pager.setCurrentItem(position);
    }

}

public MainActivity extends ActivityBase{

    ...

}
public class MyFragment extends Fragment{

    ...

    ((MainActivity)getActivity()).changePagerPosition(position);

}

Upvotes: 2

Related Questions