I have an application in which I am attaching different fragment with activity. and in that activity container I am replacing different fragment.
So at the start of the activity I replace the MapFragment in the activity container. Later on I replace other fragment and then from main activity I replace again the map fragment. Although I think I have wrong implementation of map in fragment but I wanted to know and wanted to do some different things and I think as I have no better understanding of fragment so I think that is why I am getting null pointer exception
What I have done SO far
Case 1.
AS I said On App start I am replacing map fragment but doing nothing more except showing user current location by just setting user location enable in google map v2. so on the button click of the map let say show other location , fetch location information from server and draw pins on the map. this is working good as far as the map fragment is there.
Case2
if user click button let say "Show location" while there is other fragment in the container let say About Us fragment which contains app info , so now on this button it is supposed that map fragment should be replaced again and then the function in that fragment which is going to download locations from the server and to show them on map should start working as I am calling on button click
fragmentTransaction1.replace(R.id.frame, myMapFragment, "MyMapFragment").commit();
fragmentManager.executePendingTransactions();
myMapFragment.GetAllLocations("friendsLocation");
so this GetLocations() should run , but at this stage the app crash saying null pointer exception. and in this function myMapFragment.GetAllLocations("friendsLocation")
there is a need of context which I taking as getActivity(), the context is needed as I have to make a call to web api using ION library which needs context.
So these are my basic questions
How can I get to know that fragment is started and then the function which I have called from MainActivity should run after the mapFragment is loaded and started so that I can not get the null context in the getActivity() ?
Is there any other way of doing that , I hope you understand my need that is , I want to replace the map fragment and I want to call the fragment method on the same time by passing the parameters to fragment method from the main activity , but How can it be done ?
An idea is in my mind and that is while replacing back the map fragment through Mainactivity button , i should set some static value in the mapFragment let say
public static boolean isLocationShouldDownload== flase //in map
fragment
I should set it true while replacing fragment
and then when the fragment is started after it is in running condition it should check this variable if it is true (set by the main activity) then it should download and start plotting pins. but I have no idea how can I get to know that fragment is running .
I have no idea how and where I have to implement my idea which I have stated in point no 3.
Please share your idea with me and give me suggestion or share me a source code that how can I achieve this task through main activity that fragment should replace and download the locations and to show them on map without getting app crash.
Related
android.app.Fragment android.app.FragmentManager.findFragmentByTag(java.lang.String)'
on a null object reference
I had found numerous posts with this issue here on SO, but none of them helped me.
It happens relatively rarely (1/100). The place where I call the getFragmentManager() is on the main thread, when a button is clicked :
My flow is simple , first I have fragment A, then add on it several fragments until I get to fragment X.
Once a button is clicked in a custom view class that is instantiated and held in the fragment X, I call :
fragment A.getInstance().showLoadBar();
in fragment A the showLoadBar calls a fragment just for displaying loading, and it uses the getFragmentManager() there.
Most of the time it works great, and I am not able to recreate this crash. But I can see that this crash does happen sometimes.
I have in my project an activity (that extends FragmentActivity), and two fragments (extends v4.fragment and one of them has a map fragment).
I made my project for tablet, and both fragments are shown on screen.
the problem is, I tried communicating between them (adding a marker on map from another fragment) , by using an Interface that is implemented in the activity.,
and for some reason my googleMap object returns null.
when I'm trying to use method inside the fragment
(e.g: options.mapType(GoogleMap.MAP_TYPE_SATELLITE)) using my googleMap object
it's working.
any idea what did i do wrong?
My bad. rookie mistake- I did'nt call the right fragment id from xml.... ( :
The scenario is as follows:
"Dashboard Fragment" -> Fragment 1. This transaction is added to back stack. With tag specifed while replacing the fragment as "frag1";
Fragment 1 -> Fragment 2. This transaction is also added to back stack. With tag while performing replace operation. The tag is let us say "frag2".
Now in Frgment 2 when the user wants to save data, it will make the web service call. And if it is successful, I want to replace Fragment 2 with NEW Fragment 1. And if the user presses the cancel button, it will simply perform the back press event and load the Fragment 1.
In the case of successful web service call, replacement is done properly but when I press back button there is still OLDER Fragment 1. It is for obvious reasons.
To get rid of this when the web service call in Fragment 2 is successful, I have written the following code.
if (null != getActivity().getSupportFragmentManager().findFragmentByTag("frag1")) {
getActivity().getSupportFragmentManager().beginTransaction().remove
(getActivity().getSupportFragmentManager().findFragmentByTag("frag1"));
}
loadFragment1();
and then I use the call back method to load the Fragment 1 in the same frame layout and replace Fragment 2.
It goes inside of the if block but the older fragment "Fragment1" is still there- UNHARMED.
I am not using any tag in the method addToBackStack() and keeping it like this addToBackStack(null)
I am not able to remove the older Fragment 1. Please let me know what is wrong with this approach. Thank you.
try this
findViewById(R.id.fragment1).setVisibility(View.GONE);
Is there some way I get already created currently displayed same instance of fragment in my activity. I DON'T to use
findFragmentById(int id), simply I never created that
findFragmentByTag(String tag), because I am not adding tag in every fragment .offcourse due to some requirement.
getFragment(Bundle bundle, String key), because I never am putting in bundle.
Although I may look like fool to mention that, but I want something like this. Is activity keep some fragment instance somewhere.??
What can be the best approach I can take to achieve this requirement.
UPDATE
Okay, so let me tell you why I can't use above methods. If I am adding various fragment in one activity, where I always want to come back to one fragment when back is clicked. (As we have in navigation drawer, u know). And unless there are inner fragment. so for that I don't want to add in the back stack.
Now even if I have the tag associated with my fragments, I cant say for 8 fragment if- else-if-else for getting the tag. That I know is not correct. So first two ways goes out of my option. Now third one. I exactly don't know where to keep it. And even if I keep it where will I get the bundle and key every time I just want my fragment.
You can get from fragment Manager
List<Fragment> fragList=fManager.getFragments();
for(Fragment fr: fragList){
String fragClassName = fr.getClass().getName();
if(fragClassName.equals(Abc.class.getName())){
Log.i("Fragment:","Abc");
}else if (fragClassName.equals(Xyz.class.getName())) {
Log.i("Fragment:","Xyz");
}
}
In my application, I have tabbar functionality. In one tab i am displaying server data in lisview, and on clicking on that detail page for that list item will be open in new fragment.
But when I press back button from that detail page, every time oncreateview called of previous page, so every time listview created and new fetches new server data. So how to prevent such and just display previous state when back button press?
I know it has been too long to give this answer but what i am guessing is you are replacing your fragment with other one. I mean to say is you are using
ft.replace(R.id.realTabContent, fragment);
to move to other fragment which is you are using in your onItemClick so simple solution is use
ft.add(R.id.realTabContent, fragment);
instead of replacing your fragment.
Understand the difference between replace and add. This will solve your problem.
Replace : it will replace the original fragment and re-create the view when you come back
Add : it will just add a new fragment to stack.
Hope this will help someone who is facing the same problem...
I don't think prevent calling onCreateView is a good idea, beside if the function is not called, there will be exception, so NO, you shouldn't. Instead of doing that, I suggest you move your "listview created and new fetches new server data" into another place where you can call explicitly (when you want, where you want, onCreate() is a good place), don't put it in onCreateView(). Hope this helps.
You should cache your data objects apart from view variables as their references needs to be updated if you are fetching any data from server and don't want to make a call again then use branching to branch out that code.
Create an init() method where you do all initialization and server calls and logically branch out call to init() method whenever you don't want to call init().