I have a ListView that is dynamic and will constantly change. The user selects an item and goes to a different activity.
What I want: When they hit back button and return to the ListView, I would like to call a certain action to refresh ListView (in my case an AsyncTask).
I have figured out one way to do this: By adding my refreshing code in onResume. But I find it refreshes a little too much -- I only want it to refresh when coming from the forward activities.
I have figured out one way to do this: By adding my refreshing code in onResume. But I find it refreshes a little too much -- I only want it to refresh when coming from the forward activities.
put your "refresh" code inside of onStart() instead of onResume(). onStart() gets "Called when the activity is becoming visible to the user." - Activity Lifecycle
Which means that it will happen only at the time that you activity gets put onto the screen.
Related
I am currently writing a drawer layout as my main layout, with an embedded FrameLayout that I will be using to hold each “page” when an item on the drawer is clicked on. When the app first starts, an initial fragment will be show. Other fragments may be added/replaced later which is fine, however, my problem is that when the user clicks the back button on the very first “initial fragment”, I don’t want that particular fragment to be removed from the layout. Currently, it is being removed and it’s just showing a drawer layout with no other content (which makes sense). I want the app to automatically exit if the initial fragment was the last one showing and the back button is pressed, instead of removing that initial fragment and then after another back press, then it exits.
Things I have thought of doing:
Not adding the first fragment to the backstack. (If I do this, I can compare it with the classname of the fragment which is a somewhat longer string, or I can use a boolean value for once the first fragment has been placed (and not added to backstack), the boolean is set which allows the fragments to now be added.
Overriding the onBackPressed function of the activity
Does anyone have a suggested way of doing this or can think of a better way? Thanks
The first bullet point sounds the cleanest. You have no other need to handle conditions when back is hit, correct? If that's the case, it's less lines of code (removing one as opposed to adding several) and you get to keep default Activity methods as is.
I know that's not exactly what you asked, but I think the first bullet point is so clean, that I just wouldn't try something else.
I have implemented same in one of the app using my own Stack of fragment. and also implemented onBackPressed method.
Every time when user clicks on item in drawer i add fragment in stack and in back press once its length is 1 I finish the activity with message.
On item click -- Add/replace fragment in container.
OnBackPressed -- Pop fragments from stack and once its last one i finish activity.
Hope this can give you another option to consider.
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.
In my two screen app first screen has some button, from here control jump to other activity. Now when control return to first activity from second, how can focus for last action item?
Also please suggest how can do same thing while communicating between two fragment?
Have you tried calling requestFocus() on the control in your first activity's onResume() method?
If you need to remember which control was tapped (and therefore which one should have focus), you can save a reference to in whatever code starts the second activity (such as that control's OnClickListener), and use that information in onResume().
I have a relativelayout with 30 buttons within a scrollview. What I would like to know is if there is a way of saving the position, ie. when the user has scrolled down to the second last button and clicked on it to display an image or text, and when pressed back to select a different button to return to the last position (which was the second last button for example) without having to scroll all the way down again? I had a sliding drawer and it worked fine, but with the buttons (which are image buttons) it takes a while to load the screen with all the buttons. And I found doing it without the sliding drawer it loads faster but now have to scroll all the way down to the button every time I return to the buttons.
Android docs say here:
[...] you should use the onPause() method to write any
persistent data (such as user edits) to storage. In addition, the
method onSaveInstanceState(Bundle) is called before placing the
activity in such a background state, allowing you to save away any
dynamic instance state in your activity into the given Bundle, to be
later received in onCreate(Bundle) if the activity needs to be
re-created.
Perhaps you just recreating your activity content by accident.
I am having an issue here that is driving me nuts. What I have is the following:
FragmentActivity1 holds the viewPager. It instantiates the FragmentAdapter and two fragments and attach them to the ViewPager.
Fragment1 has only one button. When user clicks this button, I create one Intent and invoke a FragmentActivty. This fragmentActivity contains a form, which the user fills in and press OK. When he does that, I persist the data in the DB of the app.
Fragment2 is a ListFragment, and lists all the data that was inserted previously by the user.
By the time user completes the form and presses OK, I persist the data, like I said and finish() the activity, returning to Fragment1. WHen I swype to Fragment2, the data is not there. I need then to swype to Frag1 and then back to Frag2. Only then I can see the problem.
I have tried setting listeners between activities and fragments but, still, cant make this work.
Have anyone seen? I am willing to share my small project as well, so you guys can take a look.
Thanks,
Felipe
I usually fill my ListView in onResume() in the ListFragment. When I'm done adding new stuff to the list (In a seperate FragmentActivity, just like you), the ListView automatically gets refreshed, because onResume() gets called ;)
Another approach is using onActivityResult(), then you can update list only when something new is added or something is removed