3

I'm using Tabs with the FragmentStatePagedAdapter. THe code below is from the onOptionsItemSelected(MenuItem item) method:

case R.id.menu_help:
  System.out.println("Pressin' da help button!");
  FragmentManager mananger = getSupportFragmentManager();
  android.support.v4.app.FragmentTransaction trans = mananger.beginTransaction();
  trans.replace(android.R.id.content, new HelpFragment());
  trans.addToBackStack(null);
  trans.commit();
  break;

android.R.id.content will however only replace the view below the actionbar (and overlay it over the tab-fragments). Is there a simple way how to replace the entire screen with a fragment (shouldn't there be some other system resource for that?) without having to create a new activity for that? Or would a new activity actually be better?

Thanks in advance for the help

Container Layout (started at launch from MainActivity):

<android.support.v4.view.ViewPager
    android:id="@+id/masterViewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

And I guess this is the class of the HelpFragment:

public class HelpFragment extends Fragment {

  private View view;

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle states) {
    view = inflater.inflate(R.layout.layout_help_screen, container, false);

    return view;
  }

}

And the Fragment XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/nothing" />

</LinearLayout>
AreusAstarte
  • 1,958
  • 2
  • 17
  • 29

1 Answers1

0

Update: The switch below is no longer needed if you update appcompat-v7 to revision 19.0.0. or newer. Please issue 59077 for more information.


I suspect that you are using a device/emulator running an Android version before 4.x (Ice Cream Sandwich, API level 14). I experienced a similar overlay problem when using fragment transaction. The layout problem occurs because the content view is referenced differently on Android 2.x and 3.x in comparison to Android 4.x. Please try changing your code from:

trans.replace(android.R.id.content, new HelpFragment());

to:

trans.replace(getContentViewCompat(), new HelpFragment());

...

public static int getContentViewCompat() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH ?
               android.R.id.content : R.id.action_bar_activity_content;
}

Further background information can be found in the post of Shellum.

Community
  • 1
  • 1
JJD
  • 50,076
  • 60
  • 203
  • 339
  • I was debugging the application on an Android 4.1.2 Galaxy S3 at the time. I've moved away from that project many months ago but maybe will dig out the whole thing again to test your solution. I should probably close the question :/ – AreusAstarte Oct 21 '13 at 12:41