parallaxviewpager icon indicating copy to clipboard operation
parallaxviewpager copied to clipboard

background disappears when I check "Don't keep activities"

Open misaka-10032 opened this issue 10 years ago • 10 comments

Maybe some states need to be saved? As a habit, I check "Don't keep activities" at system settings when debugging. After I switch to other app and switch back, which means the activity is destroyed, The background image disappears.

I carefully check the background, and find that the image is still there actually, but it's placed at the right side of screen; I can only see the image at the right edge of screen at first page of viewPager.

Any idea to fix it?

misaka-10032 avatar Sep 05 '14 04:09 misaka-10032

To use the "Don't keep activities" option is a really really really bad habit. You should be aware that a regular user with stock Android will never use nor have access to this option. By using that option you should expect applications to misbehave since you are literally calling and forcing the GC on them. It's like the back button kill thing Cyanogen decided to add to their MOD which IMHO is not only a bad practice but a pain in the butt for us developers who receive complains about bugs caused by it. In other words, do not use that option, and if you use it, don't expect applications or libraries to behave as you expect.

mradzinski avatar Sep 15 '14 17:09 mradzinski

@mradzinski I don't agree with you. We cannot guarantee if the user is interrupted (e.g. respond to a phone call or SMS). It is very likely when user switches to other apps, the activity is destroyed by system somehow for limited RAM resource. For a better UX, we must resume the activity (and its corresponding views of course) correctly even that case happens.

misaka-10032 avatar Sep 16 '14 01:09 misaka-10032

@misaka-10032 Under a low RAM scenario the system may claim the activity resources, however, the GC may or may not consume those claimed resources completely, it's not guaranteed to happen and it's completely out of your hands. However, the "Don't keep activities" option DOES guarantee a total recollection of freed resources by forcing the GC to run causing an unstable state of the application and its activities. Sadly there's no proper way of simulating a low memory scenario in Android, but the "Don't keep activities" option is not prepared nor designed with that use case in mind.

mradzinski avatar Sep 16 '14 02:09 mradzinski

Hi @mradzinski and @misaka-10032,

I had a similar problem with the component. When I back from another activity when I had a page different that the two first selected or when I invoked the setCurrentItem method on the viewPager, the background disappeared while the bitmap was there. After some testing I realized that something was happening with the destination Rect. The values were right was there was an strange behaviour with it.

I made a simple test to check it. I modified the drawBitmap call with drawBitmap(bitmap, x, y, painter) and I checked that the first part of the bitmap was painted on the third page.

I forked the project and make some corrections to resolve it. You can check them here: https://github.com/yuraksisa/parallaxviewpager

Sorry if the explication is not so accurate, but I think that if you try my code you'll get it.

yuraksisa avatar Nov 22 '14 15:11 yuraksisa

@yuraksisa nice job! Send me a PR and I'll merge it.

andraskindler avatar Jan 01 '15 21:01 andraskindler

@yuraksisa Can you please send a pr ?

kobynet avatar Jan 03 '15 22:01 kobynet

Hi,

I'm on holidays. I'll do it as soon as I come home. How do I send a Pr?

Regards

Juan El 3/1/2015 23:22, "Koby" [email protected] escribió:

@yuraksisa https://github.com/yuraksisa Can you please send a pr ?

— Reply to this email directly or view it on GitHub https://github.com/andraskindler/parallaxviewpager/issues/6#issuecomment-68611964 .

yuraksisa avatar Jan 04 '15 06:01 yuraksisa

Juan, thanks! Follow the steps detailed here.

If you need any help, let me know. And of course have a great holiday!

andraskindler avatar Jan 04 '15 12:01 andraskindler

After checking @yuraksisa branch, the bug still exists.. Reproducing steps:

  1. Add FragmentPagerAdapter as the pager adapter.
  2. Add two pages, each hold a fragment (can be same fragment)
  3. Swipe to the 2nd page
  4. Rotate the device

I can see that a very small portion of the image is painted on the right side. Checked both on android 4.2.2 and 5.0.1 and it's the same behavior on both.

Solution: Using @yuraksisa branch is not enough, viewpager has issue with measuring so when calling setCurrentItem the getWidth() method is 0. To solve this issue: In the activity holding the parallaxviewpager add this code:

parallaxViewPager = ((ParallaxViewPager) findViewById(R.id.parallaxviewpager));
parallaxViewPager.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (parallaxViewPager.getCurrentItem() > 0)
                    parallaxViewPager.setCurrentItem(parallaxViewPager.getCurrentItem());

                if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
                    parallaxViewPager.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                else
                    parallaxViewPager.getViewTreeObserver().removeGlobalOnLayoutListener(this);

            }
        });

kobynet avatar Jan 04 '15 13:01 kobynet

I experienced exactly the same issue as @kobynet. Using @yuraksisa's fork is not enough. Adding an OnGlobalLayoutListener solves the issue.

edualonso avatar Feb 03 '15 07:02 edualonso