Fragment custom view addToBackStack(), custom view, and back button - android

Problem:
I have a custom Calendar view that I generated using canvas drawing and stuff. This is the layout for Fragment A.
I have another Fragment B which when the user does something in Fragment A is instantiated and replaces Fragment A via getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container), fragmentB).addToBackStack().commit();
works fine up to this point (Fragment B is shown).
Now when I press the back button, I expect Fragment B to exit or be removed from the screen, to be replaced by the previous fragment A.
still works correctly - however the now displayed Calendar view is all messed up and sh*t, all the drawings are stacked up on the left side of the screen and does not look like a calendar at all.
Why is this happening, and how should I go about this?

i found the culprit - the cell width measurement inside my custom canvas is being set to zero for some particular reason - inside onMeasure. i moved the assignment statement inside onDraw and it worked perfectly.

Related

Parent fragment destroyed with shared element transition

I have a view hierarchy such as below.
MainActivity :
FirstFragment :
SecondFragment :
In my SecondFragment, i have a RecyclerView. When an item is clicked, i want to open it in another Fragment (in full screen) so i am using the addSharedElement() method and everything works fine after 2 days of retro engineering it : every post i have found on StackOverFlow does not mention that in order to work, the "full-screen" fragment has to replace the first parent inside the activity.
What i first wanted was to nested the full-screen fragment inside the SecondFragment but it's impossible with this method.
I have tried everything else, at each steps of my view hierarchy & none of them is working, except if i replace my FirstFragment by my full-screen fragment.
This is also impossible to use the transition with the add() method unfortunately.
The problem is that this fragment is destroyed & not recreated despite the call of addToBackStack(), when i press the "back-button" inside the NavigationBar.
I am not really familiar with the addToBackStack() method & how can i get back any fragments i have added there.
QUESTION 1 : How can i make it work either without destroying the parent fragment (FirstFragment in my view hierarchy), or without using shared element transition but achieving the same result ?
It seems that what the Transition is doing is recreating a view in the new fragment with the same width / height, x & y coordinates as the one in the previous fragment & animate it from the new fragment...
QUESTION 2 : Would it be a bad idea to do it myself ?
EDIT : My goal is to close the full-screen fragment with a swipe down and as soon as the finger is moved from 10dp along the Y axis, i want to be able to reveal the previous fragment behind (the SecondFragment in my case), that's why i don't want to destroy parent fragments.

How to blur the Parent Activity when a fragment is added?

An Activity which has an image when i click the image
1)A fragment should be added in the middle of the parent activity
2)When the fragment appears the background of parent activity should get blurred/dimmed.
3)And clicking outside the fragment the fragment should disappear again.
(Same effect we get when we click contact image in whatsApp it opens an overlay)
I have can add fragment successfully but the activity remains active.
How to achieve this effect?
I have tried fragmentTransaction.setCustomAnimation(int,int) but it does not work.
Thank you.
You have two options:
Use a DialogFragment setting the layout in the onCreateView() method.
Add a full screen Fragment with a background with alpha (for example #44000000). Add the OnTouchListener to the background view and in the onTouch() method, dismiss the Fragment.
By the way, I would seriously recommend you the first option.

How to scroll to the top of a ListFragment

My situation: I have two ListFragments (call them A and B) managed by one Activity which keeps persistent references to both of these Fragments. When I click a button in Fragment A, I replace that with Fragment B. The problem starts when I do the following flow.
A -> B -> (scroll) -> (back button) -> B
In that case, when I go back to Fragment B the second time, the previous scroll position is maintained, which I don't want. Instead, I would like for Fragment B to start with its ListView at the top of its content.
Things I have tried which do nothing:
Calling setSelection(0) in onActivityCreated
Calling setSelectionAfterHeaderViews() in onActivityCreated
Calling smoothScrollToPosition(0) in onActivityCreated
Interestingly, all of these work if I post them on a Runnable. However, when I do that there is a weird flickering the second time I open Fragment B.
So, how do I get Fragment B to automatically scroll to the top each time it is attached to its parent Activity? I feel like there must be something blindingly obvious that I'm missing, but I'm really stumped right now.
You're calling the right methods, but you're calling them in the wrong place.
I assume you have code that switches between the fragments and you call it when an item is clicked in A. So whenever you do the switch set the scroll to the top, something along these lines:
protected void switchList() {
ListFragment a = (ListFragment) getFragmentManager().findFragmentByTag("a");
ListFragment b = (ListFragment) getFragmentManager().findFragmentByTag("b");
b.getListView().setSelectionAfterHeaderView();
getFragmentManager().beginTransaction().hide(a).show(b).addToBackStack(null).commit();
}
And one important note: never keep persistent references to fragments in your activities. Whenever you need a fragment get it from the FragmentManager. This is crucial since on configuration change (like a device rotation, or when your app is suspended and restored) the fragments are recreated, and the reference you kept leads to a 'dead' fragment. Not only is it a major leak, it will also prevent your code from functioning. any change you make to the saved fragment is not reflected on the screen because the screen holds the newly created fragment.

Fragment LIfecycle in Custom Tab View

I have a custom widget that performs FragmentTransaction.replace when buttons are pressed. Currently, my code is set up such that the first time a fragment is created, it attaches a bunch of stuff to the view that isn't originally part of the xml layout file.
When the app first launches, all my fragments show stuff correctly, however, let's say I start on Fragment A. I can then transition to Fragment B (with B showing up correctly), however, when I transition back to Fragment A, all the stuff I have attached to the view of Fragment A is now gone. I know this happens because onCreateView is called which probably means the Fragment's view is re-generated when FragmentTransaction.replace is called.
Is there a way where I can keep my fragments around instead of having them re-generate their views when FragmentTransaction.replace is called?
Thanks!
Instead of using fragmentTransaction.replace, use fragmentTransaction.show and fragmentTransaction.hide.
That will keep your fragments from being destroyed.

Android overriding onBackPress unexpected behaviour

Yet another problem whilst trying to create the UI for an app.
I've got a Viewflipper in a tab which slides from View 1 to View 2 to View 3. I've overridden the onBackPress method to navigate back to a previous view if I am not on View 1.
It works fine when on View 2, pressing back goes to View 1.
But when on View 3, pressing back ends the Tab activity and takes me back to the sign in activity.
Here's a video in case my description was hard to understand: http://www.youtube.com/watch?v=2nKhgpq3rQA
Has anyone come across anything similar?
Why does the onBackPress get ignored in my Tab Activity when View 3 is displayed? I've debugged and put a breakpoint on the onBackPressed method in my Tab Activity, the breakpoint is hit in all cases except for when View 3 is the shown view. Werid.
Thanks for your time.
Update: I've discovered that if I add another ListView as the View 3 it works, but if I add a TextView as View 3 it doesn't behave as expected, crazy.
just don't call super.onBackPressed(); then you can handle every back click your self.
calling super.onBackPressed(); will get to the activity called before.
I solved this, there was some weird behaviour because I was adding the first view in my view flipper to the tab content, rather than adding the viewflipper itself.

Categories

Resources