Android overriding onBackPress unexpected behaviour - android

Yet another problem whilst trying to create the UI for an app.
I've got a Viewflipper in a tab which slides from View 1 to View 2 to View 3. I've overridden the onBackPress method to navigate back to a previous view if I am not on View 1.
It works fine when on View 2, pressing back goes to View 1.
But when on View 3, pressing back ends the Tab activity and takes me back to the sign in activity.
Here's a video in case my description was hard to understand: http://www.youtube.com/watch?v=2nKhgpq3rQA
Has anyone come across anything similar?
Why does the onBackPress get ignored in my Tab Activity when View 3 is displayed? I've debugged and put a breakpoint on the onBackPressed method in my Tab Activity, the breakpoint is hit in all cases except for when View 3 is the shown view. Werid.
Thanks for your time.
Update: I've discovered that if I add another ListView as the View 3 it works, but if I add a TextView as View 3 it doesn't behave as expected, crazy.

just don't call super.onBackPressed(); then you can handle every back click your self.
calling super.onBackPressed(); will get to the activity called before.

I solved this, there was some weird behaviour because I was adding the first view in my view flipper to the tab content, rather than adding the viewflipper itself.

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.

switching between fragments delete the views in first fragment

I have in my app a drawer navigation, which contain few fragments to go to. when my app starts it opens by default the first fragment - which contains a view pager that has 2 fragments in it as well. in those 2 fragments I have a text view and a button as well.
the problem is that if I click on another fragment in the drawer, and then go back to the first fragment, the button and the text view are gone. in the first time this view pager fragment is creating the two other fragments. I think that when I go back to this fragment again by replacing it, it wont load the other two fragments and their text and button view. I'm going crazy because of this and I cant continue my work..what is wrong here?
I don't know how to upload code snippets..sorry
Thank you for your help :)

onTabReselected for actionbar tab getting called when unwanted

I followed instruction for actionbar Tab from here
I'd like to user to be able to scroll back to top of list-view when they reselect a tab.
I put listView.setSelection(0) inside onTabReselected(Tab,FragmentTransaction) method.
It works as intended when I re-click the tab already chosen.
But the function is also called when I didn't expect it to be.
I start a new activity.
When I come back to the original activity(with the tab), the onTabReselected function is called.
How can I differentiate the two cases so that I don't scroll the list when coming back from another activity?
The unexpected call to onTabReselected was due to my setSelectedNavigationItem call in onResume.
problem solved.

View pager with fragments - showing dialog onStart

I'm new in using view pager, I've done everything correctly, I conveted my activities into fragments and put them in FragmentPagerStateAdapter.
When testing it in the emulator, what I noticed is that the onCtreate to the onResume are called one fragment before the actual visibility on the view pager - it makes sense, the device wants to get ready for the next step, but the problem is that I have one fragment which calls a dialog every onStart so that actually happens on the wrong fragment.
What should be done?
You can use an OnPageChangeListener on your ViewPager to detect when you navigate to the right fragment.

Fragment custom view addToBackStack(), custom view, and back button

Problem:
I have a custom Calendar view that I generated using canvas drawing and stuff. This is the layout for Fragment A.
I have another Fragment B which when the user does something in Fragment A is instantiated and replaces Fragment A via getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container), fragmentB).addToBackStack().commit();
works fine up to this point (Fragment B is shown).
Now when I press the back button, I expect Fragment B to exit or be removed from the screen, to be replaced by the previous fragment A.
still works correctly - however the now displayed Calendar view is all messed up and sh*t, all the drawings are stacked up on the left side of the screen and does not look like a calendar at all.
Why is this happening, and how should I go about this?
i found the culprit - the cell width measurement inside my custom canvas is being set to zero for some particular reason - inside onMeasure. i moved the assignment statement inside onDraw and it worked perfectly.

Categories

Resources