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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:43:36+00:00 2026-06-17T18:43:36+00:00

I have two xml files, and two java files. In the first xml I

  • 0

I have two xml files, and two java files. In the first xml I have few buttons, and one of them is EXIT. In the java file i write in the onCreate:

Button exitButton = (Button) this.findViewById(R.id.button_exit);
    exitButton.setOnClickListener(this);

Then further down the code I write:

@Override
public void onClick(View v) {
    switch (v.getId()) {

    case R.id.button_exit:
        Intent switchtoExit = new Intent(StartActivity.this, ExitActivity.class);
        startActivityForResult(switchtoExit, MESSAGE_REQUEST);
        break;
    }
}

the second java files is called ExitActivity.java. In the manifest file I wrote:

<activity android:name=".ExitActivity" 
            android:label="@string/exit_title"
            android:theme="@android:style/Theme.Dialog"/>

In order to make the second xml file popup like a dialog.
My second java file is(the one that popup like a dialog):

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ExitActivity extends Activity implements OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_exit);

        Button noExitButton = (Button) this.findViewById(R.id.exit_no_button);
        noExitButton.setOnClickListener(this);
        Button yesExitButton = (Button) this.findViewById(R.id.exit_yes_button);
        yesExitButton.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.exit_no_button:
            Toast.makeText(this, "Good Choice :-D", Toast.LENGTH_SHORT).show();
            break;
        case R.id.exit_yes_button:
            Toast.makeText(this, "So sad... \nnice playing with you...", Toast.LENGTH_SHORT).show();
            break;
        //break;
        }
    }
}

and my second java file has:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="@color/red">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/exitTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/exit_body" 
            />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/exit_no_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/exit_no_button" 
                android:layout_weight="1.0"
                />

            <Button
                android:id="@+id/exit_yes_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/exit_yes_button" 
                android:layout_weight="1.0"
                />

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

What I wanted to do is to have one button close the dialog, and the other button to stop the application. I tried finish(), and it worked just fine. but then when I added the onDestroy() method one button would restart the application, and the other just close it. Also, when I pressed the Back button, it would close the application.

Can anyone explain me how to get the following to happen:

  1. When the dialog pops up, and I press the back button, it just closes the dialog?
  2. When I press on exit button, it closes the application.
  3. When I press on the Stay button it closes the dialog.

Thanks

  • 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-17T18:43:38+00:00Added an answer on June 17, 2026 at 6:43 pm

    You’re doing this the wrong way. Using a second activity is overkill. Use an AlertDialog, which you can easily make with AlertDialogBuilder. Set it to have a positive and a negative button. Set an OnClickListener for the positive button that calls finish on the only activity. There you go- 1 activity, and the dialog class itself will take care of popping up and closing itself.

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

Sidebar

Related Questions

Suppose I have two XML files. First XML File: <?xml version=1.0?> <AccessRequest xml:lang=en-US> <AccessLicenseNumber>Your_License</AccessLicenseNumber>
I am have two xml files.. I first get one and loop through it
I have written code in Java to merge two xml file, the first file
I have created two folders layout and layout-land with two xml files, one for
I have set the two buttons in preference.xml file. I want to go to
I have two XML files. The first XML has a bunch of nodes that
I have two java files. In the first I have my activity which starts
I have two XML files that are generated by another application I have no
I have two XML files. The structure of both XML files is as below:
this is my situation: I have two org.w3c.dom.Document created from two xml files. What

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.