How to finish current activity in Android

If you are doing a loading screen, just set the parameter to not keep it in activity stack. In your manifest.xml, where you define your activity do: <activity android:name=”.LoadingScreen” android:noHistory=”true” … /> And in your code there is no need to call .finish() anymore. Just do startActivity(i); There is also no need to keep a … Read more

Close application and remove from recent apps/

I based my solution on guest’s above, as well as gilsaints88’s comments below (for Android L compatibility): Add this activity to your AndroidManifest.xml file: <activity android:name=”com.example.ExitActivity” android:theme=”@android:style/Theme.NoDisplay” android:autoRemoveFromRecents=”true”/> Then create a class ExitActivity.java: public class ExitActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if(android.os.Build.VERSION.SDK_INT >= 21) { finishAndRemoveTask(); } else { finish(); … Read more

How to show a dialog to confirm that the user wishes to exit an Android Activity?

In Android 2.0+ this would look like: @Override public void onBackPressed() { new AlertDialog.Builder(this) .setIcon(android.R.drawable.ic_dialog_alert) .setTitle(“Closing Activity”) .setMessage(“Are you sure you want to close this activity?”) .setPositiveButton(“Yes”, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }) .setNegativeButton(“No”, null) .show(); } In earlier versions it would look like: @Override public boolean … Read more

Finish all activities at a time

Whenever you wish to exit all open activities, you should press a button which loads the first Activity that runs when your application starts then clear all the other activities, then have the last remaining activity finish. to do so apply the following code in ur project Intent intent = new Intent(getApplicationContext(), FirstActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra(“EXIT”, … Read more

How to check if activity is in foreground or in visible background?

This is what is recommended as the right solution: The right solution (credits go to Dan, CommonsWare and NeTeInStEiN) Track visibility of your application by yourself using Activity.onPause, Activity.onResume methods. Store “visibility” status in some other class. Good choices are your own implementation of the Application or a Service (there are also a few variations … Read more