2

When I run my app, MainFragment does not show up at all in the activity extending ActionBarActivity. I did check this post, but it didn't help me resolve my problem. Here's my activity and fragment:

activity_main.xml:

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new MainFragment())
                .commit();
    }

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, 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;
    }
    return super.onOptionsItemSelected(item);
  }
}

fragment_main.xml:

public class MainFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    return rootView;
    }
}

I don't think I need to reference any xml here... everything was working fine until I extended ActionBarActivity instead of Activity in activity_main.xml.

EDIT: Added xml just in case:

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame" >

<include layout="@layout/toolbar" />

</LinearLayout>

fragment_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity$PlaceholderFragment">

// There are several text views and buttons I will not add to this code, 
// but just know that this fragment isn't blank.

</RelativeLayout>

EDIT 2: I've done some testing and whenever I remove the toolbar from the activity_main.xml, the fragment shows up, along with no action bar (since that is the theme I have set to my app in order to use the toolbar). So here is the xml for my toolbar.xml as well, which I use in activity_main.xml. Maybe I've done something wrong for it to hide my entire fragment?

tl;dr: fragment shows when this line of code is removed from activity_main.xml:

<include layout="@layout/toolbar" />

and this one in activity_main.xml:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
Community
  • 1
  • 1
wasimsandhu
  • 4,296
  • 9
  • 27
  • 40
  • @HareshChhelana One of the new Android 5.0 widgets. http://developer.android.com/reference/android/widget/Toolbar.html – wasimsandhu Nov 04 '14 at 04:33
  • @awkwardgiraffe have u import `appcompat v7` library properly and what is `android:minSdkVersion` in *manifest* – Kaushik Nov 04 '14 at 04:46

7 Answers7

5

Just opened a bug report at AOSP. A solution I found and use for now is to call add() inside a post().

new Handler().post(new Runnable() {
    @Override
    public void run() {
        getFragmentManager().beginTransaction()
            .add(R.id.container, new MainFragment())
            .commit();
    }
});
sergej shafarenka
  • 20,071
  • 7
  • 67
  • 86
2

You need to change your getfragmentManager() to getSupportFragmentManager()

Like this

 getFragmentManager().beginTransaction()
            .add(R.id.container, new MainFragment())
            .commit();

to

 getSupportFragmentManager().beginTransaction()
            .add(R.id.container, new MainFragment())
            .commit();

because ActionBarActivtiy is in supportpackage so you need getSupportFragmentManager()

amit kumar
  • 291
  • 1
  • 6
1

Maybe you're actually NOT using v7 appcompat library, in which case your MainActivity should extend FragmentActivity instead? See also: Fragment add or replace not working

public class MainActivity extends FragmentActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
      getFragmentManager().beginTransaction()
            .add(R.id.container, new MainFragment())
            .commit();
    }

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setActionBar(toolbar);
  }

  ...
}

Or, if you're using v7 appcompat library,

public class MainActivity extends ActionBarActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
      getSupportFragmentManager().beginTransaction()
            .add(R.id.container, new MainFragment())
            .commit();
    }

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
  }

  ...
}
Community
  • 1
  • 1
Fadils
  • 1,508
  • 16
  • 21
  • I need to extend ActionBarActivity for my Toolbar. – wasimsandhu Nov 04 '14 at 04:37
  • 2
    @Fadils [ActionBarActivity](https://developer.android.com/reference/android/support/v7/app/ActionBarActivity.html) is a `subclass` of [FragmentActivity](http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html) .So that is not the issue. – Kaushik Nov 04 '14 at 04:42
  • @awkwardgiraffe you can extend your `MainActivity` by `FragmentActivity` then for the toolbar change `setSupportActionBar` to `setActionBar` – Fadils Nov 04 '14 at 04:53
  • @kaushik. Thank you. I understood this. However, we should use ActionBarActivity only when we're targeting Android 2.1 and above. [link](http://developer.android.com/training/basics/fragments/creating.html). – Fadils Nov 04 '14 at 04:55
  • @awkwardgiraffe can you please post your toolbar xml too? By not showing up at all, does it mean the app is running but nothing shows up or it's not running (compile error)? – Fadils Nov 04 '14 at 22:31
0

try like this way may help you,

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
        .add(R.id.container, new MainFragment())
        .commit();
    }

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, 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;
    }
    return super.onOptionsItemSelected(item);
}

}
Akash Moradiya
  • 3,318
  • 1
  • 14
  • 19
0

First check the import in MainFragment. I'm sure it is

import android.app.Fragment;

it should be Fragment from support library

import android.support.v4.app.Fragment;

In MainACtivity instead of

if (savedInstanceState == null) {
     getFragmentManager().beginTransaction()
           .add(R.id.container, new MainFragment())
           .commit();
}

Use

getSupportFragmentManager().beginTransaction()
     .add(R.id.container, new MainFragment())
     .commit();

check in manifest file if minSdkVersion is 7 or 8 if it is greater than 10 then use Activity and Fragment from android.app.Fragment

android:minSdkVersion="8"

And you have to add appcompat v21 or appcompat v7 library properly and use a Theme from that library you have added.

Kaushik
  • 6,150
  • 5
  • 39
  • 54
0

Your Toolbar is on top of your fragment. Setting the found Toolbar view as ActionBar most likely causes another pass on layout which makes the fragment view disappear (most likely squashed under the Toolbar).

EDIT:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@id/toolbar"
        android:align_parentTop="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <FrameLayout
        android:id="@id/content"
        android:layout_below="@id/toolbar"
        android:align_parentBottom="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>    

</RelativeLayout>
Joel Bodega
  • 644
  • 5
  • 8
0

The question is solved, but it wasn't working for me.

So i will post my workaround. I simply disable the drawer indicator of the toolbar and enable it again. So the toolbar automatically shows the arrow. Try the following code in your onCreate(..)

   supportFragmentManager.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
        @Override
        public void onBackStackChanged() {
            int stackHeight = supportFragmentManager.getBackStackEntryCount();
            ActionBar supportActionBar = getSupportActionBar();
            if (stackHeight > 0) {
                mDrawerToggle.setDrawerIndicatorEnabled(false);
            } else {
                mDrawerToggle.setDrawerIndicatorEnabled(true);
                mDrawerToggle.syncState();
            }
        }
    });
myfknoll
  • 455
  • 5
  • 15