0

I've been developing some application all went fine, everytime I left the main fragment the toolbar and tablayout would also go, as soon as I was coming back to the main fragment Toolbar and tablayout where waiting for me there.

Recently, I've tested my app on newer android 7.0 and the result is that toolbar and tablayout don't go anywhere, they just stay at the top of any fragment.

I've tried this approach:

View view2 = getActivity().findViewById(R.id.tabs);
View view = getActivity().findViewById(R.id.toolbar);
view2.setVisibility(View.GONE);
view.setVisibility(View.GONE);

Using this method it removes tablayout and toolbar from the top, however when I go back using the back button, it also removes these two from the main fragment. I've tried using the onResume on my main fragment, but as far as I understand unless the parent mainactivity reloads, no result will be shown in onResume.

My question is, how can I hide toolbar and tablayout on specific nested fragments without hiding it on my main fragment. I've looked through the other questions,everyone talks about the need of hiding them in course of scrolling.

Any help will be much appreciated.

P.s. this is how I set toolbar and tablayout on my mainactivity:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar); // Setting/replace toolbar as the ActionBar
TabLayout tabLayout = (TabLayout)findViewById(R.id.tabs);
tabLayout.addTab(tabLayout.newTab().setText(getResources().getText(R.string.tab0)));
tabLayout.addTab(tabLayout.newTab().setText(getResources().getText(R.string.tab1)));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

Edit: these images will illustrate better the issue i'm facing

Main fragment

second fragment

Main fragment again

When I go to nested fragment (Second fragment) I remove the toolbar, but when I press back to return to the Main fragment it's also removed in there.

What I want is to hide it on my nested fragments and leave it on the main fragment.

Mr.BitCoin
  • 23
  • 4

1 Answers1

1

well my guess from your statement seems like you are trying to load. fragment in an activity and want not to see the toolbar and also the tab bars! well the approach you are using is right! using visibility GONE is the way to do it I think the same but the issue is the placement of the code! here is how you should do it!

in your container activity where you are trying to have the fragment loaded after doing

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar); // Setting/replace toolbar as the ActionBar
TabLayout tabLayout = (TabLayout)findViewById(R.id.tabs);
tabLayout.addTab(tabLayout.newTab().setText(getResources().getText(R.string.tab0)));
tabLayout.addTab(tabLayout.newTab().setText(getResources().getText(R.string.tab1)));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

here is what you have to do! simply where your fragmentTransaction Commits the fragment into the container after that commit you can do it like this

FragmentManager mFragManager = getSupportFragmentManager();
FragmentTransaction mFragTranscation = mFragManager.startTransaction();
mFragTranscation.add(R.id.containerId,yourFragObject());
mFragTransaction.commit();
//after that you can do the visibility GONE MAGIC
toolbar.setVisibility(View.GONE);
tabLayout.setVisibility(View.GONE);

You were trying to hide the Activity's design part from within the fragment if you want that to happen the next way to do it is using the ONACTIVITYCREATED() power but that also has to have a way to access the Views of Container Activity in the fragment which can be done in this method.

but above written code will help if you want the activity's header design gone on opening of the fragment! happy coding

for further if you want anything to come back which is hidden you can have the OnBackPressed function ! doing it for you! for example if you want to see that when main fragment comes in action and the TOOLBAR etc should be back you should Override the OnBackPressendMethod and there you can check which fragment is currently getting loaded back from stack and if its the "Required One" then just use VISIBLITY(VIEW.VISIBLE) for the ones you want to be back on screen!

override in MainFragment

@Override
public void onBackPressed() {
   //here you can call the things back if the frag required is loaded
}

//try this way to do the add fragment and adding its name in to stack

private void replaceFragment (Fragment fragment){
  String backStateName = fragment.getClass().getName();

  FragmentManager manager = getSupportFragmentManager();
  boolean fragmentPopped = manager.popBackStackImmediate (backStateName, 0);

  if (!fragmentPopped){ //fragment not in back stack, create it.
    FragmentTransaction ft = manager.beginTransaction();
    ft.replace(R.id.content_frame, fragment);
    ft.addToBackStack(backStateName);
    ft.commit();
  }
}

that way you can do it for more check this thread here STACK_THREAD

Rizwan Atta
  • 3,222
  • 2
  • 19
  • 31
  • Yes, I'm trying to hide the toolbar and tabs only in the nested fragments, I'm using the tabs as navigation where each tab has it's buttons, whenever button pressed, opens up new fragment but I don't want to see tabslayout nor toolbar there, with this approach it removes them, however when I get back to the main fragment it removes it over there also. Please, see edit. – Mr.BitCoin May 03 '18 at 10:45
  • ok its good I can see whats going here now! I am editing the answer for further solution you needed please accept the answer if it helped ^_^ – Rizwan Atta May 03 '18 at 10:48
  • But how do you detect from which fragment you are doing this? I've been trying your approach but I need somehow to know that the fragment that will load after I press back is the main fragment, right? – Mr.BitCoin May 03 '18 at 12:18
  • yes when you add a fragment you should add its label in the add to backStack option lemme add an example – Rizwan Atta May 03 '18 at 12:28