I have fragment which contains recycleview with list. in fragment i'm calling another bottom sheet dialog fragment, i Want to know when i dismiss that bottom sheet dialog fragment how to refresh base fragments list.
I Have Tried onpause and onresume method in base fragment. Please help me to solve the issue.
This method trigger when dialog fragment dismissed. To override the method in dialog fragment.
#Override
public void onDismiss(#NonNull DialogInterface dialog) {
super.onDismiss(dialog);
// use interface to callback method in base fragment
}
I'm not sure, but I tried blow code and it worked.
#Override
public void dismiss() {
super.dismiss();
((MyActivity)context).myRefreshFunction();//
}
In my main activity ActionBarDrawerToggle object is present. I have to disable navigation drawer for a specific fragment (eg. fragmentB) which is called by another fragment (eg.fragmentA),where fragmentA has been inflated by the MainActivity. How to do that?
Add this method to your main activity and call it when you switch the fragments to enable/disable the navigation drawer
public void enableNavigationDrawer(boolean isEnabled) {
getActionBar().setDisplayHomeAsUpEnabled(isEnabled);
getActionBar().setHomeButtonEnabled(isEnabled);
if(isEnabled){
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
} else {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
}
step 1:
interface DrawerLocker{
public void setDrawerLocked(boolean shouldLock);
}
use this in ur main activity ,paste this code in above ur class.
step 2 :
go to ur fragment where u want to hide the navigation drawer, into onCreateView() methode paste this code
((DrawerLocker)getActivity()).setDrawerLocked(true);
step 3: create onDestroyView() method as below :
#Override
public void onDestroyView() {
super.onDestroyView();
((DrawerLocker)getActivity()).setDrawerLocked(false);
}
now run ur code and see:-) ,credit goes to another member ...i didt remember his name ...his answer helps me to solve this problem
How can I hide action bar for certain fragment?
I have searched for the answer at stackoverflow, but I have only found a solution, which involves disabling action bar for main activity in android manifest. Since I need to disable action bar for one fragment, this is not an option.
Any ideas? Thanks.
EDIT: min API level is 7, sherlock is not being used
If you are using AppCompatActivity (you should) then this is the solution that worked for me:
((AppCompatActivity) getActivity()).getSupportActionBar().hide();
You can add this in onCreate(). The support fragment's getActivity() returns a FragmentActivity, and this does not contain the getSupportActionBar() method. Using just getActionBar() gives null-pointer exception if you have AppCompatActivity.
Put this code in fragment in which you want to hide toolbar...
#Override
public void onResume() {
super.onResume();
((AppCompatActivity)getActivity()).getSupportActionBar().hide();
}
#Override
public void onStop() {
super.onStop();
((AppCompatActivity)getActivity()).getSupportActionBar().show();
}
As it was already mentioned, actionbar may be hidden by (requireActivity() as AppCompatActivity).supportActionBar?.hide() call.
If you want to show it in some of your fragments and to hide it in some other fragments, it may be convenient to apply default (for your case) visibility in onViewCreated of your Base fragment:
abstract class BaseFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
(requireActivity() as AppCompatActivity).supportActionBar?.show()
}
}
and to hide it in particular fragments:
class HiddenActionBarFragment : BaseFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
(requireActivity() as AppCompatActivity).supportActionBar?.hide()
}
}
This solution is more flexible than using onStart - onStop for visibility change because during the transition onStart of a different fragment will be called earlier than onStop of the current fragment. So the next fragment won't be able to 'override' actionar visibility applied in onStop of the current fragment.
getActionBar().hide() or getSupportActionBar().hide() (if using ActionBarCompat v7 lib).
Regards
Hide actionBar using this in the required fragment.
getActivity().getSupportActionBar().hide();
And Show actionBar with this in your next fragment.
getActivity().getActionBar().show();
Put getSupportActionBar().hide() before setContentView in the Activity that holds the fragments.
Also add this: ((AppCompatActivity) getActivity()).getSupportActionBar().hide() in the fragments before inflating layout. This works if you are using this ActionBarActivity.It also removes my lags in hiding action bar
You can simply put this into your Fragment Class createView method:-
View layout = inflater.inflate(R.layout.player_fragment, container, false);
((AppCompatActivity) getActivity()).getSupportActionBar().hide();
Paste the code in the fragments before inflating the layout to SHOW Action Bar `
//Kotlin statement
(activity as AppCompatActivity?)!!.supportActionBar!!.show()
Paste the code in the fragments before inflating the layout to Hide Action Bar `
//Kotlin statement
(activity as AppCompatActivity?)!!.supportActionBar!!.hide()
This works fine for me.
Have you tried getActivity().getSupportActionBar().hide() in the onCreate() of the fragment you wish the ActionBar to be hidden in?
I am assuming you are not using ActionBarSherlock.
If you're like me and have one main Activity and several Fragments, you can add a listener to your NavController in the main Activity's onCreate() method.
For instance, I only wanted it hidden in my Login Fragment, so I used this (Kotlin):
navController.addOnDestinationChangedListener { _, destination, _ ->
if (destination.id == R.id.loginFragment) {
supportActionBar?.hide()
} else {
supportActionBar?.show()
}
}
This avoids having to add the call to show() in all the Fragments in which you do want the ActionBar.
This solution is for complex non-AppCompat applications that use native ToolBar when running Lollipop onwards and native ActionBar otherwise.
It assumes you want to hide the ActionBar whenever Fragments are visible.
Inside onCreate() in each of your Activities:
getFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener()
{
#Override
public void onBackStackChanged() {
U.ABkk(this, getFragmentManager().getBackStackEntryCount());
}
}
);
OR (much better) inside a 'singleton' class that implements Application.ActivityLifecycleCallbacks
#Override
public void onActivityCreated(final Activity A, Bundle savedInstanceState) {
A.getFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
U.ABkk(A, A.getFragmentManager().getBackStackEntryCount());
}
});
}
Inside the utility class:
/** Show/hide ActionBar for KitKat devices */
public static void ABkk(Activity A, int count) {
if (lollipop()) return; // No problem when using Toolbar
ActionBar ab = A.getActionBar();
if (ab==null) return;
if (count==1) { ab.hide(); }
if (count==0) { ab.show(); }
}
/** Return true if API 21 or greater */
private static boolean lollipop() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
}
Using onActivityCreated() is a solution that requires no changes to your Fragments or Activities!
I have my host activity with a ViewPager+FragmentStatePagerAdapter which let me swipe through a set of fragments (different instances of the same ItemFragment class) and the data for populating each fragment are fetched from the network.
Now, while I swipe I want to update the action bar title with a property coming from the current fragment so I use OnPageChangeListener/onPageSelected to do that work:
...
#Override
public void onPageSelected(int position) {
MyFragment currentFragment = (MyFragment) getSupportFragmentManager().findFragmentById(R.id.view_pager);
getSupportActionBar().setTitle(currentFragment.propertyIWantToUseAsTitle);
}
...
I have couple of doubts here:
am I retrieving the real current fragment? The FragmentStatePageAdapter does not set a tag for each fragment so I don't know any other way to get the current fragment rather than using the ViewGroup container
when onPageSelected is invoked, the added fragment could be still fetching the data I need to set the action bar title causing a npe.
What's the best place to keep the logic of updating the action bar title in the scenario described above?
ps. I have tried a mixed approach adding to MyFragment the code to update the ab when the data I need are available. The problem is that few fragments are loaded ahead having the action bar title changing as the fragments finish loading. The result is the ab title is a random title coming from a fragment few pages ahead.
Thanks in advance for helping!
I set the ActionBar title in the Fragment itself.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getActivity().getIntent().getExtras();
type = (String) extras.getString("type");
}// end method
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
Activity activity = getActivity();
ActionBar actionBar = activity.getActionBar();
if (type.equals("one")) {
actionBar.setTitle(R.string.one);
}
if (type.equals("two")) {
actionBar.setTitle(R.string.two);
}
if (type.equals("three")) {
actionBar.setTitle(R.string.three);
}
}
}//end method
AFAICT, your PagerAdapter should be called with setPrimaryItem() when a new page is set to be "current". You could override that method, chain to the superclass, then cast the Object (first parameter to setPrimaryItem()) to your appropriate Fragment class and do something with it. That "something" could be stashing it in a data member of your PagerAdapter (e.g., currentPage) and adding a getter to be able to retrieve it later.
I have a class extending ListActivity and I am trying to hide the default TitleBar (I think it's called ActionBar) using the code below. It works on a regular activity, but not on the ListActivity. How do I accomplish the same in this scenario?
public class MyClass extends ListActivity{
#Override
public void onCreate(Bundle b) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(b);
// Tried Window.FEATURE_NO_TITLE here as well
setContentView(R.layout.activity_myclass);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
The result is a NullPointerException. The full error stack trace is here if you need it: http://pastebin.com/VLR5dE8m
to disable Titltbar,, use this code in your manifest file ,it will help you.
<
activity
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
>
To remove the actionbar: getActionBar().hide(). Also see this answer: How to disable action bar permanently
I think You should not use requestWindowFeature() if you are using ActionBar. You can hide title of ActionBar with this:
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
http://developer.android.com/reference/android/app/ActionBar.html#setDisplayShowTitleEnabled(boolean)
It seems like the NullPointerException is generated by getActionBar(). Removing the following line from the code fixed the issue.
getActionBar().setDisplayHomeAsUpEnabled(true);
The final (working) code:
public class MyClass extends ListActivity{
#Override
public void onCreate(Bundle b) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(b);
setContentView(R.layout.activity_myclass);
}
}
Note that requestWindowFeature must be called before calling the super class and setContentView.