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.
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 a HomeActivity (for show splash screen in 3 seconds) , then automatically redirect to LoginActivity (for check users information for login).
In LoginActivity I have a exit button for exit the app, with below code
// TODO Auto-generated method stub
finish();
android.os.Process.killProcess(android.os.Process. myPid());
System.exit(0);
I used the same code in onDestroy() again.
But , when I try to exit from the app , Program is firmly closed. but remains in memory (in background app list ). How can I solve it?
It's not a good idea to call:
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
because android will handle processes automatically.
Also exclude your app from recent isn't a correct behavior.
By the way you can put under your "exit" activity tag in manifest:
android:excludeFromRecents="true"
And it will not appear in recent apps when the app is closed
EDIT
If it doesn't work in Android 5.0 it was a reported bug, so add taskAffinity property and use autoRemoveFromRecents:
android:taskAffinity=".YourExitActivity"
android:autoRemoveFromRecents="true"
Then in your onPause() you can check the sdk version to use finishAndRemoveTask:
if(android.os.Build.VERSION.SDK_INT >= 21) {
finishAndRemoveTask();
} else {
finish();
}
finish() is enough.
If the app still remains in memory after call finish(), it may be Memory leak in your app.
While mentioning tag in manifest we can keep
`android:`noHistory=true
so that mentioned activity wont be in back stack.
For example:
<activity
android:name="Splash_Activity"
android:label="Splash"
android:noHistory="true" />
You can call just finish() to close activity.
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.
When open my application first time , i pressed home key.
again i click my application.
its not calling onResume() directly. its loading from splash screen onCreate().
is it android default.?
After i've pressed "Back" button , app has closed . there after ,i opened the application and pressed home key, the issue dont come its calling onResume() method not from splash screen onCreate().
My problem is , before pressing back key, if we press home key and open the app, tha app will opened newly. its added in stack.
I've download "Facebook" application and checked. that app also hav same issue.
How do resolve this problem in android?
Android may decide to kill your application when it's not in the foreground. If the application was killed, starting it again would probably show the splash screen again.
When open my application first time , i pressed home key.
again i click my application.
When you press Home Key then your application will go in background, and it will start from where you left if you directly open your application from background apps list. (Button beside HOME key)
And if you click on application icon from list of apps, it will launch again from first activity.
You can refer to this link
How to make an android app return to the last open activity when relaunched?
Check your Developer options.
My guess is that your problem is "Don't keep activities".
i got solution from here: http://code.google.com/p/android/issues/detail?id=2373
add this code onCreate() method in splash screen activity:
if (!isTaskRoot()) {
final Intent intent = getIntent();
final String intentAction = intent.getAction();
if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) &&
intentAction != null && intentAction.equals(Intent.ACTION_MAIN)) {
finish();
}
}
I think your app will be killed if you need too much memory in the background. So if you know that your app goes to the background free some memory. It must not be all but for testing you could try it.
There is also a callback which informs you that you should free your memory:
#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
#Override
public void onTrimMemory(int level) {
super.onTrimMemory(level);
if(level >= TRIM_MEMORY_MODERATE) {
// free some memory
} else if(level >= TRIM_MEMORY_BACKGROUND) {
// free some more memory
}
}
How to close an android app if more than one activity is in active state?
A blog post entitled Exiting Android Application will show how to exit an Android app:
When the user wishes to exit all open activities, they should press a button which loads the first Activity that runs when your app starts, in my case "LoginActivity".
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
The above code clears all the activities except for LoginActivity. LoginActivity is the first activity that is brought up when the user runs the program. Then put this code inside the LoginActivity's onCreate, to signal when it should self destruct when the 'Exit' message is passed.
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
I got an easy solution for this problem
From the activity you press the exit button go to the first activity using the following source code. Please read the documentation for FLAG_ACTIVITY_CLEAR_TOP also.
Intent intent = new Intent(ExitConfirmationActivity.this, FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Now overide onResume() of the first activity using finish()
The answer is simple: You really do not need to 'close' an Android application. If no activity is shown any more, the system will kill the process after some time. The users can close activities by pressing the 'back' button. Reto Meier explains it pretty well here:
http://blog.radioactiveyak.com/2010/05/when-to-include-exit-button-in-android.html
You might also want to read this thread; it is very helpful to say the least: Quitting an Android application - Is it frowned upon?
Well, you shouldn't close your applications, as the system manages that. Refer to the posts/topics in the other answers for more information.
However, if you really, really want to, you can still call System.exit (0); like in any other Java application.
EDIT
ActivityManager actmgr = (ActivityManager) this.getSystemService (Context.ACTIVITY_SERVICE);
actmgr.restartPackage ("com.android.your.package.name");
I remembered something. I was trying to use this code to restart my application, but it only managed to kill my app. You can try it and see if it works for you.
I asked a similar question a couple of weeks back. Do go through the answers and comments for more perspective and possible solutions.
IMO quitting an application depends on what your application does and the user expectations. While I understand the rationale on not having a quit button I also do believe that it's a choice that the application designer has to make based on the situation.
Once your last Activity looses focus, Android will unload your process according to the current system needs / free resources.
You shouldn't really care about that - just use the lifecycle onStart, OnStop etc... to manage your state.
If you want to exit from one Android activity, this will bring you back to the previous activity or another activity from a specific place in current activity.
finish();
System.exit(0);