In my fragment I have a 'dismiss' button that should behave in a different way dependent on which Activity called its parent activity (say TutorialActivity).
In the TutorialActivity I am already determining which Activity called it. How to pass that data down to the fragment?
My fragments reside in a PagerAdapter and I wouldn't like to need to pass this info as a 'newInstance()' parameter every time, as it seems an overkill, taking into consideration that this parameter would be the same for each fragment in my FragmentStatePagerAdapter.
You can access the variable from Tutorial Activity by making this variable public. Suppose variable name is parent. You can access it by using instance of TutorailActivity (suppose instance of Activity used in fragment is mTutorialActivity) then it should be like mTutorialActivity.parent.
But you need to pass instance of TutorialActivity as it may be used for other purposes also like fetching strings from strings.xml and other purpose. So it would be beneficial to pass Activity instance instead of variable.
What I ended up doing: I implemented said "different behavior" in the TutorialActivity itself, having moved the dismiss button up from the fragment. Then a simple onClickListener and a switch statement inside of it.
Related
i am trying to alter the activity content from fragment. for that i want to pass the activity handle inside fragment and do the required changes.
If i can do that why there is more difficult way of interface etc.
You can call getActivity() from the Fragment.
If you want to get your activity specifically you can cast it
((MyActivity) getActivity()).someMethod()
This will tightly couple your Fragment to your activity and prevent you using the fragment in a different activity easily so be careful.
Also you need to be careful with lifecycles and such as a fragment can become detached from an activity causing a NullPointerException from time to time. So it is recommended to wrap this in a null check
I wonder the difference between two ways of transfering data from activity to fragment.
One is using getArgument() and setArgument(). I can transfer data using these methods at Fragment's contruction time.
Another is using getActivity() method. Like this way
((HostActivity)getActivity()).getXXX()
After declaring getter method of data Fragment may use, call this method in fragment through getActivity() and Type casting.
I think second one is easier and convenient. Because get/setArgument() can be called only Fragment's contruction time.
So, How to apply these 2 way to sending and getting data between Activity and Fragment?
A Fragment represents a behavior or a portion of user interface in an
Activity. You can combine multiple fragments in a single activity to
build a multi-pane UI and reuse a fragment in multiple activities.
Because fragment can reuse in multiple activity, if you use getActivity() with type casting, you must check instanceOf activity before call method. And each of activity use that fragment, you must implement method getXXX().
Use newInstance method in fragment, you only pass require parameter for it.
If you create fragment for individual activity, you can apply 2 ways transfer data.
The fragment has an independent lifecycle from activity with specific threads, functions and handlers. So you can use getters/setters Activity variables like a global variables and bundle data (arguments) to independent fragment variables.
I'm currently trying to switch my Android application from individual activities to fragments contained in a single tabbed activity, however, I'm running into some snags with figuring out how to pass data between them. I was originally just using intents. However, now that I'm using fragments, I'm currently storing any data I need as a field variable in my tabbed activity (As this answer outlines). I'm getting null pointer exceptions because my tabbed activity is attempting to load my first and second fragment, but my second fragment depends on a EditText value from my first fragment. Is there any way I can load these fragments one at a time, and pass my field data (and load my second fragment) when the user swipes? If there is a way, is this the best way of solving my problem? I'm very open to other suggestions. Thanks guys!!
There is a special dedicated topic for this here http://developer.android.com/training/basics/fragments/communicating.html.
I would declare two interface one in each fragment. Then implement the interface in the activity. On EditText change in the first fragment send the value to the activity and store the value in the activity in a instance variable. Then on the second fragment retrieve the value in the second activity from the activity.
I have a dashboard that is a fragment. Everytime I click a button, the dashboard is replaced by another fragment.
The click listener is implemented inside the dashboard fragment class. But I read somewhere that the better way to do it is to make the listeners inside the activity. Is it true? Why?
If yes, I can change it, i only have to copy the method in dashboard fragment to the activity, and make use of XML onClick feature.
I honestly can't think of a reason for declaring an onClick listener for a fragment in the activity.
First, fragments are suppose to be modular. Maybe you use it with this activity or that one. Putting the onClicks in the activity hardcodes a relationship between the two. Your activity is searching for the fragment, which isn't always there, and your fragment can't work except in that activity.
Second, where you declare your on click determines where it's implicit reference will be to. If you declare it in the activity, it can call activity functions, but It has no idea which fragment it came from. How does it reference fragment functions / data? Sure there's elaborate workarounds but why?
On the other hand, if you put it in the fragment, it can call the fragment functions. and it has the same life-cycle as the fragment (being attached to a fragment view), so the implicit reference isn't going to create a memory leak (by itself anyways). And if you want to call the activity, just use getActivity and cast it to your interface or subclass.
My launch activity starts up another activity whose launch is set to single instance. In this 2nd activity, I have a public method. I then start up a 3rd activity and that activity needs to access the public method in the 2nd activity. I don't want to use startActivity and pass it extras because I assume the onCreate will get called (or am I wrong?) and I need to avoid the 2nd activity from reinitializing itself.
When an activity is started using startActivity, is it possible to gain access to the underlying class instance itself and simply call the method?
I actually came up with a simple solution. As a matter of fact you can access the underlying class of an activity. First, you create a class that is used to hold a public static reference to activity 2. When activity 2 is created, in its onCreate method you store "this" in the static reference. Activity 2 implements an interface with the methods that you want available to any other activity or object. The static reference you hold would be of a data type of this interface. When another activity wants to call a method in this activity, it simply accesses the public static reference and calls the method. This is no hack but is intrinsic to how Java operates and is totally legitimate.
It is not a good idea.
As I can understand method from second activity is actually not connected to particular activity while you want to call it from another one. So carry the method out to other (non-activity) class (maybe static method) and use it from both activities.
It's not directly possible to gain access to activity object started using startActivity (without using some hacks). And frankly you shouldn't even trying to accomplish this.
One Activity component can cycle through several Activity java object while its alive. For example, when user rotates the screen, old object is discarded and new activity object is created. But this is still one Activity component.
From my experience, when you need to do things you described, there is something wrong with your architecture. You either should move part of activity's responsibilities to Service or to ContentProvider, or use Intents, etc. Its hard to recommend anything more specific without knowing more details.
No there is no way to pass a reference via startActivity() however you can use some sort of shared memory to keep reference to your Activity. This is probably a bad design. However passing an extra with your Intent will not cause onCreate, that is completely related to the lifecycle.