Skip to content

Instantly share code, notes, and snippets.

@libinbensin
Created May 5, 2014 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save libinbensin/7d9261fd148c15575dd1 to your computer and use it in GitHub Desktop.
Save libinbensin/7d9261fd148c15575dd1 to your computer and use it in GitHub Desktop.
Android close all below Activities
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:theme="@style/AppBaseTheme"
android:name=".MyApp">
<activity
android:name="com.example.app.activities.FirstActivity"
android:label="@string/title_activity_first"
android:taskAffinity="@string/task_affinity_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.app.activities.SecondActivity"
android:label="@string/title_activity_second"
android:taskAffinity="@string/task_affinity_name">
</activity>
<activity
android:name="com.example.app.activities.ThirdActivity"
android:label="@string/title_activity_third"
android:taskAffinity="@string/task_affinity_name">
</activity>
</application>
</manifest>
public class CommonActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyApp.mBackStackActivities.put(getComponentName().getClassName(), this);
}
@Override
protected void onDestroy(){
MyApp.mBackStackActivities.remove(getComponentName().getClassName());
super.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.common, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
switch (id){
case android.R.id.home:
finish();
break;
case R.id.action_first: {
Intent intent = new Intent(this,FirstActivity.class);
startActivity(intent);
}
break;
case R.id.action_second: {
Intent intent = new Intent(this,SecondActivity.class);
startActivity(intent);
}
break;
case R.id.action_third: {
Intent intent = new Intent(this,ThirdActivity.class);
startActivity(intent);
}
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void startActivity(Intent intent) {
if(MyApp.mBackStackActivities
.containsKey(intent.getComponent().getClassName())){
if(android.os.Build.VERSION.SDK_INT >= 16) {
Activity activity = MyApp.mBackStackActivities.get(intent.getComponent().getClassName());
// finish the activity as well as all the below Activities.
activity.finishAffinity(); // supported from API 16
}else {
// loop through all the below activity and finish it
}
}
super.startActivity(intent);
}
}
public class MyApp extends Application {
public static HashMap<String , CommonActivity > mBackStackActivities
= new HashMap<String, CommonActivity>();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment