Skip to content

Instantly share code, notes, and snippets.

@MostafaGazar
Created September 19, 2015 09:52
Show Gist options
  • Save MostafaGazar/304e89cef5f9e7be5d79 to your computer and use it in GitHub Desktop.
Save MostafaGazar/304e89cef5f9e7be5d79 to your computer and use it in GitHub Desktop.
HomeEnabled Activity
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v4.app.TaskStackBuilder;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
/**
* @author Mostafa Gazar <mmegazar@gmail.com>
*/
public abstract class BaseHomeEnabledActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
Intent upIntent = NavUtils.getParentActivityIntent(this);
upIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
TaskStackBuilder.create(this)
.addNextIntentWithParentStack(upIntent)
.startActivities();
} else {
NavUtils.navigateUpTo(this, upIntent);
}
return true;
}
return super.onOptionsItemSelected(item);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment