Nothing shown If replace current fragment with another one - android

I have such layout:
frame_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/items"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
Next, in FragmentActivity I do:
setContentView(R.layout.fragment_layout);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
String tag = MyListFragment.class.getName();
MyListFragment fragment = (MyListFragment)
getSupportFragmentManager().findFragmentByTag(tag);
if (fragment == null) {
fragment = new MyListFragment();
ft.add(R.id.items, fragment, tag);
}
else {
ft.replace(R.id.items, fragment, tag);
}
ft.addToBackStack(null);
ft.commit();
When I call this code once, fragment shown perfectly! But, when it called twice I see no content!
After some investigation, I found that problem's caused by this:
setContentView(R.layout.fragment_layout);
E.g., when it is called once, fragment content is perfectly shown! But I need to call setContentView a lot of times to show another fragments.
Where's the mistake?
P.S. It's possible to make MyListFragment hardcoded into XML, but this does not fit to me, because I need to replace layout contents with other fragments.

As nobody answers, I found another solution: keep layout for every frame and show/hide them.
That's how I did it.... Main layout:
<?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">
<FrameLayout
android:id="#+id/container1"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<FrameLayout
android:id="#+id/container2"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
That's how I show fragment2 (belongs to container2) in fragment activity:
findViewById(R.id.container1).setVisibility(View.GONE);
findViewById(R.id.container2).setVisibility(View.VISIBLE);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
String tag = MyFragment.class.getName();
Fragment fragment = (Fragment)getSupportFragmentManager().findFragmentByTag(tag);
if (fragment == null) {
fragment = new MyFragment();
ft.add(R.id.container2, fragment, tag);
}
ft.addToBackStack(null);
ft.commit();

Related

Fragment transaction add/replace not working from within a fragment

I have been breaking my head over this issue from a few days. Nothing seems to be working.
The following is the code where the fragment resides.
<?xml version="1.0" encoding="utf-8"?>
<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"
android:theme="#style/Theme.Breathe"
tools:context=".MainActivity">
<fragment
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/mainFrag"
android:tag="fragment"
class="com.sneaky.example.HomeFrag"/>
</FrameLayout>
The following is the code from mainactivity where the first transaction happens. The below code executes and functions as expected.
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
Fragment fragment = new HomeFrag();
fragmentTransaction.add(R.id.mainFrag,fragment);
fragmentTransaction.commit();
The following is the code from HomeFrag that we added in the previous snippet. This code seems to be doing nothing. Clicking the card has no effect. CardView onClick seems to be working fine because Log.d and toasts execute onClick.
card.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fragment newFragment = new FragSec();
transaction.replace(R.id.mainFrag, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
Any help would be really appreciated. :D
If you already add HomeFrag by code, Try to change XML to this without declare <fragment/> inside
<?xml version="1.0" encoding="utf-8"?>
<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"
android:theme="#style/Theme.Breathe"
android:id="#+id/mainFrag"
tools:context=".MainActivity"/>

Fragments overlapping on FrameLayout

I am trying to add multiple fragments to a FrameLayout. I need to add them one below another. But they are overlapping.
main_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
MainActivity.java
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
// Create instance of fragments
FragmentPrimaryList firstFrag = new FragmentPrimaryList();
FragmentSecondaryList secFrag = new FragmentSecondaryList();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// add fragment to the fragment container layout
fragmentTransaction.add(R.id.fragment_container,firstFrag);
fragmentTransaction.add(R.id.fragment_container,secFrag);
fragmentTransaction.commit();
}
}
They looks like this. How can I solve this?
Use the replace method instead of the add method.
The replace method hides the current fragment before adding new one, the add method simply adds the new fragment without disposing off the older fragment.
In order to retain the back stack, use the fragment transaction with backstack management.
I need to display both fragments at same time, one below another
You can try the following layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
Now add fragmments using:
fragmentTransaction.add(R.id.fragment_container1,firstFrag);
make sure you Fragment's layout_height = "wrap_content"
Add background to your fragments and make "android:clickable="true" to parent layout.
Do this way:
First fragment
FragmentPrimaryList firstFrag = new FragmentPrimaryList();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// add fragment to the fragment container layout
fragmentTransaction.add(R.id.fragment_container,firstFrag);
fragmentTransaction.commit();
second fragment
FragmentSecondaryList secFrag = new FragmentSecondaryList();
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_container,secFrag);
fragmentTransaction.commit();

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().

How to add fragment inside a fragment of ViewPager with same id where each fragment added in view pager is same?

I am trying to add a fragment inside another fragment which is added in view pager. All fragment inside view pager are same. I am just viewing them with swipe. On button click, i add a fragment inside our main fragment by using add fragment. On doing this, this new fragment is being added to last fragment of view pager instead of current one.
Reason which seems to me is when we look for fragment, we still have last fragment active and that fragment adds this new fragment on top of it.
Everything works fine for first
public void onAddFragmentClick(View view) {
if (!isInfoShown) {
FragmentTransaction fragmentTransaction = this.getActivity().getSupportFragmentManager().beginTransaction();
fragment = new MainFragment();
fragmentTransaction.replace(R.id.fragment, fragment, "detail fragment");
fragmentTransaction.commit();
isInfoShown = true;
} else {
FragmentTransaction fragmentTransaction = this.getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.remove(fragment);
fragmentTransaction.commit();
isInfoShown = false;
}
}
Main Fragment which is added in viewpager is
<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">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" >
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.wedm.wedme.ui.fragment.PictureScreenFragment"
android:id="#+id/fragment"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal|top" />
</FrameLayout>
</RelativeLayout>
For nested fragments you should use the child fragment manager of the fragment where you want to add fragments. Hope this helps.

Changing fragments is showing last fragment for 1 second

I am changing fragments inside framelayout.This is my main activity layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" xmlns:app="http://schemas.android.com/apk/res/com.impact.ribony">
<com.astuetz.PagerSlidingTabStrip
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="48dip"
android:visibility="gone"
app:pstsIndicatorColor ="#0B0B16"
app:pstsShouldExpand="true" />"
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tabs">
</android.support.v4.view.ViewPager>
</RelativeLayout>
Firstly I am showing login fragment then user logins I am hiding framelayout.Because I am using viewpager.If user wants go to settings I am loading settings fragment then I am showing framelayout again.
But I have a problem,you can see it here: http://youtu.be/H6AO7yYusjM
You can see when user goes to settings fragment,firstly login fragment showing for 1 second then settings fragment is loading.How can I prevent this ? (When I try to change fragment always showing last fragment for 1 second not just login fragment)
If user logins successfully I am removing login fragment with following lines:
pager.setVisibility(View.VISIBLE);
tabs.setVisibility(View.VISIBLE);
FrameLayout layout = (FrameLayout)findViewById(R.id.container);
layout.setVisibility(View.GONE);
I am loading login fragment with following code:
FrameLayout layout = (FrameLayout)findViewById(R.id.container);
layout.setVisibility(View.VISIBLE);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
LoginFragment conv = new LoginFragment();
loginObj=LoginFragment.newInstance("asd");
fragmentTransaction.replace(R.id.container, loginObj,"LoginFragment");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
actionBar.hide();
pager.setVisibility(View.GONE);
I am loading settings fragment with following code:
FrameLayout layout=(FrameLayout)findViewById(R.id.container);
pager.setVisibility(View.GONE);
tabs.setVisibility(View.GONE);
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
SettingsFragment conv=new SettingsFragment();
SettingsFragment.newInstance(LOGGED_USERNAME);
fragmentTransaction.replace(R.id.container,conv);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
layout.setVisibility(View.VISIBLE);

Categories

Resources