简体   繁体   中英

Android - fragment onActivityCreated() not getting called after .replace

My main application has a blanck FrameLayout the reason for this is so that I can on the go, add and replace fragments to it...

I have had no issue with this until now. I have successfully added the first fragment and the onCreate gets called etc, then in my code using a button I replace the FrameLayout with my second fragment which seems to execute, however not showing? as onActivityCreated() does not get called.

My main application calls FrameLayout xml file known as send_activity

FragmentManager fragmentManager = getFragmentManager();

    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    sendControlFragment sendControlFragment = new sendControlFragment();
    peerItemFragment fragmentList = new peerItemFragment();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.send_activity);

        if (findViewById(R.id.fragment_container) != null) {

            // However, if we're being restored from a previous state,
            // then we don't need to do anything and should return or else
            // we could end up with overlapping fragments.
            if (savedInstanceState != null) {
                return;
            }

            // Create a new Fragment to be placed in the activity layout
            sendControlFragment controlFragment = new sendControlFragment();


            // Add the fragment to the 'fragment_container' FrameLayout
            fragmentTransaction.add(R.id.fragment_container, controlFragment).commit();

        }

    }

The above works perfectly...

The button which I call the second fragment, I got told to initiate a new FragmentTransaction which I have done, however this does not call the onActivityCreated() and skips to fragmentList.addPeers(mService.peerList);

This is great that it is calling the addPeers() method however without the onActivityCreated() then it crashes!

   public void authenticateClick(View v) {

        mService.peerSearch();

        peerItemFragment peerFragment = new peerItemFragment();

        FragmentTransaction fragmentTransactionauthen = fragmentManager.beginTransaction();

        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the back stack so the user can navigate back
        fragmentTransactionauthen.replace(R.id.fragment_container, peerFragment);
        fragmentTransactionauthen.addToBackStack(null);

        // Commit the transaction
        fragmentTransactionauthen.commit();

        //pass peer list to fragment to display
        fragmentList.addPeers(mService.peerList);
    }

Can you please suggest a solution as .replace currently to my knowledge doesnt call the onActivityCreated() method...

Because i was using a listfragment I had to replace my listView id to the following, as this was causing fatal error!

android:id="@android:id/list"

it now works and replaces fragments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM