getActionBar from Fragment with AppCompatLibrary - android

I'm look for the easiest way to get an ActionBar instance from a Fragment using AppCompatLibrary and API 8.
Already tried things like
getActivity().getSupportActionBar()
but no luck.

Try to cast it:
((YourActivity)getActivity()).getSupportActionBar()

Related

actionBar = getActionBar() - "application stopped unexpectedly" in old mobile set

actionBar = getActionBar(); is working fine In most of the new mobile sets, but in some old set it give error -
"application stopped unexpectedly"
Can somebody please tell me why it is so, or is there any alternatives for getActionBar(), like actionbar = new ActionBar(). I am not using support.v7
Start using v7 appcompat library, as it is the only way.
Read this : https://developer.android.com/topic/libraries/support-library/features.html#v7
Add this line in the build.gradle and re-sync:
compile 'com.android.support:appcompat-v7:23.2.0'.
Refractor your activity classes by extending to AppCompactActivity instead of Activity.
If you've set custom action bar, call setSupportActionBar([your_custom_toolbar]) in OnCreate() method.
You can now call getSupportActionBar() error-free :)
in your onCreate() function use this line to set your ActionBar.
getSupportActionBar().show();
Hope it solves the problem!
Older devices that you are using don't have the ActionBar; so the application crashes when you try to access it.
So, to support ActionBar on older devices, you can use the support library and use getSupportActionBar() to access it.
Instead of using the plain vanilla Activity, you should be using ActionBarActivity to be able to use the getSupportActionBar() method.

getFragmentManager vs getSupportFragmentManager ERROR

i need help for this . I realize sth very strange about this. In order to disable the Action bar (Title) , i need to add in this code
requestWindowFeature(Window.FEATURE_NO_TITLE);
So this only works I change from
extends AppCompatActivity
To this
extends Activity
So after changing that, I got error for the getFragmentManager.
Please have a look at the screenshot. And let me know if u guys have any idea wads going on? THx
Use getSupportFragmentManager() instead of getFragmentManager().
AppCompatActivity is v4 library therefore required to use v4 functions
And to use it in Activity instead of Activity change it to FragmentActivity. Then you can use getSupportFragmentManager()
requestWindowFeature() is not supported in AppCompatActivity thats why you could not use that method with AppCompatActivity.
Also if you are using AppCompatActivity you need to use SupportFragment and if you use Activity then use Fragment.
If you are using this import import android.support.v4.app.Fragment;,
then you must use getSupportFragmentManager.
Hope this helps.
You have to use the Compat method getSupportFragmentManager() instead of the regular one.
That's because it returns an object of type android.app.FragmentManager which is the type of the object where you're trying to store it.
If you use the regular method getFragmentManager() it'll return an object of type android.app.FragmentManager, which is an incompatible type.

getChildFragmentManager() method is undefined

I have the same issue has this post: android.support.v4.app.Fragment: undefined method getChildFragmentManager(). I need to use getChildFragmentManager() because I'm using nested fragments.
My problem is: the solution in the other post does not work for me:
My SDK is updated.
I got my android-support-v4 from the SDK folder.
I added android-support-v4 both the my project and ABS.
Here is my libs:
How can I correctly use this method?
Not sure if you've resolved your issue yet, but I was having the same issue and I resolved it. The getCildFragmentManager() method is a method of the class Fragment and not FragmentActivity. Just because your class extends fragment activity does not mean you'll have access to this method. What I did is I created an instance of my fragment in which I wanted to place the child fragment and invoked the method through that fragment instance. There are obviously other approaches to this, but this worked for me.
maybe something in your android manifest, look at the header or 'footer of your android manifest about minimum SDK version.
This is because by using old android.support.v4.
once you change the latest library.it will solved
I solved it by change it.
Hope it will work

ActionBarCompat throws NPE on configuration change

I wrote a sample application for the NavigationDrawer pattern with the ActionBar-Compat library project. Every time i change from portrait to landscape mode i got a NullPointerException during onAttach(). The Activity returns null for the getSupportActionBar() method. This happens in the Fragment which is changed by the NavigationDrawer. You can find the code on my github project: FadingActionBar-Compat (Line 162)
Maybe it is a error which can be fixed by a update from google in the future?
I found the solution by looking in to the ActionBar Compat source code. The ActionBar is ready to use in the lifecycle methode onActivityCreated(). You musst call the super-Method before. After that you can call getSupportActionBar() without a NullPointerException.

Using Google Maps in fragments with compatibility android support libs for android

I want to use google maps as one of the fragments of many. I found no other way then the one mentioned in the following way...
https://stackoverflow.com/a/8126287
and I was successful
But now I have 2 problems
I want to call a public method of the MyMapActicity(MyMapActivity is created as mentioned in the link given above) from another fragment. i.e. e.g. if I have fragmentA as some other fragment and MapFragment as a fragment related to MyMapActivity(created with help of above link) then I want to call a method from MyMapActivity from FragmentA. How do I do this.
I also want to resume the fragment with it is called...
I got my answer at:
https://stackoverflow.com/a/8126287
The link also provides a very nice example to follow. As I have only one activity this worked for me.
Have you tried using Action Bar Sherlock? http://actionbarsherlock.com/

Categories

Resources