Reputation: 1808
For my app's UI, I use Transition
s and Fragment
s. In the onCreateView
function of my first Fragment
, I launch a Transition
to change the display from a first Scene
to a second Scene
. Afterwards, when I click on a Button
, I start another Transition
to show some kind of "splashscreen" represented by a third Scene
and, in it's onTransactionEnd
, I try to change the Fragment
with myView.postDelayed(new myRunnableForChangingFragment(),1000)
.
In debug mode, I see my Runnable
is correctly launched, but the new Fragment
doesn't replace the first one... Nevertheless, something did happen : I have to click two times on the back button to return to my first Fragment
. In fact, it seems the Fragment
is activated but not visible...
Any idea about how I could fix that?
As asked in the first comment, here's the code I use to change the Fragment
:
fragment=new mySecondFragment();
transaction=getActivity().getFragmentManager().beginTransaction();
transaction.replace(R.id.Conteneur, fragment,"Fragment tag");
transaction.addToBackStack(null);
transaction.commit();
I can add that this code launched from outside the onTransitionEnd
works perfectly.
The xml file for the "container" :
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/Conteneur" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> </LinearLayout>
The xml file for the first and the second scene (the only difference ; buttons are set invisible in code in the first one) :
<TextView android:id="@+id/TitreJeux" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginBottom="@dimen/MargeVerticaleBoutonsInterfaceJeux" android:lines="1" android:text="@string/app_name" android:textColor="#FFFF00" android:textSize="35sp" android:textStyle="bold" android:typeface="serif"/> <vd63.jeux.JeuAnimationRapide.BoutonInterface android:id="@+id/BoutonOptions" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_gravity="center" android:layout_marginTop="@dimen/MargeVerticaleBoutonsInterfaceJeux" android:layout_weight="1" android:background="@drawable/bouton_jouer" android:text="@string/BoutonOptions" android:textColor="@color/CouleurTexteBoutons" android:textSize="25sp" android:typeface="serif"/>
The xml fle for the "splash" scene :
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/EcranChargement" android:orientation="vertical" android:gravity="center" android:background="@drawable/fond_interface" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".JeuAnimationRapide"> <ImageView android:id="@+id/VD63" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/vd63"/> <ImageView android:id="@+id/Chargement" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/chargement"/>
I transition from scene one (button invisible,simple Slide
) to scene 2 then back to scene 1 (Slide
again) then to the "splash" scene (with a Fade
transition).
Upvotes: 0
Views: 64
Reputation: 31
When you switch from fragment one to fragment two use below code It works for me I hope it will help you.
Fragment fragment = SearchActivity.newInstance(context);
String tag = fragment.toString();
getFragmentManager().beginTransaction()
.addToBackStack(tag)
.replace(R.id.fragment, new SearchActivity())
.commit();
Use addToBackStack when you want to come back to this fragment again on backpress otherwise you don't need to add addTobackstack
Upvotes: 1