How I can send liveChange values from Activity to Fragment in Java? - java

how I can change a TextView in an android fragment by variable of MainActivity. I try to log geographical values (from -> to in miles) and want this result show in a fragment live.
My App has a Viewpager and three fragments. In my MainActivity I have a FloatingAction Button for start tracking and stop tracking. The result will be save in a variable on MainActivity. The fragment is loaded already.
I think I need a construct like listener for my fragment?

Related

Retrieving an object when coming back to Activity

Here's the context of my question :
Context
I've made a Recyclerview that dysplay items thanks to an object called "identity_candle". When I click on items, it brings me to another activity with more description over the item. On this activity, there is a button to view more details about it that needs another layout by itself.
So when I click on the button it lands me on another activity.
IN SUMMARY, MainActivity (RecyclerVIew) => ItemActivity => "press button" => Activity or Fragment ? (Come back : Activity or Fragment => ItemActivity (keep previous object)
Problem
But when I want to come back to the previous activity it brings me error because the object state "identity_candle" is null.
Questions
What should I do ? Should I use fragment instead of activity when I press button ? (I've seen that object keeps their state between fragment and Activity but I'm not sure) Should I use SharedPreference ? If I want to create Fragment should I extends Fragment or ActivityFragment ?
This seems heavily rooted in understanding the Activity lifecyle. You'll likely want to go through that training if you haven't already. An understanding of onSaveInstanceState(), onRestoreInstanceState(), and Parcelables should get you there.

RecyclerView inside PagerAdapter

I have a main fragment that contains a viewpager. This viewpager gets the same secondary fragment (different from the main one) but with different parameters every time. Inside the said secondary fragment that is inside the viewpager, i have a recyclerview. Inside these recyclerviews are some fields that the user fills up. When the main fragment (the one that holds the viewpager) is closed (via a button click) i need to get the data from each recyclerview. How do i to that?
The best way to solve this problem is that implement interface which will give call back to activity holding these fragment and then from Activity pass it to fragment(main) and then use viewpager to get Fragment(secondry) and pass data.
Steps one would be implent interface which will give callback to activity and the get fragment from viewpager(Link for same)

Passing ArrayList from Fragment to Activity and Using BottomNavigationView

I am having the following problem, my application has two fragments, one with a Spinner where it contains the contents of the ArrayList and another fragment containing a ListView, I want to pass the contents of the Spinner ArrayList to the Activity to send it to the other fragment, I am doing so because I'm using a BottomNavigationView. Any idea how to do it?
your Fragments have context. This context could be casted to your Activity.
in Fragment
((YourActivity) getContext()).send(contents);

How to send data between fragments with SlidingTabLayout

Basically I am trying to create an app that passes data filled in an EditText on on one fragment, into a TextView on another fragment on a button click(The buttons is in the first fragment with the EditText). I use the SlidingTabLayout. I have 2 java classes that both extend Fragment and both inflate separate xml layouts(in the onCreateView). I have a java MainActivty with a public class"SectionsPagerAdapter that extends FragmentPagerAdapter, which depending on the swipe of the user displays 1 of the 2 Fragment classes. I am really confused on how I can send data between the 2 fragment sot that from the EditText in 1 fragment can get sent to the TextView in the other fragment on a button click. Could the suggested solutions be explained as simple as possible because I am relatively new to Android Studio. Many thanks in advance.
As per my understanding, basically you want to pass data between two fragments.
You can use activity for that from where fragments are initialized.
you can do this.
in MainActivity.java:
have a function setData(Object obj) and getData() which returns that object.
From fragment:
You can call those function of activity to save your data and get your data.
Here's way:
MainActivity activity = (MainActivity) getActivity();
Object obj = activity.getData();
activity.setData(obj);
I hope it helps.

Accessing Fragment objects from parent Activity

I have an ActionBarActivity that has a ViewPager with two Fragments and tabs created with ActionBar from the support library. My ActionBarActivity has also two buttons: cancel and save. When the save button is pressed, I need to get data from both of my Fragments. How do I do this? This data is stored fetched from the layout and stored in local variables in the Fragments.
You should be able get access to your fragments by use of FragmentManager by id or tag. getFragmentManager().findFragmentById() or getFragmentManager().findFragmentByTag()

Categories