How to save the state of my fragment in Android? - 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.

Related

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

Shared Element list changes

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.

Save Fragment at Activities switching

I use PullToRefreshGridView (it's like ScrollView) with infinite scroll in a Fragment. PullToRefreshGridView contains many ImageViews, images downloaded from the Internet. User's click on ImageView starts new activity with the detailed information about image. The problem is when the user press back key in the DetailedImageInfoActivity, PullToRefreshGridView starts reload all images and loses it's scroll position. How I can avoid this?
You should save all activity data you want to reuse when going back in the onSaveInstanceState() method and restore the data when the activity comes back to the foreground in the onCreate() method.

Issues with activity group

I have an app in which I am implementing a tab bar with 3 tabs. In first tab I am implementing multiple activities by using activity group.
In this activity group I have 5 activities. In the first activity I am having edit texts, I am getting the data into edit texts from another class and then navigating to another activity by clicking a button. When I am coming back from second activity to first activty I am not able to see the selected data. It is showing the page without selected data.
I don't understand this.
From the documentation:
This class is deprecated...
And now coming to your question, you said:
... When i am coming back from second activity to first activty i am not
able to see the selected data...
It is default behavior of ActivityGroup, when you move forward, your current Activity's state is not saved and when you come back to previous Activity, it is started again and onCreate() is called again.
its because, in Activity Group Implementation, all the activities are re-created and then generates view, and then adding this view in window activity is displayed. So, everytime a new activity is displayed, as activities are re-created.

Android Tabs problem

I have used 2 tabs in my app.
when i click on first tab it is showing an list view( default) and when i click on any particular item in list view it is taking me to the next activity to display the selected data. Here when i click on tab again i want that i should go to the listview screen which is default screen.
but nothing happens on clicking the tab but when i press the back button i m going back on the screen. but i want to go to home list view screen on tab click,
pls help me in this case...
You say you start a new Activity. This one can not call the first Activity where you have the tabs. Am I right that your new activity just shows the two tabs as well?
What you could do is the following:
In the second Activity react to the onclick of the tab. When clicked call finish() to close the Activity.
I am pretty sure, that you don't need to do the following. But I write it, if you need it for something in the future :).
In the first Activity call the next through startActivityForResult(...)
In the first Activity overwrite the onActivityResult method. there you can switch to the tab with the list.

Categories

Resources