Access fragment method from intent activity - java

I have a fragment, and I am starting an activity from the fragment. Now I want to call a method from the fragment in the new activity.
I tried to use interface but it seems I can't since I don't create an object of new activity in the fragment to have it call the setListener(). I am using intent to fire up the new activity.
I am not able to find how I can get fragment instance in new activity or how to call a method in the fragment. Any help would be great!

A fragment is tightly coupled with the Activity. You always need to create the Activity as the host for the fragment.
From the documentation:
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. You
can think of a fragment as a modular section of an activity, which has
its own lifecycle, receives its own input events, and which you can
add or remove while the activity is running (sort of like a "sub
activity" that you can reuse in different activities).
So, you need to do the communication between Activities. An activity should not communicate with a fragment it didn't host. But it should communicate with the Activity where the fragment is hosted.
For example,
if you have two Activity which are ActivityOne and ActivityTwo. Where ActivityOne has a Fragment called ActivityOneFragment.
When you need to get the ActivityOneFragment from ActivityTwo, you need to communicate with the ActivityOne then tell it to get the ActivityOneFragment:
ActivityTwo -> ActivityOne -> ActivityOneFragment
You shouldn't do this:
ActivityTwo -> ActivityOneFragment

No, you can do not that. Because the background activity is paused/ dead. so you can not access its method.
if it is general method, you can put that method in other class. call it your utility class.

Related

Communication between fragments that belong to two different activities

I'm very new to Android development.
I've made some researches to achieve passing data from Fragment A in Activity A to Fragment B in Activity B.
Is that possible?
All I could get from the tutorials on the internet is that we have to create an Interface that will be implemented by the parent Activity of two sibling fragments. How to achieve it when the fragments belong to two different activities? I'm stuck.
Thanks
You would achieve it by implementing the said Interface and put the extras into the Intent for the Activity that has the other Fragment.
Pseudo code could look like this
Fragment A - calls interface methods on Activity A and passes data
Activity A - calls startActivity with the extras inside the Intent
Activity B - got started and evaluates the said Intent and passes it to Fragment B
Fragment B - receives the extras from Activity B
you can use Intent to pass data between activity and from sibling fragment call method of activity like ((MainActivity).getActivity).methodSentData(String exampledata) and ((MainActivity).getActivity).methodGetData(); to get data in the fragment.
methodSentData implements intent.putExtra("data",data); to sent data to other activity and methodGetData() implements getintent().GetStringExtras("data");
You can use local broadcast receiver.

Android UI fragments. Changing fragment due to button push

I'm trying to open a new fragment based on a button push in a previous fragment. What's the best way to implement this?
I'm curious if it's Activity -> .add + .commit original fragment - > from that fragment.java .replace new fragment?
Or do I need to pass an intent back up to the activity and create/replace that fragment from the activity?
So summarize: Activity A - > Fragment 1 - > Fragment 2.
I'm also slightly confused on what things I [need] to #Override. I think only onCreate and onCreateView [within each fragment]?
I'm only looking for high-level here; I want to struggle through the code myself.
Fragments are generally unaware of their host so I would use the standard callback method to call your activity and ask it to switch fragments.
Create an interface
Have your activity implement the interface.
Cast the getActivity() call to your interface.
Call the interface method.
This is much cleaner than casting the host activity and calling methods on it. It also means your fragment can be hosted in different activities with no cast errors.
http://developer.android.com/training/basics/fragments/communicating.html
You need to cast getActivity like:
((MyActivity) getActivity())
And then if you have that, you can call a method in yout activity:
((MyActivity) getActivity()).replaceFragments(Object... params);
And inside the method you should do the replace fragment process.
So simply you have the right idea.

Activity Lifecycle and the stack of activities

I have an activity with the following screen scheme:
|------> Activity1
MainActivity |------> Activity2
The application navigation only use startActivity() calls and the standard "back button" function.
Considering an Activity as created when its in between the onCreate and the onDestroy methods.
May I assume that when the activity1 or 2 is created then the MainActivity is created too?
There is no guarantee that the MainActivity will still be alive when you have Activity1/2 in the foreground. Definitely it will be created when the app starts since it is the only way to reach to Activity1/2.
Once the MainActivity is covered by another activity it will be in Stop state and can be killed by the system when resources is needed.
You can have some control over this behavior by specifying android:noHistory="true/false" in your activity definition, which by default set to false
Yes, when Activity1 or Activity2 is created, then by your definition MainActivity has also been created. So, if you are trying to access variables or methods from within MainActivity, then will be there.

Fragments onClick method in fragment element

I read quite some articles about fragments, but I am still confused about how to do what.
I have a MainActivity, which displays two fragments side by side. In one of the fragments I have a button and defined in the fragments layout XML for the button
android:onClick="buttonClicked"
Now I want to implement that method
public void buttonClicked(View view)
I would have assumed that this has to be implemented in FragmentA.java and not in MainActivity.java. But it only works if that method is implemented in MainActivity.java. Why is that? To me that doesn't make sense. Pre Honeycomb a method belonging to one activity stayed in that activity, now on a tablet I am merging many activities to one MainActivity and all the different methods are merged? Whatever do you put for example in FragmentA.java then? What if you have to start you an own activity because this app runs on a handheld, then the onClick method has not to be in the MainActivity but in the Activity which needs to be called then. I am pretty confused at the moment...
I'm not sure what the specific problem is, but maybe this will help.
From the Android documentation on Fragments:
You should design each fragment as a modular and reusable activity component. That is, because each fragment defines its own layout and its own behavior with its own lifecycle callbacks, you can include one fragment in multiple activities, so you should design for reuse and avoid directly manipulating one fragment from another fragment.
That is, you should never manipulate a fragment from another fragment; rather, this should be done through the underlying Activity. Read the "Creating event callbacks to the activity" section in this article for more information (it's important stuff!!).
On the other hand, if you want the button to perform an action within the Fragment itself (i.e. if you wanted a Button click to change the text of a TextView within the Fragment), you should implement this in the Fragment, not the Activity (this is because the resulting behavior is contained within the Fragment and has nothing to do with the parent Activity).
Leave a comment and I can clarify if my post is confusing... I only recently began to understand Fragment's myself :).
Well,
I guess it is related to hierarchy of android context structure.
Activity is host of all child views and hence you can say fragment is actually using its host's context.And that's why when you use onClick with fragment system always searches it in Host activity of fragment.
Check it on.
Android developer onClick attribute description
I haven't checked one thing but you could put a test.
By providing implementation in host activity rather than in fragment,but use onClick on layout file of fragment.It should call parent's method.

Android - Passing the main activity around

I have a several activities in my app. The main activity (activity1) extends ActivityGroup (I need to support 2.2 and above, so I cant use fragments).
The main activity1 creates activity2, which in turn creates activity3. But when creating activity3 I want activity1 to create it, not activity2.
How do you pass the main activity around between activities?
Thanks
A better way is to use startActivityForResult() in avtivity1 to statrt activity2.
and when you want activity3 just finish activity2 and in activity1 override
onActivityResult() and start Activity3. passing activity instance is not better idea
since that Activity may get killed when at background and thus susceptible to throw an
Exception.

Categories