Skip to content

Instantly share code, notes, and snippets.

Created March 31, 2014 03:57
Show Gist options
  • Save anonymous/9884978 to your computer and use it in GitHub Desktop.
Save anonymous/9884978 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.example.response22166282"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name="MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="Exiter" android:theme="@android:style/Theme.NoDisplay"/>
</application>
</manifest>
package org.example.response22166282;
import android.app.Activity;
import android.os.Bundle;
public class Exiter extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
finish();
// Android will clean up the process automatically. But hey, if you
// need to kill the process yourself, don't let me stop you.
System.exit(0);
}
}
package org.example.response22166282;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_finish) {
finishAndHide();
return true;
}
return super.onOptionsItemSelected(item);
}
private void finishAndHide() {
// We don't call finish() explicitly. FLAG_ACTIVITY_CLEAR_TASK should
// take care of it.
// Replace the current task with one that is excluded from the recent
// apps and that will finish itself immediately. It's critical that this
// activity get launched in the task that you want to hide.
final Intent relaunch = new Intent(this, Exiter.class)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK // CLEAR_TASK requires this
| Intent.FLAG_ACTIVITY_CLEAR_TASK // finish everything else in the task
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); // hide (remove, in this case) task from recents
startActivity(relaunch);
}
}
@miguellan
Copy link

This worked, although I still had to call finish on MainActivity, after startActivity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment