Android - Hide/Show Fragment showing the wrong fragment - android

In the first time that I change the visibility of the fragments in my app, the fragment showed isn`t the fragment that I set to show.
My layout:
<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"
android:orientation="vertical"
tools:context=".MainActivity" >
<fragment
android:id="#+id/act_main_frag_top_bar"
class="br.com.myapp.fragments.TopBarFragment"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
<fragment
android:id="#+id/act_main_frag_toolbar"
class="br.com.myapp.fragments.ToolbarFragment"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
<!-- Tabs -->
<fragment
android:id="#+id/act_main_frag_twitter"
class="br.com.myapp.fragments.TwitterFragment"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:visibility="gone"/>
<fragment
android:id="#+id/act_main_frag_facebook"
class="br.com.myapp.fragments.FacebookFragment"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:visibility="gone"/>
<fragment
android:id="#+id/act_main_frag_tv_show"
class="br.com.myapp.fragments.TVShowFragment"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:visibility="gone"/>
<fragment
android:id="#+id/act_main_frag_info"
class="br.com.bluepen.mixtv.fragments.InfoFragment"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:visibility="gone"/>
<fragment
android:id="#+id/act_main_frag_streaming"
class="br.com.myapp.fragments.StreamingFragment"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:visibility="gone"/>
</LinearLayout>
And myActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager manager = getSupportFragmentManager();
mStreamingFrag = (StreamingFragment) manager.findFragmentById(R.id.act_main_frag_streaming);
mFacebookFrag = (FacebookFragment) manager.findFragmentById(R.id.act_main_frag_facebook);
mTwitterFrag = (TwitterFragment) manager.findFragmentById(R.id.act_main_frag_twitter);
mTVShowFrag = (TVShowFragment) manager.findFragmentById(R.id.act_main_frag_tv_show);
mInfoFrag = (InfoFragment) manager.findFragmentById(R.id.act_main_frag_info);
mToolbar = (ToolbarFragment) manager.findFragmentById(R.id.act_main_frag_toolbar);
mToolbar.setListener(this);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.show(mStreamingFrag);
transaction.commit();
}
After the onCreate, the fragment getting showed is the TwitterFragment, and not the StreamingFragment that I set to show in the final of the onCreate method.

I find the problem, I changed to hide all the fragments in the onCreate. Aparentelly the android:visibility="gone" in the xml didn`t do this.
Here is the code of the onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
mStreamingFrag = (StreamingFragment) manager.findFragmentById(R.id.act_main_frag_streaming);
transaction.show(mStreamingFrag);
mFacebookFrag = (FacebookFragment) manager.findFragmentById(R.id.act_main_frag_facebook);
transaction.hide(mFacebookFrag);
mTwitterFrag = (TwitterFragment) manager.findFragmentById(R.id.act_main_frag_twitter);
transaction.hide(mTwitterFrag);
mTVShowFrag = (TVShowFragment) manager.findFragmentById(R.id.act_main_frag_tv_show);
transaction.hide(mTVShowFrag);
mInfoFrag = (InfoFragment) manager.findFragmentById(R.id.act_main_frag_info);
transaction.hide(mInfoFrag);
transaction.commit();
}
I don`t know if I can set the fragment to hide in the XML.

I had the same issue with 2 buttons (button1 and button2), when i had to hide the first button , the 2nd was hiding and the first was doing nothing... Everything i've done was CTRL+C the xml and CTRL+V back... Hope it works...

Related

Check if Fragment exists

I am following the vogella tutorial about multi-pane development in Android.
Now I would like to check if the a detail fragment is existing (multi-pane layout activated) to remove it and add it again with fresh data.
I need that to update the detail view when the user selects something in the main fragment.
As suggested in the tutorial I am checking for the Fragment like that:
ReviewMaschineFragment fragment = (ReviewMaschineFragment) getFragmentManager().
findFragmentById(R.id.detailreviewcontainer);
if (fragment == null || ! fragment.isInLayout()) {
Log.i("Detail Fragment", "Start new activity");
}
else {
Log.i("Detail Fragment", "Update...");
}
My Problem is that it always gets false even if the fragment exists.
Why is it not detecting the fragment as existing if it is present in the multi-pane layout?
I add my fragments like that to the layout:
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar_main);
setSupportActionBar(toolbar);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.maschinelistcontainer, new MaschineFragment());
if(getResources().getBoolean(R.bool.dual_pane)){
ft.add(R.id.detailreviewcontainer, new ReviewMaschineFragment());
}
ft.commit();
The tablet layout file:
<?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"
>
<include
android:id="#+id/toolbar_main"
layout="#layout/toolbar"
></include>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="9"
android:orientation="horizontal" >
<FrameLayout
android:id="#+id/maschinelistcontainer"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
/>
<FrameLayout
android:id="#+id/detailreviewcontainer"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="6"
/>
</LinearLayout>
</LinearLayout>
While creating fragment add tag value with it
eg
ft.add(R.id.maschinelistcontainer, new MaschineFragment(), "sometag");
You can use findFragmentByTag() function to get fragment if it is giving null the fragment is not exist.
Fragment fragmentA = fragmentManager.findFragmentByTag("sometag");
if (fragmentA == null) {
// not exist
} else {
//fragment exist
}
for example:- http://wiki.workassis.com/android-load-two-fragments-in-one-framelayout/
Pls change
ReviewMaschineFragment fragment = (ReviewMaschineFragment) getFragmentManager().
to
ReviewMaschineFragment fragment = (ReviewMaschineFragment) getSupportFragmentManager().

View on top of map disappears while using it with a DrawerLayout

I'm creating a layout which contains a maps (com.google.android.gms.maps.SupportMapFragment) & a mini layout to enter origin & destination(pickup_dropoff_LL LinearLayout).
XML
<RelativeLayout
android:id="#+id/main_RL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/rideType_LL">
<include
android:id="#+id/pickup_dropoff_LL"
layout="#layout/layout_pickup_dropoff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp" />
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />
</RelativeLayout>
Initially this had a corresponding Activity but I had to introduce a DrawerLayout so I had to convert it to a Fragment.
After doing that my pickup_dropoff_LL disappears.
It is shown on the screen for a micro second and then it disappears.
I suspect the issue is somewhere here but I'm not sure.
SupportMapFragment fm;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_main_activity, container, false);
fm = new SupportMapFragment() {
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
map = fm.getMap();
if (map != null) {
map.setMyLocationEnabled(true);
fm.getMapAsync(MainActivity2.this);
}
}
};
getChildFragmentManager().beginTransaction().add(R.id.main_RL, fm).commit();
return rootView;
}
1. I checked visibility for pickup_dropoff_LL. It's showing as 0, which means View.VISIBLE. But it is not visible.
2. As suggested by some links, I tried placing an empty view of same height & width over my map. Even that didn't work.
So how do I stop the view from disappearing?? Please help.
Try drawing your layout last. Put the Include after the Fragment
<RelativeLayout
android:id="#+id/main_RL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/rideType_LL">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />
<include
android:id="#+id/pickup_dropoff_LL"
layout="#layout/layout_pickup_dropoff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp" />
</RelativeLayout>
Have you tried moving your content inside of your drawer and not behind it?
<!-- Drawer Host inside the parent relative layout -->
<android.support.v4.widget.DrawerLayout
android:id="#+id/main_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Hold Fragment here -->
<FrameLayout
android:id="#+id/frag_main_host"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- Drawer -->
<RelativeLayout
android:id="#+id/drawer_pane"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start">
<!-- Menu list for drawer -->
<ListView
android:id="#+id/menu_list"
android:layout_height="match_parent"
android:layout_width="280dp"
android:dividerHeight="1sp"
android:gravity="start"
android:choiceMode="singleChoice" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
Putting anything below your view would make it not appear when opened although it is visible in property. In your class you can swap the fragment as desired by calling the fragment manager and seleting a fragment at whatever list index is selected. I suggest use an internal listener and just call a method to pick the view and swap it. Like below:
Listener (Internal class):
class MainDrawerItemClickListener implements AdapterView.OnItemClickListener{
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id ){
getView(position);
// close the drawers once selected
mDrawerLayout.closeDrawers();
}
}
That method will call this method in your main class:
// select the view
public void getView( int position ){
Fragment frag = null;
switch( position ){
case 0:
frag = new MyFragment();
break;
case 1:
frag = new MyOtherFragment();
break;
default:
break;
}
if( frag != null ){
swapView( frag );
}
}
// swaps the fragments
public void swapView( Fragment frag ){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace( R.id.frag_main_host, frag ).commit();
}
Hope it helps, I know this is an old question, but I am sure someone will have the same question.

Replacing fragments in one activity

I am able to add a fragment to my fragmentlayout, but when I'm trying to replace the fragment with a new one the screen is blank. I can add a new fragment but then it's not scrollable. I'm also not able not remove a fragment.
Does someone know what I'm doing wrong? I red a lot about it and I feel like I have tried everything..
The framelayout is defined static:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>'
And this is the method for switching fragments:
void TabOnTabSelected (object sender, ActionBar.TabEventArgs tabEventArgs)
{
ActionBar.Tab tab = (ActionBar.Tab)sender;
var fragmentTransaction = FragmentManager.BeginTransaction ();
switch (tab.Text) {
case "vandaag":
if (currentFragment != null) {
fragmentTransaction.Remove (FragmentManager.FindFragmentByTag ("gister"));
}
if (fragmentToday == null) {
fragmentToday = WZWVDataOverview.NewInstance (DateTime.Now.AddDays (0).ToString ("d-M-yyyy"));
}
fragmentTransaction.Add (Resource.Id.fragment_container, fragmentToday, "vandaag");
currentFragment = fragmentToday;
break;
case "gister":
if (currentFragment != null) {
fragmentTransaction.Remove (currentFragment);
}
if (fragmentYesterday == null) {
fragmentYesterday = WZWVDataOverview.NewInstance (DateTime.Now.AddDays (-1).ToString ("d-M-yyyy"));
}
currentFragment = fragmentYesterday;
fragmentTransaction.Add (Resource.Id.fragment_container, currentFragment, "gister");
break;
case "morgen":
if (currentFragment != null) {
fragmentTransaction.Remove (currentFragment);
}
if (fragmentTomorrow == null) {
fragmentTomorrow = WZWVDataOverview.NewInstance (DateTime.Now.AddDays (+1).ToString ("d-M-yyyy"));
}
fragmentTransaction.Add (Resource.Id.fragment_container, fragmentTomorrow);
currentFragment = fragmentTomorrow;
break;
}
fragmentTransaction.Commit ();
}
XML of the framelayout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/list" />
</LinearLayout>
Huge thanks in advance!
You can try using the .replace method. If you do not wish to be able to move back to the previous fragment than you can remove addToBackStack().
This may need to be altered slightly to fit your application.
FragmentManager fragmentManager = getFragmentManager();
Fragment mFragment = new DetailFragment();
mFragment.setArguments(bundle);
fragmentManager.beginTransaction().replace(R.id.fragment_container, mFragment).addToBackStack("DETAILFRAGMENT").commit();
POSSIBLE SECOND SOLUTION:
Check to see how your Fragment layout XML is. Possibly try using a FrameLayout as the parent.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.modup.fragment.DetailFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
</FrameLayout>
I have open fragments from navigation view as items are selected from it.(I have use DataBinding Concept)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout>
<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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.k15.projectkhyatisalesapp.activities.MainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
app:titleTextColor="#ffffff"
android:background="#ef6c00"
android:minHeight="?attr/actionBarSize"
app:popupTheme="#style/AppTheme.PopupOverlay"
android:theme="#style/ThemeOverlay.AppCompat.Dark"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="55dp"
android:orientation="horizontal">
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:layout_width="wrap_content"
android:id="#+id/activity_main_frame_layout"
android:layout_height="wrap_content"/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/activity_main_navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="#menu/listnavigation" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
</RelativeLayout>
<layout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
//Declaration of Variables
private ActivityMainBinding mainBinding; //for binding values in xml
private ActionBarDrawerToggle mDrawableToogle; //This class provides a handy way to tie together the functionality of DrawerLayout and the framework ActionBar to implement the recommended design for navigation drawers.
private Fragment fragment;
private Class fragmentClass = null; //pointing to class of fragment
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
setSupportActionBar(mainBinding.toolbar); //o designate a Toolbar as the action bar for an Activity
setUpDrawer(mainBinding.activityMainNavigation); //for select items in navigationView
mDrawableToogle = setupToogle();
mainBinding.drawerLayout.addDrawerListener(mDrawableToogle);
}
//private method to set navigationView
private void setUpDrawer(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
try {
selectItem(item); //call when item is selected from navigationView
} catch (IllegalAccessException | InstantiationException e) {
e.printStackTrace();
}
return true;
}
});
}
//items are selected from navigationView
public void selectItem(MenuItem menuItem) throws IllegalAccessException, InstantiationException {
switch (menuItem.getItemId()) {
case R.id.Home:
fragmentClass = Home.class;
break;
case R.id.Product:
fragmentClass = Product.class;
break;
case R.id.Media:
fragmentClass = Media.class;
break;
}
assert fragmentClass != null;
fragment = (Fragment) fragmentClass.newInstance();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(mainBinding.activityMainFrameLayout.getId(), fragment).commit(); //change the fragment
setTitle(menuItem.getTitle());
menuItem.setCheckable(true); //for set selected item
mainBinding.drawerLayout.closeDrawers();
}
#Override
protected void onPostCreate(#Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawableToogle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawableToogle.onConfigurationChanged(newConfig);
}
private ActionBarDrawerToggle setupToogle() {
return new ActionBarDrawerToggle(this, mainBinding.drawerLayout, mainBinding.toolbar, R.string.open, R.string.close);
}
menu/listnavigation.xml is attached to navigation view I contains items of navigation view.
In my code as items are selected from navigation view accordingly fragment is replaced. Here Home, Product, Media are my fragment, you can create any fragment.
Try using fragmentTransaction.replace() instead of fragmentTransaction.add() and comment out code for removing fragments. When you replace the fragment - it gets removed. Also - remember to call: fragmentTransaction.commit() when you are done with replacing fragments.

How to add another fragment on the existing fragment?

I am trying to create tab layout with fragments. When i add another fragment on item click it should show another fragment above it. I am unable to get the goal. Fragment just goes above the existing one but both the fragments content are visible. Please don't throw the link towards any developer page for explanation.
Here is my code
home_activity.xml
<TabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="#+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.example.tabsfragment.School_Fragment" >
</fragment>
<fragment
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.example.tabsfragment.Sports_Fragment" >
</fragment>
</FrameLayout>
</LinearLayout>
</TabHost>
Home_Activity.java
public class Home_Activity extends FragmentActivity {
TabHost mHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_activity);
mHost = (TabHost) findViewById(android.R.id.tabhost);
mHost.setup();
TabSpec schoolSpec = mHost.newTabSpec("School").setIndicator("School")
.setContent(R.id.tab1);
mHost.addTab(schoolSpec);
TabSpec sportSpec = mHost.newTabSpec("Sports").setIndicator("Sportss")
.setContent(R.id.tab2);
mHost.addTab(sportSpec);
mHost.setCurrentTab(1);
}
entertainment.xml
<FrameLayout
android:id="#+id/frameContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</FrameLayout>
I am adding another fragment on list_item_click inside Sports_Fragment
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
FragmentTransaction ft = getFragmentManager().beginTransaction();
Entertainment_Fragment enFragment = new Entertainment_Fragment();
ft.replace(android.R.id.tabcontent, enFragment);
ft.commit();
}
Here is the snapshot :
Any ideas/help with explanation would be highly appreciated.
Thanks!!
First of all, I think it's worth mentioning...
"You cannot replace a fragment defined statically in the layout file. You can only replace fragments that you added dynamically via a FragmentTransaction"
Please see
Android: can't replace one fragment with another
How can I show a new Fragment within a single tab?
Here's the solution.
In home_activity.xml, you should leave your tabcontent empty.
<FrameLayout
android:id="#+id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
Your Home_Activity
private FragmentTabHost mHost;
public void changeFragment() {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
EntertainmentFragment enFragment = new EntertainmentFragment();
ft.replace(R.id.tabcontent, enFragment);
ft.commit();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mHost.setup(this, getSupportFragmentManager(), R.id.tabcontent);
mHost.addTab(mHost.newTabSpec("School")
.setIndicator("School"), SchoolFragment.class, null);
mHost.addTab(mHost.newTabSpec("Sport")
.setIndicator("Sport"), SportsFragment.class, null);
}
In your onIemClick (Sports_Fragment), add this
MainActivity mainAct = (MainActivity)getActivity();
mainAct.changeFragment();
My full project, which is based on your code, is here.
I haven't had a chance to really check why TabHost doesn't work when I tested your code.
But FragmentTabHost works fine for me.
Edit: To fix the overlap problem, you can set OnTabChangeListener:
mHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
if (tabId.equalsIgnoreCase("School")) {
Log.v(TAG, "school");
FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction();
schoolFrag = new SchoolFragment();
ft.replace(R.id.tabcontent, schoolFrag);
ft.commit();
}
if (tabId.equalsIgnoreCase("Sport")) {
Log.v(TAG, "Sport");
FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction();
SportsFragment sportFrag = new SportsFragment();
ft.replace(R.id.tabcontent, sportFrag);
ft.commit();
}
}
});
About the backpress, you can try
ft.addToBackStack(null);

Replacing Fragment : Didn't work

I have a Button in Fragment. If Button is clicked it has to replace the Fragment with another Fragment. The Fragments in activity_main.xml is created statically.
<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"
android:background="#efefef"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3" >
<Button
android:id="#+id/bHome"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="1"
android:background="#000000"
android:padding="3dp"
android:text="Home"
android:textColor="#ffffff" />
<Button
android:id="#+id/bEvents"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="1"
android:background="#f8bd0e"
android:padding="3dp"
android:text="Events" />
<Button
android:id="#+id/bNearby"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="1"
android:background="#f8bd0e"
android:padding="3dp"
android:text="Nearby" />
</LinearLayout>
<LinearLayout
android:id="#+id/llFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<fragment
android:id="#+id/fHome"
android:name="com.example.fragmentstack.Home"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp" />
<fragment
android:id="#+id/fEvents"
android:name="com.example.fragmentstack.Events"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp" />
<fragment
android:id="#+id/fNear"
android:name="com.example.fragmentstack.NearBy"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp" />
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</FrameLayout>
</LinearLayout>
The output of activity_main.xml is
Main Activity:
public class MainActivity extends FragmentActivity implements OnClickListener {
Button home, events, near;
Fragment f1, f2, f3, f4;
FragmentManager manager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
home = (Button) findViewById(R.id.bHome);
events = (Button) findViewById(R.id.bEvents);
near = (Button) findViewById(R.id.bNearby);
home.setOnClickListener(this);
events.setOnClickListener(this);
near.setOnClickListener(this);
manager = getSupportFragmentManager();
f1 = manager.findFragmentById(R.id.fHome);
f2 = manager.findFragmentById(R.id.fEvents);
f3 = manager.findFragmentById(R.id.fNear);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bHome:
if (home.isPressed()) {
home.setBackgroundColor(Color.BLACK);
home.setTextColor(Color.WHITE);
events.setBackgroundResource(R.color.Yellow);
near.setBackgroundResource(R.color.Yellow);
events.setTextColor(Color.BLACK);
near.setTextColor(Color.BLACK);
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.hide(f2);
transaction.hide(f3);
transaction.show(f1);
transaction.commit();
}
break;
case R.id.bEvents:
if (events.isPressed()) {
events.setBackgroundColor(Color.BLACK);
events.setTextColor(Color.WHITE);
home.setBackgroundResource(R.color.Yellow);
near.setBackgroundResource(R.color.Yellow);
home.setTextColor(Color.BLACK);
near.setTextColor(Color.BLACK);
FragmentTransaction transaction1 = getSupportFragmentManager()
.beginTransaction();
transaction1.hide(f1);
transaction1.hide(f3);
transaction1.show(f2);
transaction1.commit();
}
break;
case R.id.bNearby:
if (near.isPressed()) {
near.setBackgroundColor(Color.BLACK);
near.setTextColor(Color.WHITE);
home.setBackgroundResource(R.color.Yellow);
events.setBackgroundResource(R.color.Yellow);
home.setTextColor(Color.BLACK);
events.setTextColor(Color.BLACK);
FragmentTransaction transaction2 = getSupportFragmentManager()
.beginTransaction();
transaction2.hide(f1);
transaction2.hide(f2);
transaction2.show(f3);
transaction2.commit();
}
break;
}
}
}
Now Home extends Fragment. the xml layout of home has one Button and one TextView.Here onClicking Button I'm replacing the Home Fragment with new Update Profile Fragment.
public class Home extends Fragment {
Button btn;
View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
view = inflater.inflate(R.layout.home, container, false);
init();
return view;
}
private void init() {
btn = (Button) view.findViewById(R.id.bUpdate);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fg = new UpdateProfile();
FragmentTransaction fragmentTransaction = getFragmentManager()
.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fg);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
}
}
R.id.fragment_container is present in activity_main.xml. Now the problem I'm facing is when clicking the button it is not replacing the fragment with new Fragment.
Now the problem I'm facing is when clicking the button it is not
replacing the fragment with new Fragment.
The transaction does occur but it's not visible as you add that fragment to a container that will be pushed out of the screen(all of your layout's fragments have width set to match_parent so they will fill the LinearLayout's width and the FrameLayout will be pushed out of the screen).
Also you do the replace transaction on a container that doesn't hold the previous fragments, this means that the old fragments will still be visible on the screen(plus the new added fragment).
The system you setup is not the proper way to handle your scenario. From the images it seems you have a tab like layout and in this case you should have only one container in which all of your fragments will reside(to avoid the extra work of showing/hiding the proper fragment and also provide a decent BACK button experience).
Secondly if you're going to work(do transactions) with those fragments you should really add them programmatically as replace will not go well with a static fragments. There are a lot of tutorials out there on how to make a tab like layout with fragments.

Categories

Resources