Shared Element list changes - android

Activity A contains a list of images. Activity B contains the selected image. Using shared elements it successfully animates the image being moved to the new activity and back again when I finish B.
The problem is when the list is changed and I am on B, the image will try to go back to the same spot in the list. If it was the 10th item in the list when I selected it, then when I press back there are only 3 images in the list now it will throw an exception as that 10th list item container won't exist anymore. If I select the 1st list image, but whilst on B the list grows and my item is forced to the 5th spot in the list, when I press back it will still go back to that 1st list item it came from.
Is there a way I can tell it to move to a different list item on its return transition? (Activity B is informed when the list changes, so I do have the option to get it's new position)
Any suggestions would be great guys.
Thanks

It seems you should only refresh your list when your ActivityA is ready.
Maybe you can use startActivityForResult() in ActivityA to start ActivityB, though I'm unsure about the exactly life-cycle callbacks of ActivityA when ActivityB returns.
Maybe you should look into SharedElementCallback and refresh your list when the transition has completed.

Related

Page fixed at a specific post in recyclerview

I have an activity A. From that activity I go to activity B. Now there are some image descriptions in A and the corresponding images are in B. I want to make a system where based on the descriptions from activity A, the screen gets fixed to the post with the specific image among all the posts when I move from A to B. In other words I want to fix recyclerview to a specific child when the activity B opens.
P.S.- I have the postkeys of all the posts in recyclerview in B in my database.
Thanks in advance.
find the row position associated to your postKey from your activity B recyclerViewAdapter then auto scroll to that postion
recyclerView.smoothScrollToPosition(rowPosition); //if you want a smooth scroll
or
recyclerview.scrollToPosition(rowPosition);

Recycler view Scrolling issue

I am doing a program where I have recyclerview with different options(Options 1,2,3,4,5) that are loaded from web and then displayed in my first activity and when I click on one of the options app goes to second activity.I declared first activity as parent of second activity. So that I can go back from second activity onto first activity by clicking back arrow.
My problem is that when i first open my app recyclerview is displayed correctly
(Options 1,2,3,4,5). But if I click on one of the options and go to second activity and come back to first activity recyclerview is shown one below the other i.e. twice. (Options 1,2,3,4,5, 1,2,3,4,5)
Same thing also happens if I change orientation of my device while on my recyclerview activity.
Thanks

How to return different shared element from first shared element when start a transition activity

I have an activity that contain a listview.
My listview has many pictures.
When user touch a picture, second activity start with shared element transition.
Second activity contains viewerpager that shows pictures in first activity.
I want when user press back after change current picture, return transition back to current picture item with animation.
By default sample of shared element transition,when user press back return transition animation back to first item that user touch it.
try use listView.setSelection(position) in onResume method
Or for smooth scroll listView.smoothScrollToPosition(position);
Check this sample project: https://github.com/alexjlockwood/activity-transitions
The key point: you have to tell activity 1 a new position of shared element.

How to save the state of my fragment in Android?

I have a simple app, in the home screen it has 4 tabs. Each tab is a fragment. In one of the fragments, I have a ListView that displays some songs. Nothing is stored locally, I populate the ListView with network calls (to the iTunes API, to my own server). If I click on a list item (a song), it starts a new activity that searches Youtube for that song. When I click back, it's as if the previous activity is started from scratch. How do I retain the scroll position of the ListView ?, It also goes back to the first fragment (the leftmost one in the viewpager). How do I make it return back to the fragment that the user clicked the song in?
You can try overriding the onBackPressed method in the called Activity and call finish() in that.

Android Gridview is not refreshed while pressing back button

I have one grid view in one activity.I have set Adapter using array-list.
Gridview is displaying the list of the items stored in the database.Let say it is some products.
When i click on any product in the grid-view it will navigate me to product detail screen.
From product detail screen i can delete product.If i delete product and press back button it will navigate me to product list screen.
Expected: Product list screen will come without deleted item.
Actual: Product list screen is coming with deleted item. When touch this item it will do nothing.
Supporting my question,please see below picture
You have get gridview items from db in OnResume method of activity. And when u delete any item and then back press, it will call OnResume method of previous activity where u get new list, and call notifyDataSetChanged() on the grid, in order to recreate the view
Try Some like:
if (homeGridViewDatas.size() > 0) {
homeGridViewAdapter = new HomeGridViewAdapter(activity, context,
homeGridViewDatas);
gvHome.setAdapter(homeGridViewAdapter);
homeGridViewAdapter.notifyDataSetChanged();
}
You should notify the gridadapter which something is changed by callingnotifyDataSetChanged on it. In onResumeshould be ok. Or when you update it too.
When i click on any product in the grid-view it will navigate me to
product detail screen.
When you go from gridView activity (product list screen) to detail activity(product detail screen.), the detail activity is launched on top of your gridView activity in Stack.
If i delete product and press back button it will navigate me to
product list screen.
And when you delete a particular product in detail screen and press back button, the detail activity is removed from the stack and hence the gridview activity which was below detail activity comes to foreground and becomes visible.
Product list screen is coming with deleted item. When touch this item
it will do nothing.
However your gridView activity still has the previous data.Hence it displays that old data and nothing happens on touch as actual data is different than the one in adapter.
Solution :
You need to notify the adapter for gridView in that activity, that the data has been changed and the view must be updated accordingly.
As onResume() is called everytime activity comes to foreground and is visible, it is the better place to call notifyDataSetChanged() on your gridadapter.

Categories

Resources