Skip to content

Why variable gets reset on button click(s) in Android?

Can someone help me to understand what is happening here? Have been trying to debug, but feel like stuck!

I am trying to animate some online images in my Android app using the following method.

private void animateImages() {
 
        // URL loading
        // int i = 1; (initialized earlier)
        // ArrayList<String> myImages = new ArrayList<>(); (initialized earlier)
        myImages.clear();
        While (i < 11) {
        // Adds ten images using web link
        myImages.add("My_web_url");
        i++;
        }            

        AccelerateInterpolator adi = new AccelerateInterpolator();
        try {
            Field mScroller = ViewPager.class.getDeclaredField("mScroller");
            mScroller.setAccessible(true);
            mScroller.set(viewPager, new MyScroller(getApplicationContext(), adi, 1));
        }
        catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        catch (IllegalAccessException e) {
            e.printStackTrace();
        }
 
        if (viewPager != null) {
            viewPager.setAdapter(new MyPageAdapter(getApplicationContext(), myImages));
        }
 
        final Handler handler = new Handler();
        final Runnable Update = new Runnable() {
            // Printing variables for debugging 
            System.out.println("The page number is=" + currentPage);
            System.out.println("The myImages size is=" + myImages.size());
            
            public void run() {
                if (currentPage == myImages.size() - 1) {
                    currentPage = 0;
                }
                viewPager.setCurrentItem(currentPage++, true);
            }
        };
 
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.post(Update);
            }
            // delay and period can be initialized as desired
        }, delay, period);
    }
 
}

When I call this method in OnCreate, animation works fine. However, when I call this method in OnClickButton Listener, variable myImages size (before public void run()) become zero and due to this animation doesn’t work.

In the above, MySCroller and MyPageAdapeter are java classes. But, most likely, the issue is related to button click, and I don’t understand why it resets the myImages size which halts the animation!

This is how button click listener is called. What am I doing wrong?

MyButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        animateImages();
    }
});

Edit 1:

Thanks to all the comments, I made a little progress.

I moved all these variables from MainActivity to animateImages() method. The animation runs with button click as well but there is a bump in animation, where too images moves too fast then bump and so on..

// Added just before while loop
DELAY_MS = 1000;
PERIOD_MS = 1000;
i = 1;
currentPage = 0;

I notice the same animation bump if I move the URL loading while loop to OnCreate().

Answer

The second time you call animateImages it clears myImages but then doesn’t loop because i is not reset so it remains empty. Move creation of that list to onCreate instead to avoid that issue.