Handling dependent data between tabs in tabbed activity - android

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.

Related

Get activity that called parent activity of a Fragment

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.

Access Fragment elements from Activity parent in Android Studio

I need to implement in a android app a big form that have 4 sections, so i created 01 Activity with 4 fragments using the ViewPager. Each fragment has some EditText fields. On the 4th fragment, i have the Button to finish the form, so i'd like to click on it and get all the EditText fields.
I think the best solution is to get all of these EditText fields on the MainActivity.java , but when i try to access theses elements from any fragment, I get only this error: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
any idea how to access these elements or any better solution?
Have a shared POJO in the activity, and update it from your Fragment class on every next press.
Read here on how an activity should communicate with fragment
http://developer.android.com/training/basics/fragments/communicating.html
As suggested in the docs you may have an interface for each fragment being implemented by the activity, with the final fragment notifying finish using a boolean value.
I had such a scenario once when creating a survey application.
This is the approach I took:
Create your fragments as needed.
As expected, add your first fragment to your activity.
Important part here - when a user clicks NEXT or Swipes to the next Page, add the answers to a map (each question will map to an answer)
Now repeat this process until you reach the final fragment and simply read out the values (Questions and Answers) from your map.
I found this easier to achieve and gives the expected outcome!
Good luck and happy coding!

Passing data between Fragments and saving state on Screen Orientation Change

Before I begin, I have read this, this and some more articles online. I am still unable to find a right way to do it.
So I have just one activity in my App, and 6 fragments. First one is a ListFragment which loads a list from a SQLite table. When user taps on a row in this list, I do 2 things:
1) Get an int from that row through a listener, and pass it back to the parent activity which stores it as a class variable using a simple setter method.
2) Replace this ListFragment with another simple Fragment. This new Fragment uses a simple getter() on that class variable to retrieve some information from a different table, and show all the details to the user.
So far so good. Now if I am on this details Fragment, and I change the screen orientation, the activity state is not reloaded (as I am checking if savedInstanceState is null in the onCreate()), but however, the class variables lose their value, and my app crashes.
Basically I am trying to pass data from the ListFragment to the details Fragment. I am doing it through the activity, which is causing a problem. As per Android Documentation:
All Fragment-to-Fragment communication is done through the associated
Activity. Two Fragments should never communicate directly.
There is no specific code which is giving me trouble, so didn't post any.
The onSaveInstanceState and onRestoreInstanceState is only used to save and restore per-instance state of an activity in case your activity is destroyed by OS (for example, to free the memory or in order to recreate it when the device orientation was changed). So you can save your variable in onSaveInstanceState and get them back using onRestoreInstanceState.
For your next question, I think this article will help you. Also answer by Gene for this question will help you.

Fragments' lifecycles preventing me from using notifyDataSetChanged upon activity swaps

I have a generic android tabbed setup with the default code.
I also have a second activity, listaddactivity that basically just constructs a custom parcelable object and spits it back out to the mainactivity with all the tabs and fragments.
My second tab contains a ListFragment; which I attempt to update with the data I received from listaddactivity's intent sent to mainactivity. My problem lies in that I have discovered that my ListFragment is being destroyed and reconstructed upon switching activities; as well as the data that is contained in it. Meaning that whenever I received my intent in my mainactivity that came from my listaddactivity it will update the list; but only with the latest object that I've constructed (not storing all previous objects).
How can I preserve this ArrayList<myCustomObject> of objects or preserve the ListFragment as to allow for the list to continually grow?
My solution:
I managed to make a work around for this by using a singleton as outlined by this page:
https://gist.github.com/Akayh/5566992
This allowed me to store and retrieve the arraylist from the singleton; and use it on any fragment I pleased by instantiating the singleton.

Passing pointer to Activity to next Activity

I'm very new to Android programming (and Java for that matter) coming from an iOS background.
What I am trying to do, is pass a pointer to a Fragment from one Activity to another.
Basically, I have a starting activity called BeginActivity that handles a couple of Fragments for login and register screens. Once logged in, I load up the main activity of the app called TabsFragmentActivity using this code:
public void loggedIn() {
Intent intent = new Intent(this, TabsFragmentActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
this.startActivity(intent);
finish();
}
I'm using FLAG_ACTIVITY_CLEAR_TOP as I dont want the user to go back without actually logging out first.
Now the problem:
In BeginActivity I have a pointer to a fragment that holds the users data. I am using it like a singleton, that the first few view fragments can access from BeginActivity.
I need to pass this same object to the new TabsFragmentActivity before I call finish() on it.
How do I do this?
I know I can use putExtra() but I believe that is just for strings etc.. and not other Fragments.
Is there a way in the newly created TabsFragmentActivity that I can reference the BeginActivity to 'grab' the pointer?
Thanks
First of all, you should be sure about Fragments and Activity life cycle.
Fragments are designed to be reusable UI complex components. They look like activity, but you can reuse. So,you can have as many activities you need containing the same fragments, but not the same instances of these fragments.
If you just want to pass you user data for another activity you must use Bundle and putExtra(). Depending of the user data type can be necessary implements Serializable or Parcelable Interfaces, as #gheese said.
If you want to use the same UI appearence of your fragment on two or more activities, besides use Bundle and putExtra. Each activity that you want this behavior must contains a field whose is a Fragment and in the moment of starting this fragment you can use getActivity().getIntent().getExtra to get the user information and populate your fragment.
Basically you need to be able to pass your class via the intent, look at Serializable / Parcelable interfaces
This question has the answer you require
How to pass an object from one activity to another on Android

Categories

Resources