-2

I have an Activity which launches a Fragment called main page. This page has cards with buttons and each button opens a new fragment. The problem is that when it opens a new fragment it seems to start a new Activity, which is not the main one, launched at first. For this reason the toolbar I have on the main Page doesn't appear on other pages which are opened on the click from the Main fragment.

Does anybody knows how to make it not change the first Activity or at least how to access it from other fragments. When I try getActivity() or getContext() from these Fragments it is never the same Activity loaded initially.

Btw, the first Activity extends AppCompactActivity

AppActivity activity = (AppActivity) v.getContext();
activity.getSupportFragmentManager().beginTransaction().replace(R.id.drawer_layout, myFragment).addToBackStack("string").commit();

(Appears in the main fragment on click)

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
Alon
  • 41
  • 4

1 Answers1

0

Use below code i hope this will solve your problem...

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
      FragmentEntityManagementPage  fragmentEntitymanagementPage = new FragmentEntityManagementPage();
        fragmentTransaction.add(R.id.rl_container1, fragmentEntitymanagementPage);
        fragmentTransaction.addToBackStack("FragmentEntityManagementPage");
        fragmentTransaction.commit();
Adam
  • 444
  • 5
  • 18
  • Thanks, What exactly is this fragmentEntitymanagementPage? Where can I find it in my code ? – Alon Oct 01 '18 at 11:34
  • It is your target fragment name or second fragment name where you want to share data after button click. – Adam Oct 01 '18 at 11:37
  • Thanks, So I did add this to my code :FragmentTransaction fragmentTransaction = activity.getSupportFragmentManager().beginTransaction(); fragmentTransaction.add(R.id.drawer_layout, myFragment); fragmentTransaction.addToBackStack("FragmentEntityManagementPage"); fragmentTransaction.commit(); But the problem it is still connected to the Activity I create before but it its not the same Activity as I have on the Start... – Alon Oct 01 '18 at 11:40
  • Cause I have before this : AppActivity activity = (AppActivity) v.getContext(); The question how do I get the first Activity ? – Alon Oct 01 '18 at 11:43
  • The Problem I am not allowed to share it, but I can say that the AppACtivity has getApplicationContext() - MyApp context but the Fragment has AppACtivity context... – Alon Oct 01 '18 at 11:53
  • https://stackoverflow.com/questions/20464273/get-the-application-context-in-fragment-in-android – Adam Oct 01 '18 at 12:00
  • Yeah I try to use getActivity() in the fragment but it is not the same as I do getActivity() in the AppActivity... – Alon Oct 01 '18 at 12:17