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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:27:15+00:00 2026-06-14T03:27:15+00:00

How can I call finish() and other non static methods from a DialogFragment in

  • 0

How can I call finish() and other non static methods from a DialogFragment in the activity that created it? I have tried passing messages from the OnClickLisener in the DialogFragment, to no avail.

I have a really simple app, conssting of a MainActivity and DialogFragment:

    public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle arg0) {
    super.onCreate(arg0);
    setContentView(R.layout.activity);
    showDialog();
}
public void showDialog() {
    DialogFragment newFragment = new ConfirmDialog();
    newFragment.show(getFragmentManager(), "dialog");
}

}
And the Dialog is again very simple:

public class ConfirmDialog extends DialogFragment {
@Override
public AlertDialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage("Confirm you want to continue?")
           .setPositiveButton("Yes.", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   //finish() MainActvity
                  }
               })
           .setNegativeButton("No.", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                 //Do nothing in MainActity
               }
           });
    // Create the AlertDialog object and return it
    return builder.create();
}

}

  • 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-14T03:27:16+00:00Added an answer on June 14, 2026 at 3:27 am

    There are many options. One of them is define an interface with a single method inside.

    1. Have the dialog caller implement that interface.
    2. Keep a global variable pointing to the caller.
    3. Set the variable in the onAttach(Activity activity) method.
    4. Null that variable in the onDetach() method.
    5. Call the variable (interface member) method in the onClick.

    Example:

    public class MainActivity extends Activity implements MyInterface { 
        // ...
    
        @Override
        public void onChoose() { finish(); }
    
    }
    

    And inside ConfirmDialog:

    public static interface MyInterface {
        public void onChoose();
    }
    
    private MyInterface mListener;
    
    @Override
    public void onAttach(Activity activity) {
        mListener = (MyInterface) activity;
        super.onAttach(activity);
    }
    
    @Override
    public void onDetach() {
        mListener = null;
        super.onDetach();
    }
    

    And then call mListener.onChoose() anywhere inside your class.


    I know this has been marked as accepted, but I figured I could provide more feedback to the discussion.

    A note about using or not interfaces. Andy’s answer works just as right as mine, hence why I said “There are many options. One of them is…”.

    However, the reason why I prefer interfaces for this particular problem is because most of the times you’re going to extend and reuse simple/common confirmation dialogs like that. hey are too generic to be “wasted” (or worse: duplicated if different event actions arise).

    Unless you are deadly sure that you are going to use that only once, for one purpose (finishing), you generally should avoid hardwiring (and simplifying) the implementation details of the Activity in your dialog class. Flexibility, abstraction and efficiency. Less code to maintain.

    And yes, there is a telltale that you may need that: the public keyword that you’re using, especially if it’s in a self-contained class file, which begs for reuse (too). Otherwise, you should be hiding that class inside your main Activity, since the implementation details (would) relate only to that one. Also, you would be removing the public keyword.

    Yes, you could use for more than one Activity, but you’d be limited to finish()ing. The interface will give you flexibility to do whatever you want in each Activity. In other words, it’s up to the implementer to define how it should itself behave for that event. You self-contain implementation details.

    As a sidenote, what I do is create a package with all dialogs I may need for my application. For confirmation dialogs like that, I reuse for different messages and buttons. I provide defaults, but also allow for change using setArguments. And I keep the interfaces related so I don’t need to create one interface for each dialog. The implementer responds according to which dialog triggered the “dialogs callback”. Flexibility, abstraction and efficiency, all while avoiding things humorously called Hydra and Royal Family. So. In the end, like I said, many options. Don’t over-engineer, but don’t simplify too much too early (leave room for graceful expansion).

    It’s more important to understand advantages and pitfalls than choosing this or the other answer.

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

Sidebar

Related Questions

(Yes I know I can call Java code from Scala; but that is pointless;
So I have my Activity, and on pressing a Quit button I call Activity.finish().
I have an activity than can be called from my parent activity and from
I've written an application that creates a map activity. From there the user can
I have a little question which is bothering me. How can I finish activity
In Ruby I can call methods with array elements used as positional parameters like
From this code I can call bmwCars.CopyToDataTable() as I expected. var bmwCars = from
I understand that I can call ToString().IndexOf(...), but I don't want to create an
Using a delegate I can call any function asynchronously. From the documentation I understand
Like plenty of other people before me, I have some web content that I

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.