I want to make it possible to display a fragment on top of every activity (if the right action is called). It should look like a window on top of everything. This works fine so far, but I am a bit confused about how to get the layout integration working.
When I wanted to display something like a fragment on top of everything, I had to use a RelativeLayout as the root element, then nesting maybe LinearLayout (containing the activity's layout) and fragment.
Is that the only way to achieve this? Do I have to refactor now every activity layout to use RelativeLayout as the base? Or is there a more straightforward way?
I cannot comment, so will write here. Please provide some code to express what you mean, much easier to understand it than. If i understood you correct, u want to have a window on top of another window?
Cannot see why this would be voted down, and not even a comment for the reason :/.
You might achieve what you want with an overlapping fragment. I have had this side effect when making my app, i.e. that a fragment view is transparent on top of another fragment. To achieve this, just skip if(savedInstanceState != null){return;} in your onCreate() in your activity. And instead of replacing a fragment (transaction.replace) use (transaction.add)
Related
Basically this is the structure of my app.
In Activity, there is a container which hosts fragments and on the bottom there is Bottom Navigation View. I want to implement something like this:
I know this is possible by using ViewPager but the app is already much complex that I wouldn't want to rewrite all the flows to make whole activity as fragment and then add in ViewPager. Is there any other approach I can take to solve this issue? Anything like drawer layout or similar? Any close possible solution suggestions are welcome.
This may sound stupid, but can I do that? I have searched a little, and this says "Re-attach a fragment ... causes its view hierarchy to be re-created, attached to the UI, and displayed.", probably meaning that the UI of the fragment is recreated losing states and other things.
The reason why I am trying to do is that I would like to achieve something like the Bottom Sheet of Google Maps. The only title part is showed at the bottom, but we can swipe it up and it becomes a Bottom Sheet. I think when only the title part is showing, it is not actually a Bottom Sheet, as it would be difficult to make exactly that part of a Bottom Sheet to be shown. I think Google Maps has put the title part separately, and is replacing it with a Bottom Sheet whose top part looks just like the title part, when swiping up begins.
So, to achieve similar effect, I thought I would create a cluster of UI as a fragment, and when the user begins swiping up, I place a Bottom Sheet with the peek size is set exactly to the height of the fragment, and move the fragment into the top of the Bottom Sheet, so that it can be swiped up.
But I want to keep everything of the fragment without recreating anything, as if I take the fragment and just move into the fragment instantly. Is that possible, or should I save the states and restore them when the fragment's UI is recreated?
Yes. Consider fragment as View and play with ObjectAnimator or any animation class as per behaviour. (Slide-up animation will do the trick)
Note: Fragment will initialize first even you display 20% of portion over main layout. State will remain same This does answers your question i guess.
Here is similar example for your work around. Reference
I just need help cleaning up some information that doesn't make sense to me, with use cases if possible.
WHAT I UNDERSTAND:
With fragments in android, I understand that if you plan on replacing them you need to have a container view, preferably a FrameLayout, and add the initial fragment to the container during the activities onCreate method. But there is one thing that continues to not make sense to me.
WHAT I DON'T UNDERSTAND AND NEED HELP WITH:
What rules are there regarding where/how the container view is set up, if there are any. Android Developers site makes it look like the container view needs to be it's own XML layout file, but it doesn't say that and I have seen examples on here with FrameLayouts nested inside of your typical layout files, but they are all specific uses and I need to understand the rules of setting a container up.
There are no rules. You just need any ViewGroup -- position and size it however you want. When you add the Fragment into it, it will behave just as if you'd created the Fragment's View manually, and called yourViewGroup.addView(fragmentView).
FrameLayout is typically used just because it makes a good container with no real behavior (you just give it a size and position and let the fragment fill that container).
There's absolutely no need to make the container view its own layout file. In fact, if you want the Fragment to take over the content view of the entire Activity, you could even just add the Fragment to the Activity using the ID android.R.id.content.
Here's what I want to do in an app, hope it's clear:
NOTE: I don't want to have all 3 views on the screen at once!
I have a search window, I have results, and I want to show details about a result item when it's clicked.
On a small screen (phone) it's pretty clear: I show one view at a time.
On a tablet, especially in landscape mode I'd like to show 2 views at a time, means:
[search | results] and when a result item is clicked, show [results | item detail].
My ideas:
Have an Activity "SearchAndResults", when result item is clicked, open another Activity calles ResultsAndDetails. Both Activities would use Fragments, eg SearchFragment, ResultFragment, DetailFragment.
This might be quite easy to implement but doesn't seem very elegant to me, especially because I can't have any animation (let the result view slide to the left, let the detail view slide in from the right)
Have all 3 view in one activity and hide/ show them with layout.setvisibility(). No animations either, and I hate to do too much layout stuff in my code.
A viewpager that shows 2 views, but can't be swiped by the user, only from code?
What's a good way to do this?
I would use Fragments, as in your idea #1. This question seems to be describing the exact same type of layout that you are trying to implement. It has several answers on how to animate fragment transitions, including complete code examples.
We use different activities to navigate through our app. One of them is very complex and contains a lot of nested views/images etc, so when I usestartActivity(intent1) in the activity before it, there is a short delay and it feels/looks laggy.
All the information needed to create the content views is known in advance.
So my question is: is there a smart way to prerender/preload the activity or its content view?
As i figured the intend only holds information about the next activity but no instance of the activity itself, so i assume there is no way to tell the intend to create the activity before i call the startMethod.
One idea i hat was to create a static view before starting the activity and set this view as contentView in the onCreate() method. But it seems like a bad hack to me.
Thanks in advance!
The best solution would be not to start a completely new activity but using a ViewPager or ViewFlipper. Switching between Views should be then nearly instantaneous and you also get the chance to easily apply animations.
If that is not possible you could start a new activity but put a ViewSwitcher in there. The first View would be a progress bar. The second view is inflated and added to the Switcher in a background thread.