Save data filled in multiple fragments together - android

Reqirement-I have an activity with three fragments. Each fragment has a form for user to fill and a proceed button.Proceed button will move user to next fragment.On third fragment proceed button will send data filled in the form of all three fragments to server.
Approach- I declared an object in activity.In each fragment when user click on proceed I assign fields of that object with data filled in fragment.When user click on proceed button in third fragment that object is submitted to server.
Is this approach correct? Is there any better way of doing this?

You could use Shared Preferences instead if you don't want to maintain an object and want to retain the information the user entered for later use in the app.

Working on main activity thur open fragment.
Create home activity one static method. and call after one fragment button clicked.
i have attach below step Please check.
1.Home activity create one method.
2.method name create addUserLineInfoFragment
3."A fragment" clicked set this code
((YourActivityName)getActivity()).addUserLineInfoFragment();
4.Home activity this method "addUserLineInfoFragment" set "B fragment" open code

Related

keep data retained of listview when switching between activities

i am struggling to keep data or items of list view when i leave one activity to another, for my example i created simple app on click will add number increased to a list view so each click create an item like 1 another click add 2 and so on.
the program works fin for main activity but then i would like to see the result on another activity i call it second activity but the problem is when i hit back button on second activity to go back to main activity, i will lose all of items on the list view.
i was looking on google so many information but could not get my head around it, so please advice and please be little more in detail as i am beginner, i think instance state or shared preference will do the job but i do not know any of them
thanks in advance and here is my app code for main activity and second activity and picture for output
sorry i add code as images becausethe site keep saying the code need indentation thank you
main activity[main out put][2]second activity[second activity out put][4]
You need to save the data of the ListView in some form, either in a file or in a database (local or remote).There is no direct way to store list view, but you can store the data from the list view and then set it in to the ListView later when you switch back to the activity.
You need to keep in mind that switching activity results to invoking of onPause() method in android, and if the data is not saved in the current activity then it will be lost when you move on to another activity.
Add all your values into the array, pass it to the adapter, then after clicking on the list view item, make an intent where you want to switch your activity (means from one activity to second activity), while implemented the intent, pass your array also, using intent.put extra. Then you will get your array in your second activity and coming back to your previous activity, again pass an intent (with your array) and get your array in your previous activity and pass it into the adapter.

Refresh the fragment when user change settings

I have a main activity. From this activity user can navigate to profile and in profile user can edit settings.
What I want is: when the user makes changes, I want to refresh the fragment in main activity according to these changes. As I can only update the fragment that main activity is showing when that activity came to top again. So how can I tell main activity that data has change and update the UI accordingly.
So I was wondering what's is the best way to handle that kind of scenario.
Consider creating a custom BroadcastReceiver inside the Fragment class, and on any change of settings send Intent with your custom Action.
If profile is an activity, start it using startActivityforResult, and get new data in onActivityResult if fragment instance isn't null , call some method say update in that fragment. If fragment instance is null, create new one using new data.
If profile is another fragment in the main activity, then store the changes in SharedPreferenceManager. In On Resume of your MainActivity's FirstFragment fetch data from SharedPreferenceManager. If it isn't same redraw that fragment else leave it.

How to check if fragment was created for the first time

I would like to check if a fragment was created for the first time so that I can launch a different fragment for introduction before coming to the retained fragment.
Normally for an activity I know I would use a sharedPreference-object to store a boolean value that tells me if this is the first time the user opens the activity, check the preference when the user starts the application, and if it returns true then show the middle screen.
Is the same possible for fragments?
Yes, you can use sharedPrference, but maybe it will be better to check the state when you actually switch to this fragment.
I mean you can decide in activity if to show introduction fragment or regular one before you create the fragment.

Android: refreshing a listview from a 'grandchild' activity

I have a setup as follows
Activity 1 uses a ListView and ListAdapters to display information
from list of objects of type A (by retrieving from the database the first time its called).
Upon clicking an item in the ListView in Activity 1, the control goes
to Activity 2, which again uses a ListView and ListAdapters to display
information from list of objects of type B.
There is a '+' button in Activity 2, which when tapped switches the
control to Activity 3. Here I can create an object of type B and save it to the database.
Now I use the setResult() in Activity 3 and onActivityResult() in Activity 2 to update the list in Activity 2.
So far so good. I can see the item of type B that I just created in Activity 2.
Now if I press the back button and go back to Activity 1, and tap on
the same item of type A then when I go to the Activity 2, the item that I had just created
does not show. However when I close the app, and open it again, and
follow the same path, I can see that item. (As the list was reloaded from the database)
So how do I update the list in Activity 1?
I hope I explained my question properly (apologize if not!). I don't want to put all the code here, since there is no issue with the code, unless my approach is wrong.
Put your code which loads the list from database in Activity.onResume() method. This way it should execute every time your activity is restored from invisible state (take a look at the docs on activity lifecycle for more info).
Also, you might want to implement loading from database using Loader. It monitors the data source for updates, thus keeping data up to date.

how to save the state of the activity when user pressed select?

i have created dynamic table-layout. in that layout in each row have four buttons.one button is use to navigate from one activity to another. my problem is when i switch from 1st activity to second activity by pressing button and i come back from 2nd activity to 1st activity clicked button loose its state.
Ex. 1) in 1st Activity i click button 1 and check the check box and go to 2nd activity after doing some operation i come back to 1st activity and i see the button1 and check box are unclicked and unchecked.
so how to maintain states of buttons
obviously it would be happen because i have to maintain the check box state when its come on the on resume() ..u can take a shared preferance to mantain the check box state .
Use variables and store the values in the variable when u clicked the button. Store that variable values in the Bundle object using the bundle.putExtras...() method. send this bundle object to the other activity using Intent.putExtra...s();. In the same manner ,if u need the those values then resend that bundle object and get that bundle object using Intent.getExtras....();. And finally restore the values from the bundle object.
This information may solve your problem.

Categories

Resources