Add a new fragment to ViewPager within a FragmentActivity - java

I have spent the better part of two days searching for an answer that solves my question, I have found plenty that come close but not specific to what I need:
I would like to add a new FragmentActivity to a viewPager from inside a current FragmentActivity by calling the FragmentPageAdapter (or at least, this is how I see I need to solve my problem).
For example: In fragment 1 I would like to press a button to update and replace Fragment 2 with new data, and then set Fragment 2 as the currently viewed fragment.

What you could do is implementing a custom FragmentPagerAdapter or FragmentStatePagerAdapter (If you have lots of more complex fragments).
Documentation: http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html
The Adapter handles your contents so whatever calls or operations you wanna make on a certain fragment could be done quite easy from there.
You could pass the adapter itself to every fragment that its holding or just implement listener interfaces so that the fragments can trigger the changes through the adapter.
I hope this helps you out if its still an issue!

Related

How to add / remove tabs from inside a fragment (AppCompat)

I am having a really tough time with one small issue in my AppCompat activity with tabs. I am using the Android Design Support Library, and have implemented a tabbed application with fragments.
Now, I have no problem creating tabs & fragments in the Activity's onCreate() method, but I cannot for the life of me find if it is possible to add tabs programmatically from within a fragment.
For reference, all of my tabs use the same fragment (OneFragment.java). I have tried using FragmentManager / FragmentTransaction, but although this creates a fragment (I think!), it does not make a tab.
I have also tried to add a tab to the FragmentPagerAdapter and set the Tab Layout's adapter again, but this also seems to do nothing in the UI. Any help would be much appreciated!
I have also tried to add a tab to the FragmentPagerAdapter and set the Tab Layout's adapter again, but this also seems to do nothing in the UI
If you managed to add a tab to the FragmentPagerAdapter but the UI didn't change then you probably forgot to call the notifyDataSetChanged() method on it.

Architecture for a drawer layout app

I am building an app with a drawer layout similar to the Android Facebook app. I am wondering what the best method for architecture is. Should I have a main activity which is responsible for the action bar, and then have it use fragments to display the content of each menu item, or should I be using one activity to manage the action bar, and then have each menu item kick off entirely separate activities?
I could also imagine building multiple activities, which each have to manage the action bar. This option seems the worst.
You have two architecture options here
MainActivity with Fragments
ParentActivity that handles drawer and lots of Activities that extends this Activity.
I have tried both in different projects and found some things worth sharing.
For me The MainActivity that handles drawer and then using Fragments to fill the display is the best.
You will need to handle callbacks from specific Fragments in your MainActivity and redirect them to the specific Fragment they came from. This is mainly if you use Interfaces in objects lower in the Arcitecture chain since you sometimes need to pass down Activity to certain objects. This generates more code that are not as generic as one might want in top level architecture node.
If you are using a ParentActivity and extending it for each ChildActivity, you can write all specific code in the child, meaning that the toplevel ParentActivity will almost only have generic code.
If you are using the ParentActivity with ChildActivities and you are switching between Activities, you fill get the graphic when an Activity closes and the next opens every time a user switches between navigation objects. If you use Fragments this wont happen as the Fragment will be switched in the background. The user will also experience that the navigation drawer will be closed and recreated each time he clicks on an item there.
Its also unnessecary to recreate the navigation drawer with each click on an item. This is a minus for the ParentActivity approach.
With the ParentActivity approach you will also have to keep track of how the backbutton should function, this will be autoaticly handled for you with Fragments. Also when starting new Activities you have to choose if a new Activity should be created or if the old should be killed etc.
Just my 5c, hopes it helps :)
The best way is to use one Activity with one Fragment per section/view.
Take a look at the design documentation.
Also see the Tutorial and Sample Application. It's fairly straight-forward.
You will have one activity which manages ActionBar, Drawer (ListView!) and Fragment.
Every time it clicks an item in the ListView it updates the fragment with the new view.
If you use different Activities then you should use intents with a very bad effect, use a different activity only if needed (if it's totally unrelated to the current activity maybe?)
Official documentation: http://developer.android.com/training/implementing-navigation/nav-drawer.html
If you got any problem in creating this, online you can found more tutorials but the official is very great.
You should have the activity holding the actionbar & drawer
When using a drawer you should not start new activities from within the drawer but fragment instead
Good post & video about it: https://plus.google.com/u/0/+RomanNurik/posts/3nMVVQzUTjG
another good read: http://www.androiduipatterns.com/2013/05/the-new-navigation-drawer-pattern.html
And finally this is a must see also (check the slides or the video): https://plus.google.com/u/0/+NickButcher/posts/1jeyV2n1ZpM

How to manage activity inheritance in Android Development

This is a relatively general question that I have regarding to Android development.
In the Android application, I am using SlidingMenu library. Imagine the activity I am trying to implement has a Navigation Drawer (from Sliding Menu Library) and action bar tabs with View Pager and contains different fragments.
In order to have the navigation drawer in the activity, I had to inherit the application from SlidingActivity like this:
public class ActivityMain extends SlidingActivity implements TabListener {
However, to make View Pager work in this activity, I will need to use Make a FragmentPagerAdapter instance, and if I want to use it, it requires the activity extend the Fragment Activity.
My Activity already extended SlidingActivity, so there is no way to extend another super class. I am not sure what will be a proper way to solve this conflict. When I was working on the Android app, I have saw some other cases that different components in one activity requires to extend from different super class. What will be the general solution to such problem?
Thank you
If you look at the package of SlidingMenu here, they have an activity called SlidingFragmentActivity. Extend this activity instead of SlidingActivity gives you everything you need.
You can wrap your Activities in a SlidingMenu by constructing it programmatically new SlidingMenu(Context context) and then calling SlidingMenu.attachToActivity(Activity activity, SlidingMenu.SLIDING_WINDOW | SlidingMenu.SLIDING_CONTENT). SLIDING_WINDOW will include the Title/ActionBar in the content section of the SlidingMenu, while SLIDING_CONTENT does not. You can check it out in the example app AttachExample Activity.
from https://github.com/jfeinstein10/SlidingMenu

How to use Fragments in Android properly

I asked a question a while back:
Replacing Fragments isn't working/Am I executing this the proper way?
Because I was having a really tough time wrapping my head around the entire concept. Past Google I/O videos have also helped, but I still didn't really have a reason to use fragments. I have encountered my problem again, and with more searching and Google I/O 2013 videos, I've hit another fork in the road. After starting a bounty to get a more expert opinion on this subject, I was guided to use FrameLayouts, yet in this I/O 13 video at 19:05 into the video it shows <fragment>. Can someone clear up any discrepencies or misconceptions I have?
Do I use <fragment> or <frameLayout>? Bonus points if you look at my original question to know where I'm coming from.
There are 2 seperate questions I see in here; (1) what is the <fragment> element? (2) am I (meaning you) doing what I tried to do in my previous question in the best way possible by using a FrameLayout?.
I will try to answer both.
The <fragment> XML tag is just another way to implement a fragment. As a refresher, a Fragment is like a part of an activity. It is a stand alone class, however, referring back to the fragment documentation, because a Fragment is intended to be modular, all fragments must be housed within a seperate FragmentActivity. Think of this as FragmentActivity as a container. By loading your Fragment into the FragmentActivity the FragmentActivity provides other valuable functions that the Fragment class does not have access to on its own. It also offers you the chance to swap out fragments dynamicly, allowing for a more flexable user experience.
How you load your Fragment into its containing FragmentActivity depends on what you are trying to acomplish. One way to do this is to use the <fragment> tag. With this approach, rather than declaring the Fragment in the activity java code and then loading it with a FragmentTransaction as you did in your earlier question, the Fragment class is loaded via the XML layout for the containing FragmentActivity.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
android:name="com.example.android.fragments.HeadlinesFragment"
android:id="#+id/headlines_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
You can see the fragment declared here in the example taken from the android docs. The fragment is then loaded by the FragmentActivty when setContentView() is called onCreate.
public class MainActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_articles);
}
}
The other way to display fragments is as you have done it in your prior example. Using a FragmentManager you can create a new instance of your Fragment class and load it programatically within the java code.
So which technique makes sense when? As the example from the android docs demonstraits, the XML fragment method makes sense when you are using stationary fragments, i.e. you are not dynamically swapping fragments in and out. This is because you cannot edit the XML layout you create on the fly the same way you can the java code. Also, from what I can tell, using the XML approach treats the fragments more like layout elements alowing for different control. In the android example, they use the XML approach to display 2 fragmens side by side on a tablet. By controling the fragments more like layout elements, e.g. having the ability to adjust layout_weight you can achieve a better result for that purpose.
However, if you are designing a highly dynamic UX, say with a ViewPager or some other feature where fragments will be rotated regularly, it makes more sense to use seperate Fragment classes and replace those Fragments as they become necessary.
Based on your prior question and the need to swap fragments dynamicly I think you made the right choice for that implementation.

How to use Android Fragments?

I'm looking at some demo code that shows how to use a Fragment Adapter (Tab Adapter in this case). I'm curious as to what exactly the instantiate() method does. I see it used in the following demo code on this page:
http://developer.android.com/reference/android/support/v4/view/ViewPager.html
(see the getItem() method within the TabsAdapter class)
If I'm reading the demo code correctly, every time a user clicks on one of the tabs, a new Fragment is created? And thus the fragment starts the entire life-cycle again (onAttach()...onCreate()... etc)? This sounds awfully inefficient. I would think that the fragment that will represent the content for each tab should be instantiated only once (perhaps in the addTab() method), and then saved into some collection where it can be fetched when getItem() is called.
Please correct me if I'm mistaken in any of this. I'm trying to better understand how to manage fragments.
My money would be on that the setCurrentItem() function doesn't actually destroy the existing Fragment being shown in that tab. Otherwise there's really not much of a reason for the adapter to have a List of available tabs. Likely, when you switch from one tab to another, setCurrentItem() just detaches the UI from the currently active Fragment (or calls its onPause() method) and then re-attaches the UI for the newly selected Fragment (or calls its onResume() method).
But, if you're in doubt, you could read the source :)
Hope it helps,
David
I was able to find an explanation for my question here

Categories

Resources