My android app has a registration flow setup with an Activity and I load the steps as fragments into the activity layout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
tools:context="com.example.android.ui.RegisterActivity">
<LinearLayout
android:id="#+id/register_container"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></LinearLayout>
</RelativeLayout>
Now each fragment class implements a public interface implemented in the RegisterActivity so that I know to load the next step fragment and I add the new fragment to the backstack
mFragmentTransaction.addToBackStack(mStepOne.TAG);
now it all works fine all the way through 4 steps and I can navigate back through the steps while retaining the inputted data in each fragment IF it stays in the same orientation (portait)
BUT once i change the orientation, the fragment views disappear
if I am up to step 3, I can still hit the back button and it will go back showing me the fragment that is meant to be there by viewing the backstack changed listener
mFragmentManager.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
if(mFragmentManager.getBackStackEntryCount() > 0) {
Log.d("BACK STACK", "TAG (name): " + mFragmentManager.getBackStackEntryAt( (mFragmentManager.getBackStackEntryCount() - 1)).getName());
}
}
});
The count and tags of the fragments is retained but not the views. The RegisterActivity onCreateView loads the terms fragment into the view and it is what remains in view when navigating back through the steps.
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mTerms = new RegisterFragmentTerms();
mFragmentTransaction.add(R.id.register_container, mTerms);
mFragmentTransaction.commit();
It seems to me the views are loaded into the RegisterActivity on top of each other and cleared on orientation change. Is it possible to solve what I'm trying to do? or have I implemented it wrong? It's my first android app :)
Cheers
It seems to be caused by Activity recreation. Have you ever add android:configChanges="orientation|keyboardHidden|screenSize" in your AndroidManifest.xml to your Activity? , if you add that flag to you activity, it will not be destroyed when orientation changes and the fragments stack will not disappear.
Related
I have a navigation drawer with only one activity and several fragments.
My main fragment is really heavy to load so I try to keep it in memory because if I don't do it the drawer will be laggy when I use it.
To do so I have a layout like this :
<RelativeLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
<fragment
android:id="#+id/main_fragment"
android:name=".MainFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
They they the same place but in the code I replace the fragment layout with lighter fragment and show/hide the main fragment.
This is a showFragment method to is called when I click on the item in the drawer.
case 0: //Light Fragment
fragment = getSupportFragmentManager().findFragmentByTag("light");
if (fragment == null) {
fragment = ProfileFragment.newInstance();
}
getSupportFragmentManager().beginTransaction().hide(getSupportFragmentManager().findFragmentById(R.id.main_fragment)).commit();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment, "light").addToBackStack(null).commit();
break;
case 1: //Heavy Frag
if (getActiveFragment() != null) {
getSupportFragmentManager().beginTransaction().remove(getActiveFragment()).commit();
}
getSupportFragmentManager().beginTransaction().show(getSupportFragmentManager().findFragmentById(R.id.main_fragment)).addToBackStack(null).commit();
break;
Which works fine except when I push the back button. I try many implementation of the onBackPressed but I always end up with either the main fragment showing when it should be hidden (So 2 fragment in top of each other) and the main fragment simply doesn't show.
Bonus question: Is that the correct way of doing thing ? I mean to avoid the drawer to lag or should i completely change my implementation?
Thanks.
Well, this should be more of a comment but since I cannot comment, I have to answer.
The reason could be that the fragments are not going into the backstack properly or in the order as they should.
I once created a project wherein I used Navigation Drawer with fragments. If I understand you correctly, what you want is to go to the last open fragment.
Try using the following code: Put it in onBackPressed()
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() == 0) {
this.finish();
} else {
getFragmentManager().popBackStack();
}
}
Essentially, you are popping the last fragment that was added to the backstack.
I have 3 fragments, Frag_A, Frag_B & Frag_C.
My navigation logic is: Frag_A ==> Frag_B ==> Frag_C . That's Frag_A is added into layout firstly, so, at the first time, Frag_A is shown on screen, then, if user press the Next Button, Frag_A is replaced by Frag_B, now Frag_B is shown on screen, if user press the Next button, Frag_B is replaced by Frag_C, so that Frag_C is shown on screen.
Everything works fine with my code at this point.
Method to change to next fragment:
//here, argument 'fragment' is either Frag_B or Frag_C
public void showFragment(Fragment fragment){
FragmentManager fragMgr = activity.getSupportFragmentManager();
FragmentTransaction fragTrans = fragMgr.beginTransaction();
fragTrans.replace(R.id.frag_placeholder, fragment, fragmentName);
fragTrans.addToBackStack(null);
int transId = fragTrans.commit();
fragMgr.executePendingTransactions();
}
Layout file of activity (Frag_A is added to the frag_placeholder when Activity starts):
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<FrameLayout
android:id="#+id/frag_placeholder"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</merge>
But, after Frag_C is shown, if I pop Frag_C by press the physical back button and invoke the showFragment(Frag_C) method to show the Frag_C again right after my code detected the physical button is pressed, I get a blank page shown on screen. Why & how to fix it?
================Explain=====================
I said above "*show the Frag_C again right after my code detected the physical button is pressed*", here is what I mean:
In Activity:
#Override
public void onBackPressed() {
super.onBackPressed();
//I detect the Physical Back button is pressed & invoke the method to show the popped Frag_C again.
}
Based on # Luksprog 's comment, I found that if I create a new instance of Frag_C instead of always use the same instance, the blank page problem get fixed.
I have the necessity to replace one starting fragment (I'll call it A) of an activity with two other fragments (B and C, in the "usual" list+viewer configuration). Currently I have a relative layout with two frame layouts acting as a placeholder for B and C:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RadioGroup
android:id="#+id/radiogroup_navigation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!-- Some radiobuttons (not displayed for the sake of brevity) -->
</RadioGroup>
<FrameLayout
android:id="#+id/frame_list"
android:layout_width="100dp"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_below="#id/radiogroup_navigation">
</FrameLayout>
<FrameLayout
android:id="#+id/frame_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_below="#id/radiogroup_navigation"
android:layout_toRightOf="#id/frame_list">
</FrameLayout>
When I need to display A, I just hide frame_list and add A to frame_view, and when I need to display B and C I set frame_list visible again and add the two fragments to each frame, in the same fragment transaction.
FragmentTransaction t = getSupportFragmentManager().beginTransaction();
t.remove(fragmentA);
t.add(R.id.frame_list, fragmentB);
t.add(R.id.frame_view, fragmentC);
t.addToBackStack(null);
t.commit();
In this way, when i press the back button, both C and B go away and I'm back to A fragment, but now frame_list is visible (and empty).
I am thinking to solve the problem in two possible ways:
overriding onBackPressed to hide the left frame if needed;
nesting B and C in another fragment;
But I also feel I'm probably looking at the problem in the wrong way, and maybe there's a cleaner design solution. Do you have any advice?
If I understand correctly, here is one solution:
Create two activities ActivityA and ActivityBC
Create another fragment with the radiogroup
Embed FragmentRadio into both ActivityA and ActivityBC
Have that fragment start new activities based on selection whilst finishing current activity
Make fields like this:
private static final String FRAGMENT_B_TAG = "fragmentB";
When you add the fragments, use the static Strings like tags:
t.add(R.id.frame_list, fragmentB, FRAGMENT_B_TAG);
t.add(R.id.frame_view, fragmentC, FRAGMENT_C_TAG);
In your activity, set up a listener, which will get triggered every time after you call addToBackStack(String). It will find out which fragment is currently visible and hide/show needed containers.
getSupportFragmentManager().addOnBackStackChangedListener(new OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
FragmentA fa = getSupportFragmentManager().findFragmentByTag(FRAGMENT_A_TAG);
FragmentB fb = getSupportFragmentManager().findFragmentByTag(FRAGMENT_B_TAG);
if (fa != null && fa.isVisible()) {
// Fragment A is visible, so hide the second container which is now empty
}
if (fb != null && fb.isVisible()) {
// Fragment B is visible, so show the second container
}
}
});
Notice that checking whether Fragment C is visible or not is not needed since when Fragment B is visible, Fragment C is always visible too.
This is an untested code, but I think it should work. Also, if you need any explanation, don't hesitate to ask.
Hope it helps.
I have an Activity with a Button and a FrameLayout in its layout.
When I click the Button I add the fragment to the Activity's View.
If I add the fragment to the Back stack with addToBackStack() when I click the Back button it dissapears.
I want to achieve the same functionality by clicking again the Button.
My code is this :
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
AddRemoveFragment Frag_A = new AddRemoveFragment();
FragmentManager fm1 = getSupportFragmentManager();
FragmentTransaction transaction = fm1.beginTransaction();
if ( state == 0 ) {
Log.i(TAG, "inside IF");
state=1;
transaction.add(R.id.fragment_container_1, Frag_A);
transaction.addToBackStack(null);
transaction.commit();
} else {
state=0;
Log.i(TAG, "inside ELSE");
//transaction.replace(R.id.fragment_container_1, Frag_A);
transaction.remove(Frag_A);
transaction.commit();
}
}
});
Both remove() and hide() do nothing.
From the reference I don't understand something more specific. Just says it removes the fragment from the container. Isn't this what I want?Remove the fragment from FrameLayout?
Edit: hope it has nothing to do with the support library. I saw someone having some problems with that. Here
XML :
<?xml version="1.0" encoding="utf-8"?>
<Button
android:id="#+id/button_frag_1"
android:layout_width="124dp"
android:layout_height="wrap_content"
android:text="#string/button_text_1" />
<FrameLayout
android:id = "#+id/fragment_container_1"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button_frag_1"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/button_frag_1" >
</FrameLayout>
Edit 2: I changed the code inside the else statement from transaction.replace(R.id.fragment_container_1, Frag_A); to transaction.remove(Frag_A); but still got the same functionality.
For fragments, first of all you need to remember one thing:
If you added your fragment in your XML layout, then it can't be removed, it can only be shown using the .show() method and hidden using the .hide() method. If on the other hand you create an instance of your fragment in your code then you should add it using the .add() method or remove it using the .remove() method.
As regard to your question, I dont think you need to add your fragment to back stack if you want to remove your fragment using your button (unless you want to keep the functionality of removing it using the 'back' button).
In addition I don't think you need to use replace, from the documentation of replace:
Replace an existing fragment that was added to a container. This is essentially the same as calling remove(Fragment) for all currently added fragments that were added with the same containerViewId and then add(int, Fragment, String) with the same arguments given here.
It means that it replaces the content of the container with the new fragment, so all you do is remove your fragment and add it again.
You should .add() you fragment when you want to show it and .remove() it when you dont.
UPDATE:
Following you second question, when I say that you can add you fragment in your xml I mean that you can write this:
<fragment
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/listfragment"
android:name="com.eadesign.yamba.TimeLineListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
In your XML layout file inside your FrameLayout which is your fragment container, in this case you cant remove this fragment you can only hide it.
And just to clarify you will always have to provide some kind of layout which will be the container of your fragment/fragments.
as opposite to that you can do what your are doing in your code:
AddRemoveFragment Frag_A = new AddRemoveFragment();
transaction.add(R.id.fragment_container_1, Frag_A);
transaction.addToBackStack(null);
transaction.commit();
In this case the fragment can be removed.
UPDATE2:
Try to take this line: AddRemoveFragment Frag_A = new AddRemoveFragment(); outside of the setOnClickListener method scope. I think that your problem is the fact that you are creating a new instance of this fragment on every click of your button. In fact I would move this line FragmentManager fm1 = getSupportFragmentManager(); out side as well there is no need to get the instance of a SupportFragmentManager on each click of your button. You should do this once.
I have a problem with a fragmented layout and I sincerely apologize if it has been answered before and I was too dumb to find it. I searched for hours and got nothing (well, I got lots but nothing solved my problem).
So here's my setup: I have a two pane layout using two FrameLayouts as containers for my fragments. activity_listing.xml:
<FrameLayout android:id="#+id/listing" />
<FrameLayout android:id="#+id/details" />
On opening the app (onCreate in the fragment's activity) a fragment called Listing is added to FrameLayout "listing" programmatically using a FragmentTransaction.
public class ListingActivity extends FragmentActivity
implements ListingFragment.Callbacks {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listing);
// Displaying the first listing here ...
Fragment fragment = new ListingFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.listing, fragment);
ft.addToBackStack(null);
ft.commit();
...
}
This ListingFragment is replaced a few times during runtime, so I have to create it instead of defining it in XML. I tried the latter with
<fragment android:name="com.example.app.ListingFragment" />
but then it won't be replaced or removed programmatically later on. I can only add() and then the old one is visible through the new one. But that's not the problem here - just an explanation for my way.
So far all of this works as it should, Listing is created and displayed etc., so no problems there. But:
When ListingFragment is displayed at startup and I press the back key, at the last position FrameLayout "listing" is emptied instead of dropping back to the Homescreen! I figured it has to be because onCreate of my ListingActivity I display an empty frame and add() a ListingFragment to it. So the back stack has the empty frame in it, too. Right?
I tried solving the situation by this to ListingActivity:
#Override
public void onBackPressed() {
super.onBackPressed();
if(getSupportFragmentManager().getBackStackEntryCount() == 0) {
this.finish();
}
}
But somehow that does not look or feel right ... it looks like a bad work around.
So is there any way to insert the fragment before the view with the empty FrameLayout is inflated so there is no empty state to "back" to? Or is it possible to remove the "empty" state from the back stack even though it is not in it? Any other ideas on how to avoid the empty frame after hitting "back"?
Thank you very much for your efforts in advance!
Don't call ft.addToBackStack(null) when you add the Fragment in onCreate. That tells the FragmentManger that you have another state BEFORE that fragment that you want to be able to jump back to.