how to maintain focus between two screen component? - android

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().

Related

Prevent last Fragment from being removed from backstack

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.

Android Two layout for one activity

I have a form, and I have chosen to divide it in two steps.
To do it, I created two layouts for a same activity. When the user complete the first form, I call the second layout with :
setContentView(R.layout.activity_form2);
The problem is, if the user wants to come back at the first step of the form, it's not running, because he comes back to the previous activity.
Is it correct to do that, or I need to use fragment?
Otherwise, how can I do to come back on the previous layout, and not the previous activity?
Never set different layout's for the same Activity. You can navigate to a different Activity or you could use Fragments.
Layout is set to the Activity and when you click back button Activity is popped from the back stack and previous Activity in the stack takes focus. So setting different layouts for the same Activity is not a good choice.

OnSaveInstanceState call manually in android

I have a question. This is possible to call onSavedInstanceState() and onRestoreInstanceState() manually. I need that because I want to recreate all Activity but I want to save last all views. This is schema: I have in activity three toggle buttons, I check all buttons and click on button refresh. Now I want to recreate all views and save all states my buttons and other layouts. Something like onConfigChange() but without changing orientation.

Save screen position when changing to different activities

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.

How to refresh listview by hitting back button - Android

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.

Categories

Resources