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 9056213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:08:45+00:00 2026-06-16T14:08:45+00:00

the title pretty much explains it. I have horizontal scrolling set up, the first

  • 0

the title pretty much explains it.

I have horizontal scrolling set up, the first screen has buttons to the other Fragments as well as the whole horizontal scroll system. What I would like is for the user to be able to press the back button when on one of these fragments and for the app to return to the first screen with all the buttons.

From there I want the back button to be an AlertDialog asking the user if they would like to exit the app. At the moment this is what is happening (On all Fragments when you press the back button the AlertDialog I created pops up).

I’ve looked into Fragment transactions and “addToBackStack()” but I don’t know how to implement it. I’ve looked at the dev guide and certain questions on this site but getting one or two lines of code doesn’t help in implementing it.

I have a FragmentActivity with a FragmentPagerAdapter set up and each Fragment has its own Java file. I have 5 Fragments that are all called in the FragmentActivity and FragmentPagerAdapter.

I don’t think I need to show you guys any of my code for the moment since it’s all set up in the normal manner. Please let me know if you do though.

The bit of code I found on other questions and one in particular was the following:

FragmentTransaction tx = fragmentManager.beginTransation();
tx.replace( R.id.fragment, new MyFragment() ).addToBackStack( "tag" ).commit();

It’s a bit hard to go on just that though.

I would really appreciate your help.

EDIT: my code removed – wasn’t needed.

  • 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-16T14:08:47+00:00Added an answer on June 16, 2026 at 2:08 pm

    If you use the ViewPager from your question which I answered earlier and you want to come back to the first fragment of the ViewPager when the user presses the BACK button then override the onBackPressed method like this:

    @Override
    public void onBackPressed() {
        if (getSupportFragmentManager().findFragmentByTag("outDialog") != null
                && ((DialogFragment) getSupportFragmentManager()
                        .findFragmentByTag("outDialog")).isVisible()) {
            // we have the out dialog visible and the user clicked back so let
            // the
            // normal events happen
            super.onBackPressed();
            return;
        }
        int currentPosition = mViewPager.getCurrentItem();
        if (currentPosition != 0) {
            // if the page the ViewPager shows isn't the first one then move it
            // to the first one
            mViewPager.setCurrentItem(0);
        } else {
            // we are at the first position already and the user wants out, so
            // annoy him with a dialog that asks him once again if he wants out.
            DialogFragment askHim = new DialogFragment();
            askHim.show(getSupportFragmentManager(), "outDialog");
            // in the dialog listener, if the user presses ok, finish the activity
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The title pretty much explains the question. I have a user control loaded into
The title pretty much explains the issue. I have a button that when I
Title explains it pretty much. After installing Boost module, i have troubles with uploading
Well the title explains pretty much everything. I want to use something like p
The title pretty much explains it all, I have a Member object that references
The title pretty much explains it all, I have a Member object that references
Pretty much the title explains what I try to achieve. Having a set of
The title pretty much explains the meat of the question. I'm debating on starting
The title pretty much says it all, but basically I have a main parent
Title pretty much explains it all, its the last thing I'm trying to work

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.