I would like to use tabs in my Android application and since TabActivity is deprecated, I want to do it the Fragment way.
I already constructed the skeleton of the app, and I have 3 tabs, and each the tab has its own seperate Fragment.
My questions are:
I ended up having 1 Activity for the whole app and a lot of fragments. Is this normal?
In the fragments I have buttons. Each onClick method of these buttons expect to has its handler in the Activity. I've found this post: Handling onClick events with fragments. Seriously?? The Activity will catch all the onclick's events from all the fragments and call the appropriate method of the relevant fragment?? Can't I handle the onclick event of a button that located inside a fragment in the fragment's code?? If I can't, Google should get some spanks!
Thanks in advance!
The answer to question number 2 is to implement onClick in the code of the Fragment... No way of doing this in the XML?
Related
I used to have 3 activities with 3 layouts.
1. HomeActivity.java (activity_home.xml)
2. HelpActivity.java (activity_help.xml)
3. SettingsActivity.java (activity_settings.xml)
Whenever i had to open other activities, i used animated slide-in-out intents using overridePendingIntent. In this way, all the variables of HomeActivity stayed in itself and HelpActivity's variables/methods were in itself and same for SettingsActivity. I used onCreate() in these to perform some activity specific code. and, android:onClick="fetchSarcasm" from activity_home.xml was calling specified method in HomeActivity.java. So, there were no conflicts. All the normal things, that happen in an AndroidProject with 3 activities and used intents to switch to one another, were happening as i wanted.
But now, I wanted to migrate to ViewPager instead of slide-in-out intent transitions. In this way, there will be only one Activity with an XML of ViewPager. This ViewPager and its FragmentPagerAdapter gets the Pages from 3 classes extending Fragment with relative XML layouts.
I want to ask: Where do i write my Page Specific Code for onCreate(), layoutView's onClick="" and for onChangeListeners. Because, 3 classes extending Fragment do not have any Context or findViewById() and they do not respond to android:onClick="method" in different layouts.
Do i need to mix all that code of three activities into one containing ViewPager? If so, this makes ViewPager activity pretty heavy. and, writing if(page=2){ dothis(); } seems absurd for all pages at every code point. Any checkbox(view) that is in page 3, becomes null in other pages.
I know this might be a stupid question. I have spent almost 4 days trying to achieve something but i can't. Also, this is just my 2nd month in android, so i am new. I do not use Action Bars, Tabbed Bars or Navigation Drawers.
I am available on SO Chat too if you want to ask something more.
I just want to know how do i merge all the work i did when I was not using ViewPager.
If you're only reading the Preferences once for each Fragment, you can move your previous Activity.onCreate() logic to each Fragments onViewCreated() or onCreateView(). Read your shared Preferences there and set your Checkboxes accordingly.
Fragments inside a ViewPager aren't necessarily recreated on each page change. You can change how many Fragments are instantiated at a time with ViewPager.setOffscreenPageLimit(int).
Fragment Lifecycles explained
I have been following the Udacity's android app development course.
Sometimes they add the onOptionsItemSelected() method inside the fragment and sometimes they add it inside the parent activity.
I am a beginner to android development.
Can someone clarify that when are we supposed to add the onOptionsItemSelected() method inside the fragment and when inside the activity which contains the fragment ?
Also, it would be great if someone could give an intuitive explanation of a Fragment (how is it bound to the parent activity inside which it is present)
In android option menus can be added to both Activity and Fragment. So that Activity and Fragments can have their own option menu's and their own callbacks..
To your first question. A fragment represents a specific portion of your application, so if it's important to have a new menu item present while this portion is visible, you handle the item in the fragment. If a menu item should always be accessible you manage this item in the activity.
Not sure if I got your second question, but I try to answer anyway. First of all the developer side concerning fragments is pretty long but pretty good too.
You can a reference of parent activity in a fragment by calling getActivity. That gives you the ability to make public calls like findViewById.
I'm beginner to android and currently I'm working on a small project.
In my project I have a fragment with text and imageview, and in my Main activity I have a button and imageview.
When I press the button in my activity class it opens up the fragment, but I want to animate(Move) the the imageview in main activity to the position of the imageview present in the fragment.
Is there any simple way of getting the position of the views present in the fragments to activity class?
I'm stuck in this situation from few hours. Please help me.
What you are attempting is difficult because that is not how you are supposed to do it. Fragments are supposed to be self contained units, completely modular and interchangeable. On the other hand Activities are just supposed to be empty containers for Fragments. All the logic and UI has to be contained in the Fragments them selves and the Activities are supposed to be used to arrange and display those Fragments. Nothing from outside a Fragment should have anything to do with something inside the Fragment. So if you restructure your app with that in mind you will find that everything will be MUCH simpler.
A few pointers:
Move all of the UI to the Fragment
It is completely fine to perform FragmentTransactions from inside a Fragment. You don't have to take a detour through the Activity.
Try to understand the difference between Fragments and Activities and don't let yourself be mislead by tutorials which don't adhere to this separation. Most Android tutorials on the internet are outdated and wrong. Refer to the official tutorials here.
I have some slight issues with my menu items for my fragments. I have an overarching activity that hosts the fragments. I'm unsure whether I should have my menu options code in every single fragment and not the activity or simply just the activity? What is better practice and what works best? One of my menu items is also affected by OnActivityResult and therefore that menu item doesn't work unless that method is posted into every fragment as well. This just seems like a lot of duplicated code. I would simply put everything in the main activity but that seems like bad practice and surely the main activity should have minimal code and the fragments should hold their own separate code. I need to use the OnCreateView method in my fragments in order to refresh an image adapter because the ViewGroup needs to be accessed. I'm just wondering what people's opinions are and what is best for simplicity of my code?
Thanks in advance.
It is alright to put your menu item in the activity's menu list when it semantically belongs to the activity, such as logging in and out. You can just start up some other activity with startActivityForResult and change the status of the menu item according to the result of onActivityResult.
In addition, fragments can also have onActivityResult method, so if you can separate the logic of startActivityForResult and onActivityResult into one of your fragments, it might be a good idea to do everything in that fragment.
Until now I've used TabHost for my App to create 3 Tabs. Each Tab is represented by an Activity in which I get the layout via setContentView(R.layout.something) from an XML file.
So 3 Tabs, 3 Activities and 3 XML files.
Now I've stumbled upon fragments, which are the new and better way to go, so here is my question.
Fragments handle the UI, so create 3 Fragments which are handled by FragmentPagerAdapter. Inside each Fragment I create the Content via XML files.
But where do I put all the code about which button does what, read from database or write to it etc. So far that's all been in each Activity that was loaded by the Tabhost.
Do I put all that code into the onCreate() etc. methods of each fragment or is there a better and cleaner way to do it?
The approach you propose is pretty much fine!
In terms of what to do where, I'd recommend that you do anything to create/alter the the UI of a page (fragment) in onCreateView() of each fragment, and any logic (reading databases etc) in onActivityCreated(). I'd recommend steering clear of onCreate() in a fragment, because this is called before it is associated with an Activity (preventing you doing thigs such as managed queries to contentproviders). Button click listeners could be defined either in onCreateView or onActivityCreated().
Anything more specific, let me know. Dont forget, standard Pagers don't include a row of tab titles/icons, but Google ViewPagerIndicator and you'll find a library which you can use to do this.