FragmentTransaction .replace does not load all fragment components

331 views Asked by At

I searched for an answer for a long time and couldn't find any. So I hope you can help me. I'm making an android project and with the click of a button I wanna replace the current custom fragment(made by me) with another custom fragment. The problem is that when I try to do this, not all components from the second fragment are loaded, an imagebutton, in particular. I'm doing all this from my Activity.

This is my code to replace the fragment:

public void loadTab() {
    String n1 = ((TextView) findViewById(R.id.nj1)).getText().toString();
    String n2 = ((TextView) findViewById(R.id.nj2)).getText().toString();
    ft = Frag_Tab.newInstance(n1, n2);
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.jfl, ft);
    transaction.addToBackStack(null);
    transaction.commit();
}

And I get this:

https://i.stack.imgur.com/pGWNM.jpg

But if I directly add the second frame with transaction.add(not adding the first fragment), it does appear:

https://i.stack.imgur.com/rifC3.jpg

private void initTab() {
        ft = Frag_Tab.newInstance("j1", "j2");
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.jfl, ft, "tab");
        fragmentTransaction.commit();
}

So I can't really figure out what the problem is, why does this imagebutton disappear? Thanks for the help.

EDIT:

I found this error when I open the first fragment. So, maybe it has to do with a memory issue?

Background partial concurrent mark sweep GC freed 670(44KB) AllocSpace objects, 0(0B) LOS objects, 39% free, 11MB/19MB, paused 18.369ms total 51.191ms

EDIT 2: It seems it's unclear what I want you to help me with, sorry. The problem lays with the dice imagebutton that you can see in the second image, when I use the transaction.replace that button is not loaded, but if I use transaction.add the button is in fact loaded.

So, I need to use the replace method in my code because I need to first show another fragment that asks the user about the players names to start the game, before showing the second. The way I manage to load the imagebutton is without loading the first fragment before.

Fragment 1 = player names with a button to start the Fragment 2, I use the transaction.replace.
Problem = When I do this, the imagebutton(the dice in the right) does not load in the Fragment 2.
Fragment 2 = game table with dice imagebutton.
Problem = If I start the activity directly with this Fragment using transaction.add the dice imagebutton does appear.
Final Problem = so there must be something wrong going on in the transaction.replace to no load that imagebutton.

Well, I hope I'm clear enough now.

0

There are 0 answers