How to initialize a fragment based app? - android

Can anyone help me with finding the entry point into a fragment based app? I am having an app with multiple activites. For small resolution devices one of he activities is loaded at startup, in this case a menu. This activity is declared as "launcher" in the manifest.
On a high resolution device I do not want to display the menu but all other activities should be displayed as fragments at the same time. For this I have declared a new layout in a layout-w1000dp-h720dp folder.
However from the manifest file it is still the menu which is loaded at startup and not this new layout with multiple fragments. So my app crashes at startup because it is trying to launch the menu but with the new layout with multiple fragments...
What am I doing wrong? Wow can the manifest file work together with this new layout?
I have attached an image how the app should look on a large screen device with the four fragments/activities.

Could you post the exact error you're getting? The entry point for your app would be the Activity. Since your app is based around fragments, you could have one Activity that holds the four Fragments you displayed above. Your Manifest doesn't actually need anything extra to indicate that. I'm also guessing that you have XML layouts premade for the Fragments. If so, go to the layout of your Activity and specify the four fragments.
You would launch a single Activity, which then initializes your Fragments, and adds them in a FragmentTransaction.

Related

Android Studio- starts with 2 fragments

I searched through the site but could not find an answer so here is my question.
I am using Android Studio 3.6 and when I start a new project and choose Basic Activity there are two fragments appearing in layout. So in layout there are .xml files activity_main,content_main,fragment_first and fragment_second. Therefore, when I start a new project, I get a different screen than the usual "hello world" screen. To add, I cannot basically edit the fragments. So, how can I back to "normal" and start without these fragments?
When you create a basic activity, you basically create an activity with two fragments. this means that basic activity by default provides two fragments that you can edit and use. basically, it's almost the same with bottom navigation activity. the difference is only in basic activity there is no bottom navigation.
if you want to make one activity app. you don't really need to use fragments. so you can just make a Empty activity instead of basic activity. if you want to make your activity fragmented in the future, you can add it later.

Is it necessary to use fragment in the activity?

Recently learning android-architecture-samples, I found that each activity uses fragment, even if the activity doesn't really need fragments, in my opinion, this makes the activity lazy and leads to a lot of files.
For example, TaskDetailActivity sets the Toolbar, and other view work is given to the TaskDetailFragment.why use a fragment instead of the activity to achieve, you will find that the entire demo is doing this,
I don't think this is a meaningless question. There must be reason to do so.
Can anyone tell me why I wrote this, what are the benefits? Thank you!
No. You can have activity without a fragment.
So why Google and other Android developer advocates are pushing for a single-activity-multiple-fragments architecture?
It's because, this way you can have multiple screens (fragments) share the one single activity which otherwise would be so hard to manage as many number of activities as number of screens in your app. Fragments can easily be added and popped from your activity's fragment backstack so there's a flexibility of sharing resources from a same activity with multiple fragments. Also, if you don't care about differences in the different screens in terms of configuration such as orientation etc. , It's much easier to manage multiple screens in one single container.
From my experience you should create different activities for different workflows. E.g. your whole app (core features of your app) can sit in one activity however other things like sign up, login, onboarding can be activities of their own. This way you can start one single workflow by starting a new activity.
Another good example for choosing activity over a fragment when you are adding a new screen - Your new screen has completely different configuration than other screens (fragments) that are shared within your current activity. E.g. your new screen is a photo / video gallery that requires you to support both the orientations (landscape and portrait) but your other screens in your existing / current activity can only be portrait.
I hope you have a good clarity on why people are leaning toward single-activity-multiple-fragments approach.
There is no rule like that. fragments not must for activity it based on requirement.

Android activity and fragments, best practice

I'm about to make my first Android application and I am currently reading about activities and fragments. I intend to use the Lollipop navigation drawer feature to load different screens in my application for different features. Like settings, About, Add new x, browse x, etc. Should my nav drawer be loading different activities for each item click or loading a new fragment?
All my screens will have the same style associated with them, but will have obviously much different content.
If I load a new activity, do I "lose" the nav drawer to the side? or is it always present as I want it to be accessible from any screen in the application.
I am quite unfamiliar with the Android system so far, but no matter what I choose do I need to use an intent to launch either of these. A real lay mans explanation would be greatly appreciated.
Thanks
EDIT: Within one of my screens I hope to use a tab system and have it change.
This is a mockup I have designed, as you can see I would like the nav drawer to be used from this screen but also allow the switching of 3 different tabs, within the Add new timetable screen.
How would I go about having a tab at the top and then 3 different (linked) screens under it. This would be the deepest level I would go. Every other screen would just be one screen, no tabs.
Activity typically takes the whole screen, so yes, if you launch an Activity, you will temporarily "lose" everything that's placed in other activities.
Fragments, on the other hand, can be stuffed to smaller areas, and you can have several fragments on the screen at once.

Activities vs Fragments

I'm trying to design an application where the main mode of navigation through the application's several primary screens uses Tabs. I've basically copied the approach shown in the tutorial here: http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/
However, on some of these screens there are clickable elements that are supposed to kick off a full-screen page (the tabs should no longer at the top of the page). I've been able to get this working by creating a new activity for the new screen, but I believe I want to use a fragment for the new screen because in a tablet layout, I want the new screen to appear beside the main screen rather than replacing it. I haven't been successful doing this. If anyone can give examples or pointers on how to get a single activity to run fragments with tab-based navigation and other fragments that that don't have the tab navigation.
In a similar vein, I also have a settings screen accessible from the Options Menu. Are settings screens like this typically done as activities or fragments?
Thanks in advance.
You have to design your application in two ways.
Firstly you should know if you are running big device (tablet) or smaller one (smartphone).
You could make that by preparing two different layouts, one layout in layouts resource folder for smartphone and second layout in layout-xlarge resource folder for bigger devices.
In smaller layout make only one container for one fragment (list fragment for example). In bigger layout in layout-xlarge folder you coul have two containers one for fragment with list and second for fragment with content.
In onCreate method you could see if you are using multi pane layout or not.
If your secod pane in bigger layout is for example R.id.right_panel. You just make findViewById(R.id.right_panel) and if this is null, you know that you are not running multi pane.
Now if you are in multi pane, pressing item on the list should notify activity, and activity should change/replace right fragment with another one.
If you are not in multi pane, pressing item on the list should open new activity with only one fragment of content.
There are many tutorials of this of simmilar solutions like link or link
Regards

Modifying application workflow to use TabActivity

This question actually has two parts.
The first part:
I've been developing my first app for a couple of weeks now. I have 5 screens and everything seems well. However, I'm considering changing the app's navigation to a TabView.
I haven't delved much into it, but I'm hoping someone can save me a little bit of time. It seems that people don't generally place Activities inside each tab. They simply point the tab content to a View. This is where my major setbacks are. 1) I already have Activity classes full of code and 2) I can't quickly guess how the structure of an app using TabView looks. For example, where do I put the handler code for clicking a button on a View? Does it all just get dumped into the TabView Activity somehow?
What I would like is if you could please give me a quick synopsis of what I'm looking at doing, answers to any questions you think I may have, and point me toward some resources for creating TabView applications. A quick Google search really just shows me how to create a TabView Activity and add a couple tabs to it. The code doesn't go any deeper. For example, say I have a layout xml to show in one of my tab's content pane, where does the code go for clicking a button I have in that layout?
The second part:
I've added a TabActivity to wrap the Activities I currently have in. At the moment I have Activities populating the content of my tabs (though ultimately I'd like to do this in the most efficient fashion, which doesn't seem to be having Activities be tab content). I've noticed something rather annoying. My MAIN Activity is an Activity I wrote for my user to log in to their account. After logging in, they are taken to my Tab Activity. Here is what happens:
When I am on my Tab Activity and I "minimize" the app by clicking the Home button and then launch it again, I don't get taken back to the Tab Activity. I get taken to my log in Activity. Why? I don't have the launchMode of my Tab Activity set to singleInstance... or is it singleInstance by default? How can I make the app re-launch showing the Tab Activity (ideally by setting some parameter, assuming I'm doing something wrong, and not having to save this data off somewhere and reading it and programmatically telling it what to go to)?
Thank you for all your time and help
I don't have a comment on the advisability avoiding the use of sub-activities in TabActivity. As for handlers -- if you aren't going to embed views instead of activities, then all the android:onclick type handler settings in your layout XML will call methods on the TabActivity. This is because they go to methods on the views' Context, which is the generally the nearest containing Activity. If you want to split your code up further without using Activities, I believe you'll have to use findViewById calls on the tab content views after you've set them up, and bind the handlers manually from there in your code.

Categories

Resources