简体   繁体   中英

Close Application Instead Go Previous Activity

I am learning android and want close application if my int value is 1. I want when user click on Navigation arrow icon. Currently I am trying below code but instead close application, its sending me on Previous Activity. I have verified that blocked value is 1 but dont know why its not closing application. My code is like below

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Log.e("blocked",""+blocked);
        if(blocked==1){
            finish();
            return false;
        }
        else if (item.getItemId() == android.R.id.home){
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

Let me know if someone can help me for achieve this. Thanks

You can finish your first activity as soon as you launch the second. That way when you're coming "back" from the second activity there's nothing to come back to and the app will close.

Or you can call finishAffinity() which will finish all activities in current task.

Read more: Understanding Tasks and Back Stack

One option can be using this:

Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory( Intent.CATEGORY_HOME );
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
startActivity(homeIntent); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM