I'm trying to add this function:
-When clicking on Return button it pops an exit prompts (AlertDialog with yes or no)
Problem
But its not working properly. If I click on the button, it exits the program directly.
Here is my whole Activity Code :(The exit function that I have tested is at the end of the code)
@SuppressWarnings("deprecation")
public class MainScreen extends TabActivity {
// TabSpec Names
private static final String Alerts_SPEC = "Alertes";
private static final String Status_SPEC = "Status";
private static final String Details_SPEC = "Events";
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_screen);
TabHost tabHost = getTabHost();
// Inbox Tab
TabSpec alertsSpec = tabHost.newTabSpec(Alerts_SPEC);
alertsSpec.setIndicator(Alerts_SPEC);
Intent alertsIntent = new Intent(this, FragmentAlerts.class);
alertsSpec.setContent(alertsIntent);
// Outbox Tab
TabSpec statusSpec = tabHost.newTabSpec(Status_SPEC);
statusSpec.setIndicator(Status_SPEC);
Intent statusIntent = new Intent(this, FragmentStatus.class);
statusSpec.setContent(statusIntent);
// Profile Tab
TabSpec detailsSpec = tabHost.newTabSpec(Details_SPEC);
detailsSpec.setIndicator(Details_SPEC);
Intent detailsIntent = new Intent(this, FragmentEvents.class);
detailsSpec.setContent(detailsIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(alertsSpec); // Adding Inbox tab
tabHost.addTab(statusSpec); // Adding Outbox tab
tabHost.addTab(detailsSpec); // Adding Profile tab
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
/**
* On selecting action bar icons
* */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_refresh:
// refresh
finish();
startActivity(getIntent());
return true;
case R.id.action_password:
// help action
Intent i = new Intent(getApplicationContext(), LoginActivityAdmin.class);
startActivity(i);
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
// -------------- EXIT CODE BEGINS ------------------------------
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
if ((keyCode == KeyEvent.KEYCODE_BACK)) { //stop your music here
// To exit application
onBackPressed2();
}
return super.onKeyDown(keyCode, event);
}
public void onBackPressed2() {
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MainScreen.this.finish();
}
})
.setNegativeButton("No", null)
.show();
}
}
SOLTUION :
I added this code to each child :
@Override
public void onBackPressed() {
this.getParent().onBackPressed();
}
and then called this in my main activity :
@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MainScreen.this.finish();
}
})
.setNegativeButton("No", null)
.show();
}