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

  • Home
  • SEARCH
  • 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 6732725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:41:04+00:00 2026-05-26T10:41:04+00:00

I have a fragment in an activity that I am using as a navigation

  • 0

I have a fragment in an activity that I am using as a navigation drawer. It contains buttons that when clicked start new activities (startActivity from a fragment simply calls startActivity on the current activity).

For the life of me I can’t seem to figure out how I would finish the current activity after starting a new one.

I am looking to achieve something like this in the fragment:

@Override
public void onClick(View view) {
    // TODO Auto-generated method stub
    if (view == mButtonShows) {
        Intent intent = new Intent(view.getContext(), MyNewActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
    } 
}

But it seems Fragment.class does not implement finish() (like it implements startActivity(…)).

I would like the activity backstack cleared when they launch the 2nd activity. (so pressing back from the new activity would technically drop them back to the launcher)

  • 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-05-26T10:41:04+00:00Added an answer on May 26, 2026 at 10:41 am

    When working with fragments, instead of using this or refering to the context, always use getActivity(). You should call

    Java

    getActivity().finish();
    

    Kotlin

    activity.finish()
    

    to finish your activity from fragment.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Its possible to retain a Fragment between Activities? Lets say I have Activity A
I have a HTML fragment which contains two anchor tags in various parts of
I have a simple little code fragment that is frustrating me: HashSet<long> groupUIDs =
I have two textures generated using a fragment shader. I want to be able
We have a WiX fragment in a file SomeDialog.wxs that prompts the user for
I have a schema fragment that looks like <xs:element name=dataValue> <xs:complexType> <xs:sequence> <xs:element name=value
I have a fragment class that extends Fragment and calls setHasOptionsMenu to participate in
I have this fragment of code: SmsDataClassesDataContext dc = new SmsDataClassesDataContext(); // Get the
I have this fragment that demonstrates the problem: <html> <head> <title>height query demo</title> <script
I have a vertex/fragment shader that draws a rectangle performing many calculations based on

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.