Static objects in Activities with View Pagers

Asked

Viewed 58 times

1

I have an Activity with a view pager, and this one controls three fragments, F1, F2, F3. The F1 fragment shows user-added items, the F2 fragment loads all items from an external server, showing them in a list, and finally, the F3 fragment shows the items the user has decided to store. User saved items are saved in txt, so they do not need server access.

The problem: Whenever the user saves an item, I need to add a mark on the item selected in F2, and automatically add it in F3, and the same happens when the user unchecks in F3 and the item needs to be updated in F2, but in this case only the layout, as it has nothing to do with external connections.

The solution created by me: Leave the public and static adapters in Activity and create methods within it that update the adapters whenever there is an action, for this, I had to create a class that extends the class Application, to create a global context.

Doubt: It is known that it is not good to create static contexts due to memory leakage problems, but I believe that this should happen because keeping code working during other activities, really must weigh. But using this solution between fragments, since these are always working in the foreground, can also bring risks of memory leakage? Noting that I only use the static context in this Activity so that the adapters can be updated by other fragments.

Thanks in advance!

  • 1

    Is it not possible to create a Singleton to support the adapters/data? Thus, it is not necessary to mess with context and the application. It has a lib called eventbus, which serves for this type of problem. It sends a message to "nothingness" and a certain method will be "listening" if that message was called. http://greenrobot.org/eventbus/documentation/how-to-get-started/

  • This idea sounds interesting. But I could implement it even if my adapters need context-like parameters?

  • I didn’t understand the problem. can specify more?

  • The adapters I’m using need the Context parameter, and my question is, if using Eventbus, can the context be used in this library? He wouldn’t end up being static either?

  • 1

    I don’t think so. Normal variables within static methods continue to be normal variables. So, I guess nothing will change.

1 answer

0

There are a few ways to solve this your problem. , an alternative:

Create an interface

public interface MeuFragmentInterface {
    void fragmentBecameVisible();
}

Attach ombudsman in setOnPageChangeListene

mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(final int position, final float v, final int i2) {
        }

        @Override
        public void onPageSelected(final int position) {
            MeuFragmentInterface fragment = (MeuFragmentInterface) mPagerAdapter.instantiateItem(mViewPager, position);
            if (fragment != null) {
                fragment.fragmentBecameVisible();
            } 
        }

        @Override
        public void onPageScrollStateChanged(final int position) {
        }
    });

Implement Interface in your Fragment

public class MinhaActivity extends Fragment implements MeuFragmentInterface{
    @Override
    public void fragmentBecameVisible() {
          System.out.println("TestFragment");
    }
}

So you can call your method during the action of fragment.

Details

  • Okay, that makes sense, but I’d like to update the adapter items as soon as the click runs, not just when it’s visible to the user. Think about it, so that the user does not see the animation of adding or removing items from recyclers... understands?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.