Note that there are some explanatory texts on larger screens.

plurals
  1. POFor tabbed views, do I really have to put all my code into a single activity?
    text
    copied!<p>I am trying to make a simple UI with 3 tabs, each with their own distinct purpose. I understand that the new way to implement tabs is with Actionbar and fragments, but this is confusing to me, because in the end all the logic of my application is currently sitting in one rather big activity, whereas I wish more functional separation was encouraged by the platform. Is what I've done all wrong here?</p> <p>I have a lot in common with the gentleman posting here: <a href="https://stackoverflow.com/questions/9106063/android-layout-fragment-activity-confusion">Android Layout Fragment/Activity Confusion</a>.... It's unfortunate most of his questions weren't answered. </p> <p>... And, I got basically all of my understanding of how to do tabs with Actionbar from one person's example online, here: <a href="http://arvid-g.de/12/android-4-actionbar-with-tabs-example" rel="nofollow noreferrer">http://arvid-g.de/12/android-4-actionbar-with-tabs-example</a>. (There just aren't a lot of examples!)</p> <p>Here's getting the <code>Actionbar</code> and creating two tabs:</p> <pre><code>ActionBar actionbar = getActionBar(); actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); ActionBar.Tab calendarTab = actionbar.newTab().setText("Calendar"); ActionBar.Tab logTab = actionbar.newTab().setText("Sleep Log"); Fragment calendarFragment = new CalendarFrag(); Fragment sleepLogFragment = new LogFrag(); calendarTab.setTabListener(new MyTabListener(calendarFragment)); logTab.setTabListener(new MyTabListener(sleepLogFragment)); actionbar.addTab(logTab); actionbar.addTab(calendarTab); </code></pre> <p>Here's the entirety of the code that is inside these "subclasses" I've made of <code>Fragment</code>:</p> <pre><code>public class CalendarFrag extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.calendar, container, false); } } </code></pre> <p>Literally all I do is inflate a view; it just feels kind of wrong. And then I have a longer activity in which I have all the logic of my application, which is also annoying. Unfortunately, the way I have it set up I need to dynamically generate the content that goes into the views like <code>R.layout.calendar</code>, which I have to do <em>somewhere</em>. </p> <p>Now I'm running into lots of errors moving all of my code around, and I'm wondering if it's really worth it to sort it out--originally I was trying to do tabs with TabHost and TabWidget and all that, but it is deprecated. </p> <p>Am I really supposed to put all of my code in just one activity to fill three tabs? Isn't there a better way? Where should I put in the logic that dynamically fills these empty fragments? In the fragments themselves somehow, or in separate activities is what I'd prefer, I guess.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload