want to close application completely

我只是一个虾纸丫 提交于 2019-12-24 06:44:29

问题


i want to close my whole application. i have tried out finishActivity() on back press but it doesn't work. I want to close my application on back press event completely. How could i do it?

 public void onImageViewClicked(View view){
        switch(view.getId()){
            case R.id.viewStk:
intent = new Intent(MainActivity.this,ViewStkActivity.class);
                startActivityForResult(intent, 12);
                break;
            case R.id.about:
                Intent aboutIntent = new  Intent(MainActivity.this,AboutActivity.class);
                startActivity(aboutIntent);
                break;
    } 

}

回答1:


I use:

@Override
public void onDestroy()
{
    super.onDestroy();
    // Force the process to be killed on finish(), not kept around
    System.runFinalizersOnExit(true);
    System.exit(0);
}

which is called after using this.finish()

(This is an overridden method of the activity)

People will complain and tell you it breaks the Android app lifecycle, but having an "exit" button on my app during development was an immediate boost of my productivity.

Edit

Apparently, the latest version of the API notes that the runFinalizersOnExit function is deprecated and unsafe. I've had no issue with it on Gingerbread, ICS, or JB. But in interest of full disclosure, I'll point this out.




回答2:


public void onClick(View v) {
    finish();
    android.os.Process.killProcess(android.os.Process.myPid());
}



回答3:


This will also help

Intent i = new Intent(yourScreen.this,Home.class);
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        i.putExtra("EXIT", true);
        startActivity(i);

and in the onCreate of the Home class, do this to check,

if (getIntent().getBooleanExtra("EXIT", false)) 
    {
        finish();
    }

what this will essentially do is no matter at what activity you are, you can call the home screen with the clear top flag. In the home screen there is a check condition in the onCreate method which will help to clear the stack..Now ,if you press back button you will exit the app as the stack is cleared..

Let me know if the problem still persists...




回答4:


In the activity.java-file that needs the implementation (fx MainActivity.java) insert this:

    public void exitProgram(View view){
    finish();
    System.exit(0);
    }

Just remember to also call the function in your .xml file, fx:

<Button
...
...
android:onClick="exitProgram"
...
/>

and finally in the buttons attributes, at the "text" attribute - press the small button next to the field and choose the one for the button you want to work with - done.

A good way to do it? No. Not at all. But if you just want a "panic-lets-get-outta-here"-button, then here you go.

There are a ton of different ways to implement the exit-functionality (one of them shown here) - as there are a ton of ways to implement the simple "Hello World" program - each has their value and you should really consider, why you have chosen the one you have.



来源:https://stackoverflow.com/questions/12381736/want-to-close-application-completely

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!