1

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();

    }
berserk
  • 2,690
  • 3
  • 32
  • 63
Kinn
  • 21
  • 1
  • 6

4 Answers4

1

Try something from this link:

Key Events in TabActivities?

Each tab's Activity handled the "back" presses.

Community
  • 1
  • 1
Zoran
  • 1,484
  • 1
  • 10
  • 13
0

My previous answer was wrong, try this:

 /* you don't need this
 @Override 
public boolean onKeyDown(int keyCode, KeyEvent event){ 

    if ((keyCode == KeyEvent.KEYCODE_BACK)) { //stop your music here
         // To exit application
        onBackPressed();
    } 
    return super.onKeyDown(keyCode, event); 
} */

@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) {
                   onBackPressed2();
               }
           })
           .setNegativeButton("No", null)
           .show();

}

public void onBackPressed2()
{
    MainScreen.this.finish();
}

}

Or do the following:

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event){ 

    if ((keyCode == KeyEvent.KEYCODE_BACK)) { //stop your music here
         // To exit application
        onBackPressed2();
        return true;
    } 
    else
    {
        return super.onKeyDown(keyCode, event); 
    }
}
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
0

Do not call super.onKeyDown(keyCode, event); when you handle the key event.

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event){ 

    if ((keyCode == KeyEvent.KEYCODE_BACK)) { //stop your music here
         // To exit application
        onBackPressed2();
        return true; 
    } 
    return super.onKeyDown(keyCode, event); 
}
mach
  • 8,315
  • 3
  • 33
  • 51
0

you need to put onBackPressed() method inside FragmentAlerts.class. MainActivity is extends by TabActivity so default call gets to your first tab. so call your onbacpress indside first activity i.e FragmentAlerts.

chet's
  • 193
  • 2
  • 8
  • solved by adding OnBackPressed() in each Fragment function for me – Kinn Jun 05 '14 at 09:08
  • good. TabActivity represent declaration of Tabs only if you want to perform any key events then implement it inside that particular Activity. – chet's Jun 05 '14 at 09:19