Android thinking about the stack - android

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.

Related

What's the best way to store an ArrayList between activities, and save to internal storage on exiting app

I've been developing an Android catalogue app for a flooring store and wish to implement a persistent cart feature. The hierarchy of activities is as follows:
Activity 1: Choose the category of product (carpet, timber, etc.)
Activity 2: Shows list of products in the selected category
Activity 3: Shows more info and colors for the selected product
Within activity 3 I would like to have a button for adding the product to a "cart". This would be very rudimentary data: product name, color, image URL. Then on all 3 activities, I wish to implement a button that reveals a fragment containing all items that have been added to cart. When the app closes this list of items should be stored locally and repopulate the fragment when the app is reopened next. However, while the app is still running, is storing locally and reading in the list every time the user changes between activities an acceptable/ efficient solution? Is there a better solution that involves keeping the list in memory/ passing it between activities?

Too many Activity in the stack make the app pretty slow

Recently I created a social app. I didn't use fragment and the project is almost finished. I have several Activities like UserProfile, Followers, Followings activity.
Normally it's just working fine. But if user click UserA UserProfile activity -> and then click A's Followers -> select UserB Userprofile activity -> click B's followers activity -> select UserC Userprofile activity....
In this loop, the app would get pretty slower because it opened too many activities at same time and the back stack hold all of them.
I just wonder if there's any optimization I could do for this situation? Because UserProfile activity layout would always same except the user information content. Is that possible to use Fragment for each activity, even though different activities would show up in sequence one by one?
Thanks!
You should architect this in a different way. You should only ever have one UserProfileActivity in the stack. If you already have the UserProfileActivity for User A in the stack, and you want to show the UserProfileActivity for User B, just call startActivity() for UserProfileActivity with Intent.FLAG_ACTIVITY_REORDER_TO_FRONT and pass some extras to indicate that the Activity should show User B. Use the same concept for all of your activities.
To make sure that the BACK button navigation works correctly, you will need to override onBackPressed() and figure out what Activity needs to be shown and with what data. Then call startActivity() and also set Intent.FLAG_ACTIVITY_REORDER_TO_FRONT and provide extras so the Activity will show the correct data.
To assist in keeping track of where you are in the navigation, you might want to create a stack of items that are stored in a static variable somewhere. Each item would indicate what Activity is being shown and with what data. Every time you launch a new Activity, you push a new item on to this stack, and every time the user presses the BACK key, you pop the top item off the stack and then look at the one underneath it to determine what Activity to start and what data to send in the extras.
With this scheme, the user can click around all day long and you will never have more than one instance of each Activity, but the user will still be able to navigate all the way back.

Restoring instances of activities in a list?

I was wondering if it is possible to save multiple instances of activities in a list as in when a user selects an item in a listView it opens up its corresponding activity that was activated earlier.
To give you a sense of where I am coming from here is the concept of the app I am making.
In Main Activity there is:
A button that when pressed launches a new activity called Exercise
A fragment WorkoutsFragment that keeps track of a list of prior workouts.
In Exercise Activity (whose parent activity is main activity):
Users can input what exercises they are doing in the activity.
When the user goes back to main activity a new item is added to the list in WorkoutFragment and that item contains a summary of the exercises done in the prior Exercise activity. When this item is pressed I want it to go back to the Exercise activity created earlier with all information retained.
So if a user presses the workout item for June 12 I want to to resume the Exercise Activity that was created on June 12.
Would it make more sense to just start a new exercise activity and populate its contents from a database?
You are thinking right. The right way to do it is to create a new Exercise activity and populate its content from database. Maintaining references to activities is not a good practice.
You can also maintain a cache of recent exercises accessed to avoid the database call.
This seems like a good case for using the Activity onRestoreInstanceState.
See this guide on restoring different states of an Activity:
https://developer.android.com/training/basics/activity-lifecycle/recreating.html
You may pass basic data between your Activities through the Intent extras.

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.

Android : getting result from an activity without finish call in that activity

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.

Categories

Resources