In my application Activity A has a list of items
on choosing a product i go to activity B that give details of that item and a button "Choose this item"
which will go to activity C.
In actvity C this choosen item is displayed and there is a button "choose more items"
on clicking this i have to go back to activity A, and repeat the same steps.
BUt when another item is choosen, activty C should display both items.
SO i thought from actvity C, i start activity A, by calling startActivityForResult() and add the result to existing list of items.
In that case, i have to call finish() of A to retrn value.
Is this right way of implementing
Since "choose more items" can be clicked many times in real life, wont it end up killing and starting many times the same activity
I have set the launch mode as single task for the activities
What would be the best way to handle this situation
thanks a lot for your time and help
Well, I will suggest that you can take a static List or ArrayList and work accordingly.
1.) You can take a public static List and can initialize as per your requirement, something like this.
public static List<CartListClass> cartlist = new ArrayList<CartListClass>();
2.) You can access this from any class using Acitivity_name.cartList and add the required information.
3.) Now, when you will come to Activity C you will have all the required information in the static List, so from there you can get and show the required Details of the product.
And, the static List will maintain the information also, when you select another product or item from Activity A to Activity C, it will be added to the List below the previous item or product.
UPDATE:
Better approach would be using BroadCastReceiver to update the value or using Interface.
I would save identifiers of chosen items in Activity C to preference.
So, in resume of Activity C, I'd check how many I have in the saved preference and load corresponding data to views.
Related
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.
I have a the following scenario:
Activity A: A list of items where user can select an item
Activity B: A list of options for the selected item
Activity C: A shopping cart displaying the selected item and selected options
In Activity C I have a button to allow for continued shopping. It easy to load a new instance of Activity A when the user clicks on continue shopping but if the user adds many items I will have many instances of no longer needed Activities.
My two questions:
Is there a safe way to get back to activity A? What does the code look like?
Should I just recreate the Activity each time and trust the OS to kill the previous instances? In other words is there a performance reason to worry about having multiple instances.
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.
i am very confused about how i should implement the following in my android application.
Let me explain the functionality that i want :
-i have a list view in my First Activity , say Activity A
-now , from this activity , i start another activity(activity B) with an intent for a result.
-with the result i get from the B, i want to update the listview in activity A.
-That is add an item in listview with the string returned.
i am thinking of, storing the items in array list and setting the array list to an array adapter to the listview.
now when the Activity B returns with a result i modify the arraylist and again set it to the listview.
i want to know, is it possible??
and also i have a question : when does the activity B returns?
so that in activity A's which methods(such as onResume(), onStart(), onRestart() ) should i write the logic for modifying the listview...?
i am very new to android development
Regarding your first question, I think you are on right track. You can just add the returned string to an ArrayList and then to the ArrayAdapter.
For your second question, you will get a clear understanding how it works, by seeing the activity flow diagram here: http://developer.android.com/reference/android/app/Activity.html
I'm making an app which has a flow roughly as below:
User starts on the main screen with an empty list, hits menu, and goes to "add item." (Activity A)
User is given a new activity which allows them to specify search criteria, then hits "go" to do a search. (Activity B)
User gets a list of results, and can click on one of them to view more details. (Activity C)
User sees details of item, and can use a menu item to save it to their list in Activity A. (Activity D)
Right now, I am having each Activity call each other Activity for results, and then it is passing the result all the way back up the stack as it returns to Activity A.
Is there a way to jump this, since all I want is for a result in Activity D to get to Activity A directly?
Note that a user should still be able to navigate backwards (using the back button) through each activity, but if they explicitly save the item in Activity D, I want it to jump straight to Activity A.
I recommend just invoking the activities (not using the *ForResult) calls, then having activity D invoke Activity A with an INTENT_ADD_ITEM with data, then have Activity A add the item.
Hope this helps...
Just so that people can benefit from what I learned later...
The key to solving this problem is using flags with Intent, in this case using FLAG_ACTIVITY_CLEAR_TOP. Other flags are useful as well in controlling the flow of your UI.
It is a bad idea to try to solve this problem by chaining startActivityForResult() through activities. It means that it's difficult to change the flow of your application.