sam
sam

Reputation: 1

BACK key does not finish activity

I am running a service that starts a new activity when specific applications are launched.

For example, when I launch sms application, my service detects it by checking a top activity package name and starts a new activity.

But the problem is that after starting a new activity, when I finish that activity and press BACK button from sms application to go back to Home screen, it does not finish my sms application.

Even though the screen is at home(launcher), when I check top activity name, sms app is running as the top activity, which means sms app is not finished after pressing BACK button.

I use Intent.FLAG_ACTIVITY_NEW_TASK intent flag for starting a new activity and finish() to finish it. Does anyone have an idea why my BACK button does not finish sms application in this case?

thanks,

777

Upvotes: 0

Views: 1392

Answers (5)

newbie
newbie

Reputation: 959

try this

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

    // TODO
}

@Override
public boolean onOptionItemSelected(MenuItem item){
    if(item.getItemId() == android.R.id.home){
        finish();
    }
}

Upvotes: 0

bovquier
bovquier

Reputation: 128

I did something like that in my activity:

@Override
    public void onBackPressed(){
        AlertDialog.Builder alert = new AlertDialog.Builder(ctx);
        alert.setTitle("Wylogowanie i wylaczenie.");
        alert
            .setMessage("Exit?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    //DO SOMETHING YOU NEED eg. destroy/close DB, stop services, and move the task to back
                    db.getWritableDatabase(); //getDB
                    db.destroyDB(context); //Destroy it
                    db.close(); //and close
                    stopService(new Intent(getBaseContext(), SVCKeepLogged.class)); //stop some service
                    moveTaskToBack(true); //hide activity (this do not kill it - you can use finish() here)
                    //finish();
                }               
            })
            .setNegativeButton("NOOO!", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                //Keep app alive.
                    dialog.cancel();
                }

            });
        AlertDialog alertDialog = alert.create();
        alertDialog.show();
    }

Upvotes: 0

Ajit Kumar Dubey
Ajit Kumar Dubey

Reputation: 1563

If you want to finish the activity when user navigate to back through device So you can use this code it is more helpful.

    public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {

        this.finish();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

Upvotes: 0

Abhishek Susarla
Abhishek Susarla

Reputation: 578

OK if it doesnt workout.. try overriding the OnBackPressed method and put finish() in that.. hope this helps

Upvotes: 1

PearsonArtPhoto
PearsonArtPhoto

Reputation: 39698

From what I've seen, the back button will halt the current activity, whatever it's doing. If you absolutely need to finish it off, take a look at the lifecycle of an activity, and perhaps put some code into the onPause() and onStop() functions.

Upvotes: 1

Related Questions