How call method from Fragment in Activity? - java

I have a class MainActivity with three fragments.
Fragment Two uses a WebView.
I want to create a method Webview.reaload() in Fragment Two
and call this method from MainActivity.
How can I do that?

With getActivity() you will get the activity instance, cast it to your activity name and call function from fragments. Assuming activity name as MainActivity
((MainActivity)getActivity()).myFuncInActivity():

Interfaces work well will this kind of communication.
Check out this guide:
https://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity

Related

Do you know how to send an order from a fragment to a class?

I am creating an app which uses a fragment.
Say you have an Activity and a Fragment.
Activity calls the Fragment and if I press one of the positive/negative buttons on it, I want to send an order from the Fragment to the Activity to call up the camera.
So it will once return to the Activity and then it calls up the camera.
Do you have any suggestions?
Create a method in the activity like
void openCamera(){
//write open camera code here
}
Just call the open camera (YourActivityClassname)getActivity().openCamera();
For example, your activity name is MyActivity the code is :
((MyActivity)getActivity()).openCamera();
call from Fragment
There are possible solutions that I can think of. First one is using an interface and second one which I can think of is using viewmodel. Once you press positive or negative you can update your viewmodel from fragment and can listen it inside Activity or fragment which ever you are using. But I think for this interface will be perfect as you want to listen it into activity and could have used viewmodel approach if you were doing fragment to fragment transaction.

Correct way to start a map activity?

I want to start as MainActivity a MapActivity and include inside a menu fragment and some more stuff. My dilemma is what would be better, make my MapActivity as main or make another activity with the map and include it inside an empty MainActivity where I will put more stuff later?
make your main activity your map activity, MainActivity is really just a name for a class and can be entirely removed or changed, it doesn't have any special meaning, so if the main point of your app is to use maps, make your map activity your main activity.

Is there any method that i can call on SecondActivity to add a new item on Arraylist on FirstActivity.Fragment that has Recyclerview?

Hi im currently building my First android app and i have a problem that i cant solve this past 3 days. so i have MainActivity and on it there is a Fragment with Recyclerview and ArrayList< Item > listOfItem, on this Fragment there is a Floating button that when i click it will take me to SecondActivity there is a edit text on this SecondaryActivity that i have to fill then i will pass the data back to listOfItem.My problem is what method can i call on SecondActivity to create/add an listOfItem when i go back to MainActivity Fragment? I dont want to make Adapter and listOfItem to be static. Is there a way? thanks
Simple, You can achieve this by using Shared Preference.
What you need to do is just save that Edit Text data to Model(POJO), save that Model to Preference and in your Main Activity - Fragment on Resume method show updated data from that Preference.
Yeah here you need to take care of
The fragments onResume() or onPause() will be called only when the Activities onResume() or onPause() is called. They are tightly coupled to the Activity.
For more convient and efficient handling of data from any activity or fragement to any activity or fragment, you can use Event Bus.. Here is link where you will get the details information: https://github.com/greenrobot/EventBus
The best solution would be to start the second Activity for result.
startActivityForResult(ActivityBIntent)
Then checking the result overriding
onActivityResult() on the first activity and updating your list/adapter from it

How to extend class with two another classes?

I want to extend my class with 2 other classes Fragment and Activity.
I tried this:
public class Frist extends Fragment , Activity
{
.....
.....
}
But it's not working. How can I fix it?
You can't extend multiple classes. Why you can't extend fragment in another class?
You should use decorator or composition pattern
Java does not support multiple inheritance
Fragment and activity both are completely different thing.
If your want to make activity having fragment over it then in android studio create project with Activity with Fragment instead of with Blank activity
Then Automatically android studio will provide with some code having fragment and classes and xml for it.

Sending data from Fragment to MainActivity

I have a TextView in my MainActivity and a Button which is in a Fragment attached to the MainActivity. When I click the Button I want the TextView to say, "Button Clicked".
Is this possible?
I know two Fragments attached to the same Activity can easily communicate with each other and send data to each other. But can an object in the Fragment send data to an object in an Activity
Is it better programming to make the TextView its own Fragment and attach it to the Activity? Then I can simply have the two fragments send data to each other.
Sorry if this isn't a proper type of question for StackOverflow. I am new to Fragments and have not been able to find a clear explanation on this issue.
Thanks in advance!
The currently accepted answer (to use a static method in the Activity) is both odd and arguably "wrong".
The use of the static method is odd because there's just no need for it to be static.
It's wrong because the Fragment must have knowledge of the particular Activity in which it is hosted. This is "tight coupling", and also makes it so that the fragment is not re-usable.
There are two common solutions to this issue:
Create an interface containing the methods in the Activity that can be called by the fragment. Implement that interface in the Activity (all Activities that use the fragment), and then in the Fragment, use getActivity() to get the Activity, and cast it to the interface. In this pattern, one also typically checks (using 'instanceof') whether the Activity implements the interface, and throws a RuntimeException if it does not.
Use an Event Bus (e.g. Square's Otto, GreenRobot's EventBus) to communicate between the Fragment and it's parent Activity. I feel
that this is the cleanest solution, and completely abstracts the
Fragment from it's Activity.
You can create a static method inside your Activity which will have the TextView inside it. And when you need updatation just call it from fragment.
something like:
In Activity:
public static void updateText(){
//your textview
text.setText("Button Clicked");
}
Just call it when you will click on the Button from fragment.
something like:
From Fragment:
//Inside Button click listener
MainActivity.updateText();
Not tested, but hope this approach will work.
Have you tried the getActivity() method to retrieve a reference to the parent activity and use a method to set the data, something like:
// public final Activity getActivity ()
MyActivity activity = (MyActivity) getActivity();
activity.setText("...");
I may be wrong but I would try that.

Categories

Resources