Let's say in an activity's xml it's using a LinearLayout as its root layout. And the activity during runtime wants to show a dialog/dialog fragment. Wouldn't the activity's xml have to be implicitly wrapped around with a FrameLayout for this to work due to needing to control the Z axis of views? Like you open the activity's xml and you see LinearLayout. However, behind the scenes, it's wrapped in a FrameLayout. Is this correct?
No, it's not.
It's complicated, and I'm honestly unable to properly explain it, but every Activity is placed in its own Window. Every Dialog attached to that Activity uses the Activity's Window to display itself.
Related
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.
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)
Is there a way in an Android Activity to do a setContentView(), so that I can have Android compute the layout, and so I can successfully get Views in it via findViewByID(), but not yet display it?
This would be in an app with a main activity and some subordinate activities and the one I want to start but not display would be one of the subordinate activities. (in other words the main view would already be filling up the screen, so it would be sufficient to simply keep the new one hidden at the bottom of the view hierarchy). The activity would be started with a "standard" launch mode.
If there's a way to do it by keeping it at the bottom of the View hierarchy, how would I force it to the top when I do want to display it?
NOTE: This app already exists - it's a large, complex industrial app with 14 Activities, written for Android 2.35 and 2.36, so re-architecting it to use Fragments instead of Activities would be impractical. I just want to modify one Activity to not display, or to just display at the bottom of the View hierarchy so it's not visible.
This would be in an app with a main activity and some subordinate activities and the one I want to start but not display would be one of the subordinate activities
That is not possible. Or, more accurately, you are welcome to start that activity and not populate its UI, but it will still take over the screen, and so the user will be presented with a blank screen, which is not especially useful.
in other words the main view would already be filling up the screen, so it would be sufficient to simply keep the new one hidden at the bottom of the view hierarchy
Each activity has its own view hierarchy. A "subordinate activity" cannot and will not be at the top, bottom, or anywhere else with respect to some other activity's view hierarchy.
Im quite new to Android but I think that this can bea easily done using fragments. One fragment will be the one that will be currently shown and the other will be overrlayed with a hiden parent view for an example. When you need to show the other fragment just set the layout to visible.
Hope it works, Good luck.
If you want to do this without using Fragments which I suggest you to use them you can use LayoutInflater to inflate whatever layout you like and change between them using setContentView repeatedly.
View layout1 = LayoutInflater.from(this).inflate(/*your first layout */ R.layout.activity_layout_1, getWindow().getDecorView());
//here you can use findViewByID like this
View someViewInLayout1 = layout1.findViewByID(R.id.some_view);
//... get fields for all others views you need here in layout1
//than you inflate your other layout
View layout2 = LayoutInflater.from(this).inflate(/*your second layout */ R.layout.activity_layout_2, getWindow().getDecorView());
//... do the same for layout2
Now you can call setContentView whenever you want to change between layouts.
setContentView(layout1 /*or layout2*/);
Anytime one calls setContentView() multiple times, previous views get destroyed. How does one create a view that will exist, as an overlay or under the main window, despite multiple calls to setContentView()?
OK, I think I'll answer myself: After some reading I believe I shouldn't be calling setContentView multiple times.
Instead I should use one layout that has the view I wanted to exist despite layout changes and also put ViewFlipper and use that for layout changes not setContentView.
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.