Android fragment transaction error when "clean up memory" - android

Short story:
I got error when use FragmentTransaction.setCustomAnimations(....).
When I checking animation listener is only call onStartAnimation & not call onEndAnimation. (it's only happen when I use some tool optimized ram).
Long story:
Here is follow I test in my app.
Input:
We have 2 fragment A, B. MainActivity will open fragment A then open Fragment B. When user press back -> back to fragment A.
Step:
Open fragment A.
Open fragment B.
Use some tool like battery doctor -> Optimized Ram.
Open app -> Current still in Fragment B.
Press back to go to fragment A.
=> Animation never call onAnimationEnd && APP STUCK. I can do anything in with my app.
Notes: If I don't use some tool for "Optimized Ram" -> It always working as well.
Please help me for fix this bug !
UPDATE
I just got Log when app "lag".
Could not find class 'android.support.v4.app.FragmentTransitionCompat21$1', referenced from method android.support.v4.app.FragmentTransitionCompat21.setEpicenter

Related

Fragment.OnStop is not called while Activity.OnStop is called

I have ActivityA which contains FragmentF.
ActivityA contains also NavigationDrawer infrastructure.
When I start the app - all is fine.
There are called (among others):
ActivityA.OnStart
FragmentF.OnStart
ActivityA.OnResume
FragmentF.OnResume
.. and content is shown.
But when I press home (to minimize app & show home screen) - here comes the problem:
ActivityA.OnStop is called
FragmentF.OnStop is NOT called
Interesting facts:
- if I switch items in NavigationDrawer then FragmentF.OnStop is called and content of another fragment is loaded in ActivityA
- in all cases (minimize the app & switch content in NaviDrawer) FragmentF.OnPause is always called
As a workaround I put code (expected to work in OnStop) to OnPause but wondering:
- why FragmentF.OnStop is not called
- how to make it called
I'm using Xamarin (thus CamelCase namie convention :) - but I don't suspect this platform for bug, it seems like Android native behavior.
First of all, I do not know what kind of specific layout in your project or use other plugins. I test it in my demo(Activity contains Fragment).
github.com/851265601/FragmentDemo
When i click the home button, Onstop of Fragment was called like screenshot in this demo. This issue may related other reasons, do you use Util.log to generate it and capture the log in logcat. Note: Don't put time-consuming operations in OnPause

Activity recreate issue in Android Nougat Only?

I have developed small project, in which there are three fragments added in a single activity.
I am getting an issue in Nougat only i.e.
Start the application and go from fragment 1 -> fragment 2 and in fragment 2 there is a single button on this button click open external browser and when press back button, activity reload again and start from fragment 1.
In below versions from Nougat it is working perfectly, it back on fragment 2 but in Nougat (Samsung Galaxy s8+) it activity reloads and starts from fragment 1.
so please help me to resolve this issue.
thanks
First check your code in onResume where if you are reloading the ViewPager or setting the currentItem.
Other Reason can be Don't keep activities activated. Can you check in the Developer tools to see if Don't keep activities is on?
You can check it from Device settings Like this:
Settings -> Developer Options -> Don't keep activities

Android barcodescanner still image with multiple ZXingScannerView

in my app I have a fragment using the dm77/barcodescanner.
I'm working on a shop app, ex: you scan an item -> page of product -> continue shopping -> open another scanner sessione -> etc.
Now, as you can see, because the clinet wants to maintain the back stack, multiple istances of that fragment could be open (but not at the same time).
The first fragment with the scanner works well, the others has a still image, even if I call
scannerView.stopCamera();
scannerView = null;
leaving the first fragment.
NOTE: the other fragment works well if, instead of add we use replace. But that will create problems on the fragment's animations.
After furious debugging, I found how to make all working:
When the fragment is visible / in use, create and add programmatically to your layout the ZXingScannerView, starting the camera and preview;
When the fragment is not visible / used, remove the ZXingScannerView from your layout;
On some android version (like 5.01) be careful to not call startCamera more than one time, otherwise you'll not be able to open it anymore until you restart the app. The error here is caused by dead thread exception.
Hope to have been enough clear and helpful.

How to avoid multiple instances of a fragment in back stack?

I want to avoid that in this navigation use case: A -> B -> A -> B -> A -> B ...
Where all the fragment instances are kept in the back stack. Reason: Avoid out of memory error.
I tried creating an own navigation workflow, like described here: https://stackoverflow.com/questions/18041583/fragments-backstack-issue?noredirect=1#comment26393904_18041583 (which is supposed to mimic activity behaviour calling always finish() after starting a new one, together with letting only the very first one (home) in the navigation stack).
But it seems to be either very wrong or ununderstandable.
So I thought, also, to implement a behaviour like activity "bring to front" flag. But I don't know how to do it. Maybe something with popBackStack - but I don't know how to ask the fragment if the transaction already is in the backstack. And I don't know if I'm on the right path.
This should be a quite standard task, since every navigation menu basically has this problem. But still, seems not to be straight forward to implement, and also can't find information about it.
Any idea?
Take a look at the FragmentManager backstack. It has facilities for looking at/popping entries in the fragment backstack. You might want logic something along the lines of: if the user is asking for a fragment that is at the top of the stack (the previous fragment) then exit this fragment (go back) otherwise start a new one.
That would produce:
A (user asks for B)
A->B (user asks for A again)
A
.. but would not prevent
A (user asks for B)
A->B (user asks for C)
A->B->C (user asks for A)
A->B->C->A
That would require rewinding the stack back to "A" from "C", which you can do.. but then if that is the case, perhaps you should be unconditionally popping the fragment stack before starting a new fragment (I.E. No back stack at all..)

Android application doesn't exits nor goes to background

I have created a Android contact-book application. But when I enter the application, go through 3-4 screens and accordingly various activities are called. After which if I press the back button repetitively I return on first screen of the application.
If here I press back button some or the other previously visited screen is shown again, rather the application should either exit or should go in background.
Where have I missed out some standards that causes me to be stuck?
In short, the back-button on the first-screen of application shows irrelevant previously visited screens of the Android application. So I'm unable to move application in background or exit.
This issue can have these causes:
The application wasn't closed before
If you are developing with Eclipse and the emulator and old version of your application does not necessarily get killed if you fix something and test the application again. Eclipse only tells the emulator to start the main activity.
So your Activity stack possibly looked this:
Main Activity -> Activity A -> Activity B -> Activity A
thus pressing back from the last one will bring up the Activities in the order
Activity B -> Activity A -> Main Activity -> Home-Screen
if you do not exit the application and just let the emulator create the Main-Activity again this will happen:
Main Activity -> Activity A -> Activity B -> Activity A -> Main Activity
Thus any actions from that point on will extend the activity stack but does not necessarily clean the old entries out of your stack. (this will only happen if the system considers them not necessary anymore and kills them)
Overriden methods
It is possible (but unlikely) that you have caused this behaviour yourself.
If you did not override methods such as onPause() you most certainly did not cause this. Some apps are forcing the start of an activity in the onPause()-method (which is very bad style, since this is not expected behaviour). This could change your activity stack.

Categories

Resources