-1

I'm still early in the Android Development, I want to make an App which have many Fragments.

First, I have watched Eric Burke's presentation about Android Anatomy, which said that a Fragment couldn't replace Activity. There's no way you could make an app with only one Activity and many Fragments. Even if it could be done, the data flow would be a mess.

So, I know that in API < 7 we could use TabActivity, ActivityGroup, TabHost to navigate through the Activity. So here's what I'm asking.

Could I make some navigation (ex. Tabs/ActionBar) that always show even when I switch the Activity, without using deprecated method ? Even the ActionBar Navigation mode is deprecated for API 21.

So maybe, could anyone give me some suggestion to make a navigation without using Tabs.

Adinia
  • 3,722
  • 5
  • 40
  • 58
Gabriel Juan
  • 77
  • 1
  • 8
  • 1
    **"There's no way you could make an app with only one activity and many fragments."** : Why not? The whole point of Fragments is they are modular and can be interchangeable. **"Even if it could be done, the data flow would be a mess."** : Not if you design your app and Activity / Fragments correctly. How many Fragments are you expecting to need? – Squonk Dec 19 '14 at 10:04
  • Have to agree with @Squonk – Carnal Dec 19 '14 at 10:06
  • Example of having navigation without tabs would be using ViewPager. You can swipe between fragments and even implement "tab like" functionality if you use the correct constructs next to your ViewPager. Whilst having a full blown app with only 1 activity would be difficult, you should aim to have very little logic in your activities and shift most/all your logic to fragments. They're much more flexible. – kha Dec 19 '14 at 10:30
  • @kha : **"...you should aim to have very little logic in your activities..."** : Sorry, but no. Fragments should have their own logic but all Fragments MUST be attached to an Activity in order for them to exist / function. Any communication between Fragments should be don via the Activity they're attached to - Fragments should be modular and self-contained. The Activity should "know" everything about all Fragments it hosts but no Fragment should ever know about any other Fragment. A Fragment should be able to handle all of its key functions but the main logic should normally be in the Activity – Squonk Dec 19 '14 at 17:10
  • @Squonk I believe that's a matter of preference. I prefer to have most of the logic in my fragments. This way I can make sure I can reuse the fragment in different places without relying on any activity to facilitate their actions. If there's a need to communicate between fragments (which I try to avoid if I can), I still would prefer doing so using interfaces (due to lack of "events" in Java) which may or may not be passed in to the fragment. I avoid relying on activities to give my fragments "meaning". How I see fragments is widgets that do one thing well and are independently functional. – kha Dec 20 '14 at 11:02
  • [cont] and I see the activity as a blank canvas, hosting these fragments. On its own, the activity decides which fragments should be present in the layout and how they should be placed but beyond that, I don't rely much on the activity. Again, this is mostly due to my lack of preference for having chatty fragments and the fact my use-cases are mostly tailored around the fact that my fragments are fully independent and don't rely on other fragments to provide them with added functionality. Regardless, whist your points are completely valid, they do not invalidate my points in my view. – kha Dec 20 '14 at 11:05
  • @kha : If you re-read my comment, I say "Fragments should have their own logic..." - I'm not arguing against that point. In fact I regularly make comments on Stack Overflow questions / answers pointing out that Fragments *SHOULD* be self-contained, modular and re-usable. I'm also not disagreeing about using interfaces for whenever a Fragment needs to communicate outside of its own little world. My point was primarily that all Fragments must be attached to an Activity in some way and the Activity class has mor 'intelligence' and capability than a Fragment. Sure, keep Fragments self-contained... – Squonk Dec 20 '14 at 13:04
  • @kha : ...but without an Activity (or Activities) to provide overall control and structure, then Fragments are non-functional and as such, Activities should have sufficient logic to form the main back-bone of the UI framework, decide how they use Fragments in multi-pane or single pane layouts and how various events / actions are handled. – Squonk Dec 20 '14 at 13:08
  • 1
    @Squonk Fully agree with all your comments. To me, having `sufficient loic to form the main back-bone of the UI and how to use fragments in different situations` constitutes as very little logic :). The main (and often far more complex) logic generally resides in the fragment. What I initially commented on which you disagreed with was perhaps badly worded. I didn't mean you should not have any logic in your activities. Fragment on its own does not work anyway. What I meant was to have the real logic in fragments and the plumbing logic (which is not really business logic) in the activity. – kha Dec 20 '14 at 13:15
  • 1
    @kha : Agreed - perhaps I misunderstood your original comment and, in turn, you misunderstood my response. I think we both agree - Fragments should "know" how to do everything they need to do and Activities should do any "heavy lifting" if necessary. :) – Squonk Dec 20 '14 at 13:41
  • Okay, sorry to leave this post way too long, I don't have much time to use internet these days, so, THANKS A LOT for your comments, it helps me a lot, about how am I should see the fragment and activity. So, here's the next question.. If you guys using LINE messenger, or facebook, or facebook messenger, they have tabs right? Do you guys think, that all are fragments? Sorry for my bad english.. – Gabriel Juan Dec 20 '14 at 18:54
  • Okay, just to make things clear. I want to make an app, a mall directory app. So, I need 5 tabs, each tab with 2 views, the main view, and the detail view (either fragment or activity, some are list view, some are pager view). For example, in tab 2, there will be mall tenants (listview), when you click one of the item, it will pop up the detail of the tenant. On the other tab, let's say we have a promotion tabs, which come with same view as tab 2 (listview), if we click one of the item, it will bring us to the detail view. – Gabriel Juan Dec 20 '14 at 19:07
  • [cont] So, what I want from the app is, all the navigation will be done using tabs, and the action bar/navigation tab will not move even if I click the list item and bring us to the detail view. So, should I create 1 activity with all the fragments, or 3 activity (main menu, promotion, tenants) with 2 fragment for each activity ( I have 2 pages menu that will come with view pager)? I really appreciate it a lot for answering my question. Really, so much thanks! – Gabriel Juan Dec 20 '14 at 19:11

3 Answers3

0

As I also agree that you better don't create an app with only one activity and many fragments on top of it, maybe you want to check out the ViewPager in combination with a PagerAdapter. Hereby you can swipe between fragments without the need of Tabs. But the ViewPager can be combined with tabs as well.

Hookah_Smoka
  • 374
  • 1
  • 2
0

I've got an app which uses only one activity and several fragment. Switching between the fragments with events like pressing buttons on the action bar or completing background processes.

Action bar example with two icons "info" and scan:

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {    
        FragmentManager fm1 = getFragmentManager();
        FragmentTransaction ft1 = fm1.beginTransaction();
        MainFragment mainFragment = (MainFragment) getFragmentManager().findFragmentByTag("Main_Fragment");


        switch (item.getItemId()) {
            case R.id.action_info:
                if (fm1.findFragmentByTag("Info_Fragment") == null) {        //Check if Fragment is already open
                    ft1.setCustomAnimations(R.anim.enter, R.anim.exit, R.anim.back_enter, R.anim.back_exit);
                    ft1.replace(R.id.content_layout, new InfoFragment(), "Info_Fragment")
                            .addToBackStack(null)
                            .commit();
                }
                return true;

            case R.id.action_scan:       
                    if (fm1.findFragmentByTag("Scan_Fragment") == null) {        //Check if Fragment is already open
                    ft1.setCustomAnimations(R.anim.enter, R.anim.exit, R.anim.back_enter, R.anim.back_exit);
                    ft1.replace(R.id.content_layout, new InfoFragment(), "Scan_Fragment")
                            .addToBackStack(null)
                            .commit();
              }
                return true;
        }
    }

Communicate with the main activity over BroadcastRecievers

Wuuzzaa
  • 43
  • 6
0

I'm answering the question you raised in your comments (and apologies for the delay).

Your question was:

Okay, just to make things clear. I want to make an app, a mall directory app. So, I need 5 tabs, each tab with 2 views, the main view, and the detail view (either fragment or activity, some are list view, some are pager view). For example, in tab 2, there will be mall tenants (listview), when you click one of the item, it will pop up the detail of the tenant. On the other tab, let's say we have a promotion tabs, which come with same view as tab 2 (listview), if we click one of the item, it will bring us to the detail view.

So, what I want from the app is, all the navigation will be done using tabs, and the action bar/navigation tab will not move even if I click the list item and bring us to the detail view. So, should I create 1 activity with all the fragments, or 3 activity (main menu, promotion, tenants) with 2 fragment for each activity ( I have 2 pages menu that will come with view pager)? I really appreciate it a lot for answering my question. Really, so much thanks

This is the important requirement you have: the action bar/navigation tab will not move even if I click the list item and bring us to the detail view.

You can't achieve this with having more than 1 activity. Remember, you can only have 1 activity active (no pun intended) at any given time. This means, your activities will host their own version of the action bar/navigation tab. This in turn means, if you have more than one activity, when you transition between them, your action bar will inevitably change. You can try to get around it by having the same exact action bar on all your activities but that's not changing this argument.

Your app seems simple enough to do with 1 activity and 2 fragments -- 1 for each screen/tab which hosts the list view (and your detail view is probably a dialog since you want it to 'pop up').

It's only when you start adding more and more functionalities that the value of adding multiple activities become apparent. I very recently (a few hours ago) answered another question about fragments and activities. I think it maybe helpful to you to read: Why not always use fragments with one activity

If you have any questions, please let me know but do add an @kha so that I get a notification and can answer you quicker.

Community
  • 1
  • 1
kha
  • 19,123
  • 9
  • 34
  • 67
  • sorry for taking too long for answering. I've been on holiday. So, would you care to tell me how to add users? And by the way. Yeah, my apps will be 5 tabs, with each tabs have 2 fragments. Do you have any email or something so I can ask you if I need some troubleshooting? At the end, I'm using God Activity that taking cares of it's fragments. I'll try using this method for a while. When it comes very complicated, maybe I will split the apps by its flow. Your answer on another question is very helpful. Thanks ! – Gabriel Juan Jan 05 '15 at 11:18