I've read there are a lot of ways to close an application, so I would like to know if this way is correct or there are defects
main_activity.java
public boolean onOptionsItemSelected(MenuItem item) {
.....
.....
else if (id == R.id.exit) {
onDestroy();
}
}
#Override
protected void onDestroy() {
System.exit(0);
super.onDestroy();
finish();
}
Is this conceptually correct?
To close your app, this pinch of code will help you do the job.
android.os.Process.killProcess(android.os.Process.myPid());
Android How to programmatically close an app..
But you may want to know that I have never seen this approach in use anywhere. Even apps from certified developers do not try to close and remove themselves from processes. What you can really do and preferred is finishing all the running activities and services and hence allow user to kill apps from recent apps himself (default behaviour of any app).
This functions closes all activities but not app -
finishAffinity(); // API 16
Or you can call
finish();
everytime you end an activity.
These do not close Services to best of my knowledge and you programmatically have to stop running Services.
Cheers!
The best way to exit an app is using
finishActivity();
You don't want to kill the whole process, it is not good practice.
Related
I am building an app which uses either some activities in which the user can navigate or uses one specific activity which starts the app when a deeplink is used. In this DeeplinkActivity I have a button with which the app should exit the entire app and not put it into the background.
I tried the following code, which only closes the app, but it still remains in the background?!
public void onStopButton(View view) {
log.info("UI -> Stop this app");
// this.finishAffinity();
android.os.Process.killProcess(android.os.Process.myPid());
// finish();
System.exit(0);
}
I tried all kinds of suggested combinations, but none of them really exits the app. Anybody have some other (working) code suggestions?
I have set minSdkVersion=19 and targetSdkVersion=26
First, note that having a button such as you describe is considered to be an anti-pattern in Android.
That being said, if your minSdkVersion is 21 or higher, replace your onStopButton() with:
public void onStopButton(View view) {
finishAndRemoveTask();
}
If your minSdkVersion is lower than 21, there are some clunky workarounds that I do not recommend.
I have 6 activities in my application.
In 2 activities, I have coded quit button, but whenever I quit my application with that button, and start that app from "Recent applications" section, it continues from that activity.
How to code it in such a way that when I restart it from "Recent applications" section, it starts from the very first activity.
This is my code.
public void clickexit(View v)
{
moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
In XML file,
android:onClick="clickexit"
So if your Android version is 4.1 or higher use this
finishAffinity();
System.exit(0); // try this one
else you can try this
ActivityCompat.finishAffinity(YourActivity.this);
For more details refer to this
Read this document: document
If you put android:excludeFromRecents in your manifest will do it. Also you can find more tools with that link.
I have added intent-filter to my activity, so I can receive send intents that contain text, I have set data to text/plain, everything works fine, I can choose my application from the picker, it is opening, showing me the text, but, when I minimalize my app, and then click again on ColorNote application (I am using it to take my notes) it is opening my app instead of ColorNote, anyone ever had similiar issue? And know how to resolve it?
#Edit
Do not know why I am getting - points, if you think it is easy question you could write how to solve it instead of giving -.
By the way, I don't think any code is neccessary in here right now.
That's the way it should behave. For e.g. if an application starts another and you leave the application without closing it's task you will return to the intent it started. I can reproduce this with apps which take me to the play store:
What I did:
First test:
Start application
Let it start the Play Store
Press Home
Start the application again
welcome back to the app store
Second test:
Start application
Let it start the Play Store
Close application (Task Manager)
Start the application again
welcome back to the application
Okay now on how to solve your problem. I would check if my application was started by another intent -> http://developer.android.com/training/basics/intents/filters.html
boolean wasStartedByOtherApp = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
if (intent.getType().equals("text/plain")) {
wasStartedByOtherApp = true;
}
}
#Override
protected void onPause() {
super.onPause();
if(wasStartedByOtherApp){
finish();
}
}
I have not tested this. This should just give you an idea on how to solve the issue you are facing. But be warned as an android user I would be angry if an application closes itself just when I want to check my calendar if I have time and would like to return to the intent the app just started.
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;
}
}
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.
public void onClick(View v) {
finish();
android.os.Process.killProcess(android.os.Process.myPid());
}
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...
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.
When I exit my android application it resumes to the previous screen. How can I exit the Android application properly?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()) {
case EXIT:
try {
this.finish();
} catch(Exception e) {
}
break;
}
return false;
}
The answer is that you shouldn't exit your Android application.
Have a look at this excellent answer for more information.
this.finish();
will only finishes your current activity , that's the reason your resuming to previous screen.
In android you can't kill the application.
Still you want to kill your application , then kill your process ID.
//Put this in to exit an application. Not a window
System.Exit(0);
If I don't exit my Android app such as myTuner, the radio station keeps playing...stopping it from playing is kind of ridiculous when exiting to give back the phone its memory makes every bit more sense. Whoever thought of allowing Android apps to just accumulate un-exited and slow down your phone made a huge mistake in my opinion.
It's how people want to user their phone that matters more than how someone else thinks they have to use it. I prefer freedom of choice for something that makes more sense.
Make apps you can exit out of like other OSes allow for.