0

I am having trouble to navigate from one fragment to another through a button in my android application. I have considered several questions about this issue but the solutions provided are not solving my problem. Here is my code and I don't know what I am doing wrong.

public class fragment_profile extends Fragment {

TextView txtFname, txtLname, txtGender, txtAge, txtPhone, txtEmail;
Button btImages, btVideos;
ImageButton btProfilePic;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_profile, container, false);


    btProfilePic = (ImageButton)rootView.findViewById(R.id.ProfilePic);

    txtFname = (TextView)rootView.findViewById(R.id.tvFName);
    txtLname = (TextView)rootView.findViewById(R.id.tvLName);
    txtGender = (TextView)rootView.findViewById(R.id.tvGender);
    txtAge = (TextView)rootView.findViewById(R.id.tvAge);
    txtPhone = (TextView)rootView.findViewById(R.id.tvPhone);
    txtEmail = (TextView)rootView.findViewById(R.id.tvEmail);

    btImages = (Button)rootView.findViewById(R.id.btnImages);
    btVideos = (Button)rootView.findViewById(R.id.btnVideos);


    //The code to replace fragment is not good, the clicklistener is working fine as I have tested it with a toast message

    btImages.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentManager fragmentManager = getFragmentManager();

            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

            //An object of the fragment tree is created
            fragmentImages ImageGallery = new fragmentImages();
            //The fragment is finally added
            fragmentTransaction.replace(R.id.fragment_profile, ImageGallery, "Image Gallery").commit();
            //Set title of action bar = title of fragment
            getActivity().setTitle(getTag());
        }
    });

    btVideos.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {



            FragmentManager fragmentManager = getFragmentManager();

            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

            //An object of the fragment tree is created
            fragmentVideos VideoGallery = new fragmentVideos();
            //The fragment is finally added
            fragmentTransaction.replace(R.id.fragment_profile, VideoGallery, "Video Gallery").commit();
            //Set title of action bar = title of fragment
            getActivity().setTitle(getTag());
        }
    });


    return rootView;
}
}

The issue is with the code inside the onClickListener. Can anyone tell me what I am doing wrong? Thank you.

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
Jamiil
  • 3
  • 4
  • Are you getting any error? – Rohit5k2 Feb 14 '16 at 13:46
  • Is this R.id.fragment_profile part of your fragment layout or main activity layout ? –  Feb 14 '16 at 13:51
  • This tutorial may be useful. http://developer.android.com/training/basics/fragments/communicating.html – OneCricketeer Feb 14 '16 at 14:10
  • @Rohit5k2 When I click on the Buttons it just change the title (Which is currently "Profile") to nothing ("Profile just disappear"). And nothing happens – Jamiil Feb 14 '16 at 16:30
  • @cricket_007 Thank you :) I will go through it and give you feedback :) – Jamiil Feb 14 '16 at 16:33
  • @penguin thank you mate. My first mistake is "R.id.fragment_profile" is not the id of my main activity layout. My second mistake is `getFragmentManager` . I was not able to use `getSupportFragmentManager` because I didn't write `getActivity().getSupportFragmentManager` Thank you for your help – Jamiil Feb 14 '16 at 16:52

2 Answers2

1

You want to use the supportFragmentManager. Within your onClickListener(s), make your transaction this:

//An object of the fragment tree is created
fragmentImages ImageGallery = new fragmentImages();

getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_profile, ImageGallery).commit();

//rest of your setting the activity title below

It is also best practise to make write your classes like FragmentImages and then your variables as imageGallery for example or even fragmentImages so you know what the object is.

Tom Alabaster
  • 965
  • 6
  • 12
  • Should be mentioned that `R.id.fragment_profile` needs to be the id of the container in the Activity XML where the Fragments are shown – OneCricketeer Feb 14 '16 at 14:08
  • @cricket_007 yes that's right, I forgot to mention that! – Tom Alabaster Feb 14 '16 at 14:29
  • Thank you for the reply :) Yeah I don't know why android studio is not allowing me to use `getSupportManager` although I am using the Support Library :/ – Jamiil Feb 14 '16 at 16:35
  • 1
    @Jamill Check your imports. You need to import the correct support Fragment class, not `android.app.Fragment` – OneCricketeer Feb 14 '16 at 16:39
  • It's okay :) you need to use getActivity().getSupportFragmentManager() to use the support library, otherwise you can only access the non-support fragment manager but let's be honest, no one usually uses that! – Tom Alabaster Feb 14 '16 at 16:40
  • Hey guys thank you very much. You solved my problem. Firstly @cricket_007 That the id should be that of the container, which I changed. Then @TomAlabaster suggested `getActivity().getSupportFragmentManager` and I changed this also. Now everything is working fine. Thank you guys I appreciate :) – Jamiil Feb 14 '16 at 16:49
0

I think you have to add your fragment_profile fragment to the backstack so you can recall it afterwards after you have triggered the other fragments. Try the following:

fragmentTransaction.replace(R.id.fragment_profile, ImageGallery, "Image Gallery").addToBackStack(null).commit(); 

Same with the latter:

fragmentTransaction.replace(R.id.fragment_profile, VideoGallery, "Video Gallery").addToBackStack(null).commit(); 

I got the information from here: Fragment Replacing Existing Fragment

Community
  • 1
  • 1