-5

As the title says when on the "Home Fragment" when i click on an image tile I want the new activity to load but I would like to keep the navbar visible. Currently with the code below the activity starts but the navbar disappears. I would like help to write code so the activity starts but the navbar stays.

public class HomeFragment extends Fragment {

private ImageView imageCbt;
private ImageView imageDistorted;

@Nullable
@Override
public android.view.View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_home, container, false);

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);

    // get the button view
    imageCbt = getView().findViewById(R.id.whatIsCbt);
    imageDistorted = getView().findViewById(R.id.distortedThinking);

    // set a onclick listener for when the button gets clicked
    imageCbt.setOnClickListener(new View.OnClickListener() {
        // Start new list activity
        public void onClick(View v) {
            Intent mainIntent = new Intent(getActivity(),
                    IntroActivity2.class);
            startActivity(mainIntent);
        }
    });

    imageDistorted.setOnClickListener(new View.OnClickListener() {
        // Start new list activity
        public void onClick(View v) {
            Intent mainIntent = new Intent(getActivity(),
                    TwelveTypesDistortedThinkingActivity.class);
            startActivity(mainIntent);



        }
    });

}
Abhimanyu
  • 11,351
  • 7
  • 51
  • 121

1 Answers1

0

To change the current fragment, you can replace the code inside your click listeners to this one (Replacing a fragment with another fragment inside activity group):

Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack if needed
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

Then you need to convert your activities to Fragment (maybe some refactoring will be necessary) and to change the current fragment in the onClickListener methods.

PS: When you use a navbar, fragments are a best practice in application architecture.

  • Fragment newFragment = new Fragment(); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); // Replace whatever is in the fragment_container view with this fragment, // and add the transaction to the back stack if needed transaction.replace(R.id.fragment_container, newFragment); transaction.addToBackStack(null); // Commit the transaction transaction.commit(); – user11498215 May 14 '19 at 12:57
  • The FragmentTransaction and getSupportFragmentManager() displays in red with an error saying 'unable to resolve' – user11498215 May 14 '19 at 12:58
  • 1
    If you're inside a Fragment you have access to the activity after the onActivityCreated was called by using getActivity(). Then getActivity().getFragmentManager() will work. For the FragmentTransaction, maybe you're missing a library or an import. – Romain Goutte-Fangeas May 14 '19 at 13:01
  • Thank you for your help this was very helpful – user11498215 May 14 '19 at 16:55