I am trying to create a swappable fragements on my android app
Only half the part of fragment seems to be replacing
screenshot:
The xml file for the main is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/app_bar_main"
tools:context=".MainActivity"
android:id="#+id/contentlayout">
<fragment
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="#+id/header_menufragment"
class="com.example.www.newapp.header_menu" />
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/dynamicfragment"
class="com.example.www.newapp.mainfragment"/>
</LinearLayout>
the whole dynamicfragment is supposed to be replaced by projectfragment on click of Project button.
project.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getFragmentManager()
.beginTransaction()
.replace(R.id.dynamicfragment, new projectfragment())
.addToBackStack(null)
.commit();
}
});
the projectfragment class has
public class projectfragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_project, container,false);
}
}
the layout file is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:background="#color/colorDarkRed">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight=".5">
<ImageButton
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
<ImageButton
android:id="#+id/img2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
<ImageButton
android:id="#+id/img3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="#drawable/imgfile"/>
</LinearLayout>
Kindly help me solve this issue.
You cannot replace a fragment defined statically in the layout file. You can only replace fragments that you added dynamically via a FragmentTransaction.
Use FrameLayout as container and replace Fragment inside that.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent" android:layout_height="match_parent">
<FrameLayout android:id="#+id/fragment_container" android:layout_weight="1"
android:layout_width="match_parent" android:layout_height="match_parent" />
</LinearLayout>
On Activity add Fragment same as you do:
getFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, new projectfragment())
.addToBackStack(null)
.commit();
For more info refer below link:
link1
link2
that worked for me try this way
WishaFriend ff = new WishaFriend();
android.app.FragmentManager fm = getActivity().getFragmentManager();
android.app.FragmentTransaction ft = fm.beginTransaction();
ft.replace(((ViewGroup) getView().getParent()).getId(), ff);
ft.commit();
dialog.dismiss();
try add this
ft.replace(((ViewGroup) getView().getParent()).getId(), ff);
I'm having overlay in my application, I did a search and I found some solutions that apparently were simple, but did not work for me. Probably I have a little confusion because I don't understand why it's not working.
So, in my activity_main.xml file I have a FrameLayout element 'container_body'. In my MainActivity I'm set the content of this xml. In this activity I have a drawer-list with some fragments.
When I click in any fragment I'm seeing overlay.
In MainActivity I'm calling the displayView that is responsible to make the fragments transitions, I'm replacing the container_body element for the content of fragment, but this doesn't working.
Any suggestions, please?
[Images] The first screen
The fragment screen
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
private void displayView(int position) {
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (position) {
case 0:
fragment = new PostView();
title = getString(R.string.title_section1);
break;
case 1:
fragment = new RegionView();
title = getString(R.string.title_section2);
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();
// set the toolbar title
getSupportActionBar().setTitle(title);
}
activity_main.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:contentInsetStart="72dp" />
<FrameLayout
android:id="#+id/container_body"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1.25"
android:layout_gravity="top|left|bottom|right">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/tile_bg"
android:orientation="vertical" >
<ListView
android:id="#+id/list_view_messages"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#null"
android:divider="#null"
android:transcriptMode="alwaysScroll"
android:stackFromBottom="true">
</ListView>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/llMsgCompose"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="horizontal"
android:weightSum="3" >
<EditText
android:id="#+id/inputMsg"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#color/bg_msg_input"
android:textColor="#color/text_msg_input"
android:paddingLeft="6dp"
android:paddingRight="6dp"/>
<Button
android:id="#+id/btnSend"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#color/bg_btn_join"
android:textColor="#color/white"
android:text="#string/btn_send" />
</LinearLayout>
</FrameLayout>
<com.heinrichreimersoftware.materialdrawer.DrawerView
android:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true" />
Fragment
public class PostView extends Fragment implements CommonPresenter {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_post, null);
return (view);
}
This is a really lame question, but I really cannot figure out why it happens, so please help me out.
I have a simple activity and I add a fragment to it dynamically. The problem is that once the fragment is added to the activity, the activity layout is also visible. Why does this happen?
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnLoad = (Button) findViewById(R.id.btn1);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
HelloFragment hello = new HelloFragment();
fragmentTransaction.add(R.id.rootView, hello, "HELLO");
fragmentTransaction.commit();
}
};
btnLoad.setOnClickListener(listener);
}
}
activity_main.xml
<LinearLayout 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" tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rootView"
android:orientation="vertical">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="OK"/>
</LinearLayout>
</LinearLayout>
HelloFragment.java
public class HelloFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/** Inflating the layout for this fragment **/
View v = inflater.inflate(R.layout.hello_fragment_layout, null);
return v;
}
}
hello_fragment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="fragment 1"/>
</LinearLayout>
Convention is to have a FrameLayout that you use fragmentTransaction.replace()/add() on.
You shouldn't have views like TextView and Button in your MainActivity layout file. Instead, have those in another fragment and just have a FrameLayout in your MainActivity xml. Load the fragment with TextView/Button first, and call replace on button click with your new fragment.
Set a background color to your fragment linearLayout and make it clickable
The fragment is just another element on the layout. Adding it to the layout does not automatically remove other views. If you want the fragment to replace the content of rootView you need to remove rootview's children views first. Something like:
((LinearLayout)findViewById(R.id.rootView)).removeAllViews();
So your onClick method should look like this:
#Override
public void onClick(View v) {
((LinearLayout)findViewById(R.id.rootView)).removeAllViews();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
HelloFragment hello = new HelloFragment();
fragmentTransaction.add(R.id.rootView, hello, "HELLO");
fragmentTransaction.commit();
}
your rootview work as container for both fragment and your views on activity. check it and make separate view for fragment.
I know it's very late to answer this question but for those who are still looking do the following in your activity layout:
<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" tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="OK"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rootView"
android:orientation="vertical">
</RelativeLayout>
And fragment layout to following:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="#color/white" <!--Add this-->
android:layout_height="match_parent">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="OK"/>
</LinearLayout>
activity_main.xml
<FrameLayout 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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rootView"
android:orientation="vertical">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="OK"/>
</LinearLayout>
<FrameLayout
android:id="#+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</FrameLayout>
MainActivity.java
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
HelloFragment hello = new HelloFragment();
fragmentTransaction.add(R.id.fragmentContainer, hello, "HELLO");
fragmentTransaction.commit();
}
};
I have a solution for this to achieve what you can do is make two fragment one for textview and button your showing as default when activity is opened and another which you have already created as HelloFragment.
From Activity onCreate() method call the first fragment with default textview and button and from the click listener of that button call HelloFramgent.
Step1: Crate another fragment with any name MainFragment having a xml layout as follows:-
main_fragment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="OK"/>
</LinearLayout>
MainFragment.java - Class
public class MainFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/** Inflating the layout for this fragment **/
View v = inflater.inflate(R.layout.main_fragment_layout, container, false);
Button btnLoad = (Button) v.findViewById(R.id.btn1);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
HelloFragment hello = new HelloFragment();
fragmentTransaction.add(R.id.rootView, hello, "HELLO");
fragmentTransaction.commit();
}
};
btnLoad.setOnClickListener(listener);
return v;
}
}
Step2: Call the MainFragment from activity onCreate() method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
MainFragment mainFragment = new MainFragment();
fragmentTransaction.add(R.id.rootView, mainFragment, "MAIN_FRAGMENT");
fragmentTransaction.commit();
}
Build your user interface in Fragments (In most cases one Fragment equals one screen) and then use Activities to arrange and display those Fragments.
check this: click here to view detailed answer
You have two linear layouts, one inside another, so you need to do the following steps:
Set id to 1 linear layout rootView and next to the childview.
Find your child view:
View view = findViewById(R.id.child_view);
view.setVisibility(View.INVISIBLE);
Replace your rootView with your fragment:
getSupportFragmentManager().beginTransaction().add(R.id.rootview, new blankFragment())
.addToBackStack(null).commit();
Then override:
#override
public void onBackPressed() {
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
if (fm.getBackStackEntryCount() > 0) {
view.setVisibility(View.VISIBLE);
}
super.onBackPressed();
}
The Activity views are not being masked against the Fragment's views, so both appear. One solution is to load the fragment into a frame in the main activity's layout which has a slight elevation (2 dp). When the fragment is loaded its base elevation will cause it to always draw over the activity. Set the fragments parent container with a color to hide the activity views.
MasterActivity layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Activity Title"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:textSize="24sp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:layout_centerHorizontal="true"
android:textAllCaps="false"
android:text="Button1" />
<Button
android:id="#+id/button2"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_centerHorizontal="true"
android:text="Button2" />
<!-- Fragment holder, with elevation -->
<FrameLayout
android:translationZ="2dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragHolder"
/>
The Fragment layout has a background color:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:layout_margin="10dp">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Test Background Task"
android:textSize="24sp" />
</RelativeLayout>
Make your First Layout invisible while calling fragment and again while returning from fragment make your main view visible and remove fragment at that time so it will not conflict with each other it worked for really well just check it once
I can't replace the fragments using the codes below. I mean that the second fragment appeared under the first fragment. The first fragmnent didn't hide as I expected. What is wrong here?
Thanks a lot!
My activity :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final FragmentManager fm = this.getFragmentManager();
final Fragment fragmenttwo = new FragmentTwo();
Button fmchangebutton = (Button) this.findViewById(R.id.fmchangebutton);
fmchangebutton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment1, fragmenttwo);
ft.commit();
}
});
};
};
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/fmchangebutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Change Fragment" />
<fragment
android:id="#+id/fragment1"
android:name="com.example.fragementex.FragmentOne"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
fragmnetone.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" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="clip_horizontal"
android:ems="10"
android:gravity="center_vertical|center_horizontal|top|fill_vertical"
android:text="This is Fragment One" >
</EditText>
</LinearLayout>
fragmnettwo.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" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="clip_horizontal"
android:ems="10"
android:gravity="center_vertical|center_horizontal|top|fill_vertical"
android:text="This is Fragment Two" >
</EditText>
</LinearLayout>
You cannot replace a fragment attached to the layout, you need to create a host container as FrameLayout like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/fmchangebutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Change Fragment" />
<FrameLayout
android:id="#+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Then, you will able to - first - add and display the first fragment and replace it with the second as follows:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set the layout which contains the framelayout
setContentView(R.layout.activity_main);
final FragmentManager fm = this.getFragmentManager();
final Fragment fragmentone = new FragmentOne();
final Fragment fragmenttwo = new FragmentTwo();
// if first initialization
if(savedInstanceState == null) {
// display with "add" the first fragment
this.getFragmentManager().beginTransaction()
.add(R.id.frame_layout, fragmentone).commit();
}
Button fmchangebutton = (Button) this.findViewById(R.id.fmchangebutton);
fmchangebutton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
FragmentTransaction ft = fm.beginTransaction();
// "replace" the fragment inside the framelayout
ft.replace(R.id.frame_layout, fragmenttwo);
ft.commit();
}
});
}
I created an activity with a navigation drawer and a FrameLayout. This frame layout should contain the fragment which corresponds the selected navigation item. So I followed the instructions in android tutorials, but it doesn't work.
Problem:
It seems to me the fragments are only added and not replaced. What am I doing wrong?
My activity xml:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainViewDrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="#+id/mainViewContentFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="#dimen/drawer_content_padding" >
</FrameLayout>
<ListView android:id="#+id/mainViewDrawerList"
android:layout_width="#dimen/drawer_size"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="none"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#color/drawer_background"
/>
</android.support.v4.widget.DrawerLayout>
my first fragment xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="#dimen/activity_vertical_margin"
android:paddingRight="#dimen/activity_vertical_margin"
android:paddingTop="#dimen/activity_horizontal_margin"
android:paddingBottom="#dimen/activity_horizontal_margin"
android:id="#id/fragmentProfile">
<TextView
style="#android:style/TextAppearance.Small"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/mainViewProfileGivenNameLabel"
/>
<TextView
android:id="#+id/profileGivenNameTextView"
style="#android:style/TextAppearance.Large"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
my second fragment xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#id/fragmentProtocol">
<ListView android:id="#+id/protocolTableListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:transcriptMode="alwaysScroll"
android:cacheColorHint="#00000000"
android:listSelector="#android:color/transparent"/>
</LinearLayout>
my transition method in the activity:
private void switchToFragment(int navigationId, boolean addToBackStack){
FragmentManager fragmentManager = getFragmentManager();
String fragmentTag = null;
boolean fragmentCreated = false;
switch(navigationId) {
case NavigationItemProvider.NAVIGATIONITEM_PROFILE:
fragmentTag = NAVIGATIONTAG_PROFILE;
break;
case NavigationItemProvider.NAVIGATIONITEM_PROTOCOL:
fragmentTag = NAVIGATIONTAG_PROTOCOL;
break;
}
// determine if the fragment is already instanciated otherwise create
FragmentBase fragment = (FragmentBase)fragmentManager.findFragmentByTag(fragmentTag);
if (fragment == null){
if (fragmentTag == NAVIGATIONTAG_PROFILE) {
fragment = new ProfileFragment();
fragmentCreated = true;
}
else if(fragmentTag == NAVIGATIONTAG_PROTOCOL) {
fragment = new ProtocolFragment();
fragmentCreated = true;
}
}
// switch to fragment
if (fragment.isVisible()){
return;
}
FragmentTransaction transaction = fragmentManager.beginTransaction();
if (fragmentCreated) {
transaction.replace(R.id.mainViewContentFrame, fragment, fragmentTag);
}
else{
transaction.show(fragment);
}
if(addToBackStack)
{
transaction.addToBackStack(null);
}
transaction.commit();
}
solved the problem:
In onCreate I returned the result of super.onCreate and not the result of Layoutinfalter.inflate and in addition I didn't call inflate with last parameter set to false.