Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9115145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:21:39+00:00 2026-06-17T04:21:39+00:00

As far as I understand, every Fragment has its own backstack, and this is

  • 0

As far as I understand, every Fragment has its own backstack, and this is shared with all the fragments that belongs to the FragmentActivity. Suppose you have to manage multiple tabs, and every tab could navigate through multiple fragment. Suppose you want to “record” the navigation history for every tab so switching between fragments will allow you to return to the fragment you were viewing. Is it possible to achieve? Do I need to link every tab to an fragment activity? In this case how can be the switching between FragmentActivity managed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:21:41+00:00Added an answer on June 17, 2026 at 4:21 am

    There is no “standard” way of going about this, because this design style is discouraged. However, I found a way to make it work: You will design your navigation manually.

    Your application should have one Activity, a FragmentActivity. It has a FragmentTabHost that will hold each of your TabFragments.

    TabFragment is the abstract class I created to represent your tab in the TabSpec. It will manage the navigation and swapping of fragments WITHIN the tab.

    Then, the individual Fragments you create can be swapped within the TabFragment object. Here is the code:

    The Activity

        public class MainActivity extends FragmentActivity {
    
                private FragmentTabHost tabHost;
    
    //(TabFragment)s will set this property when created so the Activity can communicate with it
                public TabFragment activeFragment;
    
                @Override
                public void onCreate(Bundle savedInstanceState){
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.main);
    
            //create tabHost based on .xml layout
                    tabHost = (FragmentTabHost)findViewById(R.id.tabhost);
                    tabHost.setup(this, getSupportFragmentManager(), R.id.tabcontent);
    
            //add each of your tabs to the tabHost. These are all (TabFragment)s
                    tabHost.addTab(tabHost.newTabSpec("New Tab").setIndicator("New Tab"),
                          ExampleTabFragment.class, null);
                }
    
        /*override the onBackPressed method, so that your application does not close every 
    time the user navigates back. Instead, calls the activeFragment to determine behavior*/
                @Override
                public void onBackPressed() {
                    activeFragment.onBackPressed();
                }
    
        //method for TabFragment to call when the user navigates out of the app
                public void close(){
                    super.onBackPressed();
                }
            }
    

    TabFragment

        public abstract class TabFragment extends Fragment {
    
            @Override
            public void onResume(){
    
    //sets the property in the Activity so we can reference this from it.
                ((MainActivity) getActivity()).activeFragment=this;
                super.onResume();
            }
    
    //this will be the method called when the back button is pressed. It will navigate.
            public abstract void onBackPressed();
    
        }
    

    Instance of TabFragment
    Inside an instance of a TabFragment should be a FrameLayout to which child Fragments can be attached. The first time the tab is clicked, it will launch the Fragment specified in onCreate(). After switching back to it from another tab, it will resume whatever Fragment was last displayed. The onBackPressed() method should be used to navigate back through the fragments if hierarchical navigation is desired. I used a byte property (tabContentIndex) to determine how to navigate. The Fragments can swap themselves for other Fragments if you add a constructor that takes and instance of this TabFragment. They will do this by accessing the start(Example)Fragment() methods. Remember that the ‘back’ button must eventually exit the app.

    public class NewTrailTabContent extends TabFragment {
    
        //to determine what child Fragment is active
        byte tabContentIndex;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
    
    //a simple FrameLayout in this case. Child Fragments will be attached.
            return inflater.inflate(R.layout.example_fragment, container,
                    false);
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
    
    //The tab starts with this Fragment
            startDiologFragment();
            super.onCreate(savedInstanceState);
        }
    
        public void startExampleFragment(){
    
    /*Fragment's constructor allows us to reference the parent to navigate. In effect, this 
    Fragment will be able to call these navigation methods.*/
            ExampleFragment newFragment = new ExampleFragment(this);
            FragmentManager fragmentManager = getChildFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager
                    .beginTransaction();
    
    //this Resource is the FrameLayout
            fragmentTransaction.replace(R.id.example_contentpane,
                    newFragment);
            fragmentTransaction.commit();
    
    //this is set so the onBackPressed() method knows how to operate.
            tabContentIndex =0;
        }
    
        public void startTestFragment(){
    
            Fragment testFragment = new TestFragment(this);
            FragmentManager fragmentManager = getChildFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager
                    .beginTransaction();
            fragmentTransaction.replace(R.id.example_contentpane,
                    testFragment);
            fragmentTransaction.commit();
    
    //different contentIndex
            tabContentIndex = 1;
        }
    
    //this method called by the Activity
        @Override 
        public void onBackPressed() {
    
    //this will close the app because we are at the top of the hierarchy
            if(tabContentIndex==0){
                ((MainActivity)getActivity()).close();
    
    //this will switch to the original content fragment.
            }else if(tabContentIndex==1||tabContentIndex==2){
                startExampleFragment();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As far as I understand, every string is an object in Javascript. Still, it
I got an email today saying: In every case that we have examined, this
I understand so far that in Jquery, with html() function, we can convert HTML
Every article on async programming in ASP.NET i've read so far states that while
I thought that I understand id's vs classes in css documents. As far as
Today I've had a read about wordpress plugins, so far I understand that they
I have a code which has a start() function that executes every cycle. In
As far as I understand from the documentation the QUdpSocket are async but, still,
As far as I understand Facebook allowes you (through the API) to post on
As far as I understand, https is http plus SSL/TLS. What do I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.