0

Hello you!

I am not a native english speaker, so hope you understand me anyway.

In my application i have a activity with a RelativeLayout wich i use as Fragment-container.

<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" >

    <RelativeLayout
            android:id="@+id/fragmentContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/white" />
</FrameLayout>

At the time i have two different Fragments, one will be shown at Startup and die other one will be called by click on a TextView in the first one. Both Fragments have its own class and the first one has a interface to the main activity to perform the replacement of the Fragment after onClick.

And now there is my Problem....

public void onCallForAGB() {
    getFragmentManager().beginTransaction() 
    .replace(R.id.fragmentContainer,  new LoginReaderFragment(), "agbreader")
    .addToBackStack(null)
    .commit();

    LoginReaderFragment fragment = (LoginReaderFragment) getFragmentManager()
        .findFragmentById(R.id.fragmentContainer);

    if (fragment != null && fragment.isInLayout()) {
        fragment.setText("Gruß an die Welt!");
    } else {
        Toast.makeText(mContext, "Fail", Toast.LENGTH_LONG).show();
    }
}

just for explaining: the container holds a Fragment named LoginLoginFrament. The user of my app can Login or click on a link (TextView) to show the AGB (dont know how to translate, maybe "Therms for use and Law dependencies" ??). Via the interfaceonCallForAGBwill be executed and replace the Fragment with a Fragment calledLoginReaderFragment` wich is a Fragment with a headline (TextView) and a textfield (TextView) to show filecontents. The function setText(string) should set the text after the Fragment was created.

But after Fragmenttransaction.commit() it seems the change will not be executed immediately. It seems it will be executed when leaving the whole procedure. So i can't access the new fragments Views (to change text) without the complete perfomed replace by commit.

So here is my question: How can i force the FragmentTransaction to be executed after commit() ? Or is there a workaround, so that i can change the headline and text of the new Fragment right after changing from the first to the second Fragment?

Here is the Errorreport (LogCat) which is why I have this assumption

05-29 20:59:18.748: E/AndroidRuntime(14334): java.lang.ClassCastException: de.example.myapp.fragments.LoginLoginFragment cannot be cast to de.example.myapp.fragments.LoginReaderFragment

I think LoginReaderFragment fragment = .... create this error because the old Fragment (LoginLoginFragmet) is still active.

Hope you can help me. Greets !

Justl24
  • 119
  • 1
  • 7
  • You're expecting the fragment to change immediately, which it doesn't... you should pass in the desired text to LoginReaderFragment's constructor (or set the arguments as a Bundle), and have it set the text inside onCreateView – Buddy May 29 '15 at 19:58
  • ok. That works in this case. And i think, if i call the setText Methode from an other Point in Application it will work too, so i can change the Text again later. Not the prefered way was searching for, but easy and clean. Thank you! – Justl24 May 29 '15 at 20:51

1 Answers1

0

Do not try to set the Fragments UI data from the Activity itself after creating it. Instead, give the fragment the information, which should be shown after the fragment is visible. The best way to do this is to use the getInstance() pattern. see here.

You need to call the setText function from onViewCreated() in the fragment itself. this grants you that the data will be set, after the view is availible.

Community
  • 1
  • 1
Paul Reznik
  • 965
  • 1
  • 6
  • 18