I have an activity with the bottom navigation menu.
I want to make it when I press the middle button on the menu, a small fragment pops up (doesn't fill the whole screen) and the rest of the screen gets a little bit darker and every touch that occurs outside of the small fragment I created will not be recognized as a touch in the other background fragment but instead will destroy the small fragment.
I created the small fragment and called detach and attach to the background fragment when I create and destroy the small one. However, it doesn't call the onPause and onResume functions in the fragments so it doesn't work. Also, I don't know how to make it that if I press outside of the fragment, it gets destroyed.
Can anyone help me?
That's what I'm trying to achieve:
Related
I am currently writing a drawer layout as my main layout, with an embedded FrameLayout that I will be using to hold each “page” when an item on the drawer is clicked on. When the app first starts, an initial fragment will be show. Other fragments may be added/replaced later which is fine, however, my problem is that when the user clicks the back button on the very first “initial fragment”, I don’t want that particular fragment to be removed from the layout. Currently, it is being removed and it’s just showing a drawer layout with no other content (which makes sense). I want the app to automatically exit if the initial fragment was the last one showing and the back button is pressed, instead of removing that initial fragment and then after another back press, then it exits.
Things I have thought of doing:
Not adding the first fragment to the backstack. (If I do this, I can compare it with the classname of the fragment which is a somewhat longer string, or I can use a boolean value for once the first fragment has been placed (and not added to backstack), the boolean is set which allows the fragments to now be added.
Overriding the onBackPressed function of the activity
Does anyone have a suggested way of doing this or can think of a better way? Thanks
The first bullet point sounds the cleanest. You have no other need to handle conditions when back is hit, correct? If that's the case, it's less lines of code (removing one as opposed to adding several) and you get to keep default Activity methods as is.
I know that's not exactly what you asked, but I think the first bullet point is so clean, that I just wouldn't try something else.
I have implemented same in one of the app using my own Stack of fragment. and also implemented onBackPressed method.
Every time when user clicks on item in drawer i add fragment in stack and in back press once its length is 1 I finish the activity with message.
On item click -- Add/replace fragment in container.
OnBackPressed -- Pop fragments from stack and once its last one i finish activity.
Hope this can give you another option to consider.
I have a fragment that fits half the screen which I want to put an overlay over and I want the fragments functionality (scrolling and clicking on items in it) to be disabled. I also want the activity to register a click anywhere on the disabled fragment (which will remove the overlay and re enable the fragment)
My first idea was to try find a way to pause the fragment, but nothing really worked, so now I am thinking about putting another fragment or possibly dialogue over the fragment and so that it covers the scrollbar and clicks in the fragment and registers its own clicks. Would this be possible? Would it be better to do it with a fragment or dialogue?
When I first start the application it works correctly, but when application goes in background (by clicking home button) something goes wrong.
My Activity has a pager with 4 fragments, and when I reload Activity some of the fragments are lost (nothing is displayed).
I read that the pager keeps memory of the current fragment, the one on the left and the one on the right. How can I solve such a problem?
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.
I have an application that has a list of buttons created dynamically in the code. When one of these buttons is clicked, a new activity containing a similar list of buttons is added on top of the stack.
These lists of buttons each have a different view for portrait and landscape, so I am overriding OnConfigurationChanged to lay update the existing buttons because it is faster than letting the view recreate itself.
When the second view changes orientation, even though the previous activity is in the background, it performs the changes to the buttons in its list.
The second activity is also transparent and you can see the previous activity through it. Normally you won't see the first activity from the second activity, but the entire second activity is in a HorizontalScrollView and the user can slide the view off the screen which then finishes the activity and goes back to the first activity.
The problem is that when there are hundreds of buttons on each activity and both activities are being updated on orientation change, things slow down considerably.
In all my cases, I have noticed that things go smoothly if only one activity is being updated on orientation change. Is there any way I can postpone the previous activities changes OnConfigurationChange or speed this up in general?