way to exit from the app in android - android

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.

Related

Quit and remove application from RAM

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.

Exclude current activity from Recent Tasks

My app, running in background, at some point displays activity to purposefully interrupt user's flow. My activity starts a new task, which appears in "Recent Tasks" lists while being in foreground. Is there any way it could be prevented? Setting android:excludeFromRecents does not work - activity is not presented anymore in "Recent Tasks" only after is has been paused.
Manifest looks like:
<activity
android:name="com.example.recenttasks.MainActivity"
android:excludeFromRecents="true">
</activity>
and activity is started this way:
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
The key thing you mentioned is
appears in "Recent Tasks" lists while being in foreground
I think you can't change that behavior. I just tested on my app and the same thing happens. If I press the "recent tasks" button while having my activity in the foreground, it appears listed there. The moment I move out of it to another activity or to my phone's main screen, the activity is not listed anymore.
I also tested this on the built-in DeskClock app that comes with recent Android versions and the same behavior is present there when a new alarm is triggered. Note that the AlarmAlertFullscreen activity of that app has the same parameters you mentioned in your question.
I'm not sure you can circumvent this or why you would need to in the first place since the activity is not listed anymore once it loses the focus.
you are defined it in manifest that is enough but it is not coming..
ok..try add this flag also to your Intnet and start the Activity..
intnet.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Should the user be able to return to it? (Meaning if it shows and user clicks recents, have they lost their chance to see the hidden activity or should it still be there?)
If they cannot return to it anyway then the best action would be to finish() the activity onPause(). This will mean that as long as you have used android:excludeFromRecents and android:noHistory="true", there will be no item in recents.
If however you do wish to return to the 'interruption' activity (but do not want a recents entry) you could consider still finishing the activity onPause - but also recording a preference flag (something like IS_INTERSTITIAL). Your other activities can then check this preference in onResume and, if it is true, send an Intent to restart the Interstitial instead - To the user it will just appear they are resuming the app in the same state as they left it (interstitial)
Edit: If the screen needs to stay (rather than be re-instantiated) it may be possible to use a DialogFragment, although then you must worry about configuration changes. There is one hack you could try (explained in this answer) -
Set your label empty By using android:label="" your recent task is excluded. however this is a definite hack which may produce inconsistent results (as I haven't tested it to be sure)

Redirecting to home in android

Redirect to home. I opened different App from my App using intent. When I click back it was redirecting to my application. But at that time I want to close that App or redirect me to home. I tried with finish() and destroy(). But those are not working ?
Intent nextIntent = new Intent(Intent.ACTION_MAIN);
nextIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
nextIntent.setComponent(new ComponentName(packageName,package_running_cls));
NotFirst.this.finish();
startActivity(nextIntent);
If finish() is not working you should override onBackpressed() method and in onbackpressed you can use intent to go to your HomeActivity like this
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
Intent in=new Intent(this,YourHomeActivity.class);
startActivity(in);
this.finish();
}
try this, it may help you.
You can use moveTaskToBack(true).
This will hide your application until the user wants to use it again.
The longer answer starts with another question: why do you want to kill your application?
The Android OS handles memory management and processes and so on so my advice is just let Android worry about this for you. If the user wants to leave your application they can press the Home button and your application will effectively disappear. If the phone needs more memory later the OS will terminate your application then.
As long as you're responding to lifecycle events appropriately, neither you nor the user needs to care if your application is still running or not.
So if you want to hide your application call moveTaskToBack() and let Android decide when to kill it.

Exit an android app

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);

How to remove application from recent application list?

I guess that Android won't let people to do this, because they think they have perfect handle for the task/applications. However, I really need to do this in my case.
I have an activity A acting as the entry point of my application. In that activity, it reads the preference and decided which activity to start, say B or C. After that, it finishes itself. So, activity A never appears to the users.
My application stores things on sdcard, and reads from it constantly. So, when the sdcard is unmounted, I need to display a message to the user that the sdcard is unavailable, instead of opening B or C. I set a check in A to display that message when sdcard is unavilable. When that message is displayed, A will not try to start B or C.
Things works perfectly if user only enter my application from application launcher. However, I found that user can also enter my application by long pressing home and choose it from the recent application list, if he has opened it recently. When user does that, it skips A and goes directly to B or C. I don't have the check in both of them, so exception is thrown while I am trying to access sdcard, and force close dialog pops up.
I can simply move my check to both B and C to fix this problem. But in the future, the number of activities started from A will increase. If there are 6 of them, I'll need to copy this check to 6 places. Needless to say, this looks very ugly, and is a maintenance nightmare.
So, the best fix should be removing my application from recent application list when the sdcard is uunmounted. However, I can't find how to do this. Even killing the process or use ActivityManager.restartPackage, it still appears in the list. Can anyone tell me how to remove it from the list?
try
<activity android:name=".MainActivity"
android:excludeFromRecents="true" ...
in your AndroidManifest.xml's activity declaration.
Other attributes can help your activity isolate from other activities in the same package.
<activity
android:name=".aActivity"
android:excludeFromRecents="true"
android:taskAffinity=""
android:launchMode="singleInstance">
just add android:excludeFromRecents="true" in your activity's tag in the manifest file..its easy
You need to set below code in Launcher Activity in AndroidManifest.xml :
<activity>
...
android:excludeFromRecents="true"
...
</activity>
or start this activity from
intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
and also in AndroidManifest.xml
<activity>
...
android:label=""
...
</activity>
Setting label as empty will let this activity NOT be shown in Recent App list.
I would not recommend to do so but I would try the following options:
Option 1:
On B and C add:
protected void onPause() {
finish();
}
Option 2:
Add to B and C the following in the AndroidManifest:
android:noHistory= "true"
Removing your application from the recent apps list is probably not possible, and definitely not the best solution. That will just confuse the user who expects all apps to behave similarly.
Regardless, I don't think it will solve your problem. If the user hits home while on Activity B, then selects your app from the home page, it will start Activiy B again.
There are a number of ways to solve the real problem. One easy one might be to create a base Activity that performs the SD card check, and have all of your activities extend from it. That way the check is only in one place.
OK, I know for a fact that it can be done in 2.3.4. The app Toddler Lock when opened clears the recent app list so that when you "Lock" your phone if you long press home key the list is blank. Unfortunately I have not found how to do it. So for anyone who is looking and reading postf that say it is not possible don't give up. I sure heck am not.
if you wanna exit Application on Button click use this code :
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
To kill the complete app and remove it from Runningapp list kill the app through its pid(its nasty)... use this lines before above code.
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
try this,
it will finish all activities and remove app from recents
private fun killCurrentApp() {
val am = this.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager?
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
val appTasks = am!!.getAppTasks()
if (appTasks.size > 0) {
val appTask = appTasks.get(0)
appTask.finishAndRemoveTask()
}
}
}

Categories

Resources