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:
- When the dialog pops up, and I press the back button, it just closes the dialog?
- When I press on exit button, it closes the application.
- When I press on the Stay button it closes the dialog.
Thanks
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.