How to prevent double addtobackstack while on PreferencesFragment in android
<p><strong>Background:</strong></p>

<p>I'm using PreferencesFragment to manage preferences in my android app and i use options menu to access those preferences.</p>

<p><em>MainActivity class overriden methods:</em></p>

<pre><code>@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()){
    case R.id.menu_prefs:
        PrefsFragment prefs = new PrefsFragment();
        getFragmentManager()
            .beginTransaction()
            .replace(R.id.main_container, prefs)
            .addToBackStack(null)
            .commit();
        break;
    }
    return true;
}
</code></pre>

<p><em>res/menu/menu.xml:</em></p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;menu xmlns:android="http://schemas.android.com/apk/res/android" &gt;

&lt;item
    android:id="@+id/menu_prefs"
    android:icon="@android:drawable/ic_menu_preferences"
    android:title="@string/menu_prefs_title"
    android:showAsAction="ifRoom|withText"&gt;
&lt;/item&gt;

&lt;/menu&gt;
</code></pre>

<p><strong>Problem:</strong></p>

<ul>
<li>While on main screen, I click preferences icon in action bar, PrefsFragment starts.</li>
<li>While on PrefsFragment, I click preferences icon in action bar PrefsFragment starts <strong>again</strong> (this is where problem arise).</li>
<li>I hit the back button and it reverts fragment transaction and goes back to <strong>PrefsFragment</strong>. (it should go back to main screen).</li>
</ul>

<p><strong>Question:</strong>
Is there any smart way to disable double showing PrefsFragment?</p>

<p><strong>Desired outcome:</strong>
Clicking prefs icon in action bar if on main screen shows PrefsFragement, clicking prefs icon in action bar while PrefsFragemnt is visible does nothing, clicking back button while on PrefsFragment always goes back to main screen.</p>
