سؤال

So i have on MAIN ACTIVITY(M), two FRAGMENTS(F1, F2) and a BUTTON(B) on F1.

Now when the main activity is started F1 is automatically loaded which is fine, what I want to do is start F2 when I click on B which is on F1.

Following is my code for Fragment1 that is HomeFragment:

package info.androidhive.slidingmenu;

import java.lang.reflect.Field;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;

public class HomeFragment extends Fragment {

 public static HomeFragment newInstance() 
 {
        return new HomeFragment();
    }

public HomeFragment(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState){


    View rootView = inflater.inflate(R.layout.fragment_home, container, false);

    ImageButton ib = (ImageButton) rootView.findViewById(R.id.buttonEnq);
    ib.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            InfoFragment infoFragment = new InfoFragment();
            /*FragmentManager fm = getFragmentManager();
            fm.beginTransaction().replace(R.id.frame_container, infoFragment).commit();
            */

            FragmentTransaction ft = getChildFragmentManager().beginTransaction();
            ft.setCustomAnimations(R.anim.abc_slide_out_bottom, 0);
            ft.replace(R.id.frame_container,infoFragment);
            ft.commit();
        }
    });

    ViewPager viewPager = (ViewPager)rootView.findViewById(R.id.myfivepanelpager);
    ImageAdapter adapter1 = new ImageAdapter(this.getActivity());
    viewPager.setAdapter(adapter1);

    return rootView;

    }

@Override
public void onDetach(){
    super.onDetach();

    try{

        Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
        childFragmentManager.setAccessible(true);
        childFragmentManager.set(this,null);

    }catch(NoSuchFieldException e){
        throw new RuntimeException(e);
    }catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
  } 
}

frame_container is the name of the container in the main_activity.xml. I have tried the following posts:

Can you tell me what I'm doing wrong?

هل كانت مفيدة؟

المحلول

Problem is your trying to add the Fragment B as a Child Fragment of Fragment A in R.id.frame_container, and I suspect the frame_container is not a view in Fragment A layout(R.layout.fragment_home).

You can always add a child fragment using getChildFragmentManager(), but it needs a container to hold the fragment.

In your case , I don't see any need of adding the Fragment B as a child for Fragment A.

You need to replace the Fragment A attached in Activity with Fragment B using the the Activity Fragment Manager

Change the line in Button onClick()

FragmentTransaction ft = getChildFragmentManager().beginTransaction();

as

FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top