Update a view in a fragment [duplicate] - android

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
ViewPager Activity to notify a Fragment of a specific event
I am experimenting with using fragments and I ran into this problem.
I have a service running in the background which I want to update a listview in a fragment.
First I just wanted to have the fragment listen for a broadcast, but it seems like registerReceiver is not allowed in a fragment?
How do I do this. Do I need to listen for a broadcast in the main activity and then somhow send it to the fragment?

I'd use the activity for this. Google has some good examples and description of the topic fragment and activity communication. You can read about it here: http://developer.android.com/training/basics/fragments/communicating.html
In short, fragments use listeners to communicate with activities and activities just use public methodes in fragments.

Related

Pass data and update fragments text/buttons on same host activity of NavigationUI

In the activity that hosts the navigation drawer I have the "Login" component on the menu which manages the google authentication. I want to propagate the user's info on another "Home" fragment and update its view with those infos.
QUESTION:
How do I pass data between two fragments of the same host activity in NavigationUI and how do I update the TextView of the "Home" fragment for once (since the view refreshes its default values all the time).
I tried with intents by specifying to the main activity the extras to receive so I can propagate them to the fragments, but it does not seem to work (getting the "Cannot find activity" error).
What am I missing?
Actually, you have asked multiple questions trying to answer all of them.
How do I pass data between two fragments of the same host activity
if you are using ViewModel, use LiveData to communicate between two fragments. - suggested approach
if you are not using ViewModel then use the interface callback approach.
examples of the above two are mentioned here.
https://developer.android.com/guide/fragments/communicate
How do I pass data between two fragments of the same host activity using NavigationUI
in the nav_graph.xml you can find the arguments section where you can mention the arguments you want to pass.
An example of that is available in the given link
https://developer.android.com/guide/navigation/navigation-pass-data
please add a comment if you want any more answers

how to communicate between fragment and dialogFragment [duplicate]

This question already has answers here:
How to get data from DialogFragment to a Fragment?
(7 answers)
Closed 6 years ago.
I have a fragment and a dialogfragment, I want to communicate between them:
send data from fragment to dialogfragment and
send data from dialogfragment to fragment
I read about setTargetFragment and getTargetFragment but I couldnot understand how to use them correctly to send/receive data to/from dialogfragment.
any help?
There's answer for this already. Look at this for Communication between Fragment and DialogFragment - if this helps you, you can accept the answer, otherwise you should motivate why your specific problem is different from the one presented in here.
To see how to use the setTargetFragment and getTargetFragment, you can look at the [selected answer here] (How to get data from DialogFragment to a Fragment?) - specifically, you might find the code helpful for what you want to do.
So, I have Communicated from DialogFragment to Fragment using this way
and Communicated from Fragment to dialogFragment using setArguments() and Bundel

What is the best way to pass data from one fragment to another in android? [duplicate]

This question already has answers here:
How to pass values between Fragments
(18 answers)
Closed 7 years ago.
What is the best way to pass data from one fragment to another in android ? I know Otto and Event Bus can handle the issue, but what exactly should the non-library way to do out?
Please consider following before answering
http://developer.android.com/training/basics/fragments/communicating.html
Often you will want one Fragment to communicate with another, for example to change the content based on a user event. All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.
You can do it using Bundle
MyFragment fragment = new MyFragment ();
Bundle data= new Bundle();
data.putString("Key", "Value");
fragment .setArguments(args);
getFragmentManager().beginTransaction().add(R.id.container, fragment ).commit();
Retrieve the bundle in another fragments onCreate by
String value = getArguments().getString("Key");
If you don't use Otto / EventBus then you should use listener interfaces.
Implement a listener in your MainActivityand pass the value to another fragment from the MainActivity. Fragments are generally used as reusable components and it is always better to reduce the coupling between fragments using listener interfaces in your MainActivity.

Android - end fragment activity [duplicate]

This question already has answers here:
Finishing current activity from a fragment
(13 answers)
Closed 9 years ago.
What is the equivalent of
finish()
for a fragment activity in
OnPause();
I cant find it anywhere, I just want the fragment to end when it is paused
As others have already stated, call finish() as you would in a regular Activity. In regards to the onPause() part of your question, I'm not sure what you are looking for, but it might be a good idea to review the lifecycles of Activities and Fragments...
Activity Lifecycle
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
Fragment Lifecycle
http://developer.android.com/reference/android/app/Fragment.html#Lifecycle
call this line and it will kill the current fragment
getActivity().finish();
You can still use finish(). FragmentActivity extends Activity
http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html
A FragmentActivity is extended from an Activity, so you can still use the finish() call to end your class.

Fragment vs. FragmentActivity [duplicate]

This question already has answers here:
What is the difference between Fragment and FragmentActivity?
(4 answers)
Closed 9 years ago.
I am new in Android programming. I want to do a Fragment, I have seen examples that uses Fragment and FragmentActivity, What's the difference between them and which cases should be used each one?
Thanks
FragmentActivity is our classic Activity with fragment support, nothing more. Therefore FragmentActivity is needed, when a Fragment will be attached to Activity.
Well Fragment is good component that copy the basic behaviors of Activity, still not a stand-alone application component like Activity and needs to be attached to Activity in order to work.
Read here Difference between Fragment And FragmentActivity
FragmentActivity is part of the google-support-v4 lib which is basically adds a Fragment support to system with OS under 2.3. So FragmentActivity is exactly as a simple Activity only it gives you the ability to add Fragment to it.
Fragment is an object that shares parts of the Activity life cycle and can be added as part of you UI to an Activity or FragmentActivity with it's logic. the beauty of Fragments is that can be reused across different Activities in your application.

Categories

Resources