I've got an activity with an actionbar, and actionbar tabs. When I choose an item from the contents of the tabs, I'm trying to replace the current fragment with a new one, add the transaction to the back stack, and hide the tabs.
I hide the tabs by changing the action bar navigation mode to standard.
The problem is that when I press the back button, I just get a blank view with the action bar (in standard mode).. The fragment transaction doesn't appear to be reversed.
If I don't hide the tabs by changing the navigation mode to standard, the transaction reversal works fine..
I've tried overriding the back press to change the navigation mode back to tabs, but it doesn't work.
Could someone tell me how they would achieve this?
Here is the code where the tabs get hidden and the fragment transaction takes place:
ActionBar actionBar = getActivity().getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
Fragment albumListFragment = new AlbumListFragment();
albumListFragment.setArguments(bundle);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(android.R.id.content, albumListFragment);
ft.addToBackStack(null);
// Commit the transaction
ft.commit();
For clarity: I would expect that pressing back after this transaction has been committed, the tabs would come back into view with the previous fragment. At the moment after pressing back it's not showing the tabs OR the fragment.
Not sure if this question is still relevant or not, but I seem to be having good luck hiding the tabs after the fragment transition.
From my ListFragment, I do a standard fragment transition very similar to how you're doing it in your code.
In my detail fragment, however, I hide the action bar in the onCreate method:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getActivity().getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
}
and show it again in my onPause method:
public void onPause() {
super.onPause();
ActionBar actionBar = getActivity().getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}
Related
I have two fragments in my app namely HomeFragment and FeedbackFragment. In addition to switching fragments on tab buttons, I would also need to switch between fragments on a button click that is within the fragment. The tab items are highlighted properly on switching fragments using tab clicks. But the tab items does not get highlighted when i switch to other fragment on a button click from fragment1
Below is the code used to switch across fragments on button click and it works.
FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
But the respective tab icon of FeedbackFragment is not highlighted. Currently the navigation item of HomeFragment remains highlighted even after FeedbackFragment is replaced. How do I highlight the menu item of Feedback Fragment ?
I tried below approaches but nothing worked:
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation);
View view = bottomNavigationView.findViewById(R.id.tab_calls);
view.performClick();
Also,
MainActivity.mBottomBar.selectTabAtPosition(2);
Nothing worked. Please help.
Try using the method to select a tab as if it was tapped:
bottomNavigationView.setSelectedItemId(R.id.tab_calls)
What I have done now is in onResume of each fragment I try to de highlight all other tab bars and highlight the current tab bar again. I access the bottomNavigationMenu from each fragment and do this. Not sure if this is the right way to handle it but it works atleast.
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.
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
My application contains an activity with several fragments. The activity displays the ActionBar by default. However, there are several fragments, displayed in order, where the ActionBar should not show. For these fragments, I hide the ActionBar in the onCreate code with getActivity().getActionBar().hide(); . However, each time one of these fragments are loaded, the ActionBar flashes on the screen momentarily before disappearing.
How can I make the actionbar disappear before the fragments are displayed on the screen? For reference, below is the code I use for transactions between the fragments:
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.container, fragment);
transaction.commitAllowingStateLoss();
I would suggest two ways to solve this.
Reconsider your activity-fragment relation, should it be split into different activities, i.e. some activities with action bar, some without action bar.
Use toolbar, and remove all default actionbar, assign the toolbar to your fragments which need it instead of the activity. Be reminded that you are not using setSupportActionBar() as this is not part of the activity layout.
Let's say I have a FragmentA visible.
I add a FragmentB (keyword: add, not replace), add it to the Fragment back stack and commit.
The issues I'm having doing this is:
1) The action buttons of the action menu of FragmentB are added, but those of FragmentA are not removed.
2) The title of the ActionBar does not change (despite calling getActivity().setTitle("FragmentB") in onResume() of FragmentB.
I can resolve both of these by calling replace instead of add when showing FragmentB however, for quite a few reasons I specifically need to add the Fragment instead (one of them being, I need to retain the state of FragmentA while showing B).
So how would I go about updating the ActionBar correctly as described?
Try this piece of code:
getActivity().getActionBar().setTitle("FragmentB");
Use this code in your Activity.. (for setting title to your fragment).
public void setActionBarTitle(String title) {
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(title);
}
from your fragment onResume(), call this using
// Set title bar
((MyActivity) getActivity())
.setActionBarTitle("Fragment A");
and in each fragment you need to override onCreateOptionsMenu() to load your menus of that fragment.
How can I completely remove the ActionBar from Certain Fragments. I want to remove it not just to hide it. I have Actionbar Tab Navigation. From that i added a new Fragmnt which didn't need actionBar. So i need to remove it. I can't hide it because when i pressed back button and moved to Tabs section, i need to show Action bar again.
This is how i am replacing Fragments.
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.add(R.id.frame_container, fragment)
.addToBackStack(null).commit();
The action bar is not part of your fragment. It's part of your activity.
You can create a new Activity with a theme Theme.Holo.Light.NoActionBar and open this Activity with :
getActivity().startActivity(new Intent(getActivity(), SecondActivity.class));
And inside your second activity you can display the fragment you want.
I recommend you this post to understand more the differences between Fragment and Activity: https://stackoverflow.com/a/10515807/3112836
If you're not using actionbar.hide() just because you have to show Action bar in other fragment, than I think you should give it a try by using :
actionbar.hide() inside onAttach method of your fragment and actionBar.show() inside your onDestroy() method.