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

    }
2
  • why onBackPressed2 ?
    – user2035951
    Commented Jun 5, 2014 at 8:47
  • i could name it whatever but even with onBackPressed and with @override nothing happens
    – Kinn
    Commented Jun 5, 2014 at 8:48

4 Answers 4

1

Try something from this link:

Key Events in TabActivities?

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

3
  • THANK YOU ,you are a life save =)
    – Kinn
    Commented Jun 5, 2014 at 9:05
  • Will there be maybe some Solved mark on my answer? :)
    – Zoran
    Commented Jun 5, 2014 at 9:09
  • 1
    done =) by the way i found the solution on the link so delete super.onBackPressed();
    – Kinn
    Commented Jun 5, 2014 at 9:11
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); 
    }
}
7
  • i just tested this , nothing changed
    – Kinn
    Commented Jun 5, 2014 at 8:48
  • Oh, I think I misunderstood your question at first. I'll fix my answer. Commented Jun 5, 2014 at 8:49
  • Thanks , but still nothing happens ,i'm wroking with 4.1.2 platform API 19
    – Kinn
    Commented Jun 5, 2014 at 8:55
  • When happens nothing, do you get alert dialog? Or nothing happens when you click yes?
    – Zoran
    Commented Jun 5, 2014 at 8:58
  • Tested the second one ,still no ,I DONT UNDERSTAND WHY ITS NOT WORKING , ITS SO LOGIC TRUE
    – Kinn
    Commented Jun 5, 2014 at 8:59
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); 
}
1
  • Deleted that and teste pure OnBackPressed() , still nothing
    – Kinn
    Commented Jun 5, 2014 at 8:56
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.

2
  • solved by adding OnBackPressed() in each Fragment function for me
    – Kinn
    Commented Jun 5, 2014 at 9: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
    Commented Jun 5, 2014 at 9:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.