0


I have an app structure like this:
App Structure
Background:
The Parent activity #1 is just holding the tab host, tab widget and is also setting up the 3 tabs to have their content set to the 3 tab activities (tab1, tab2, tab3 activities).

Update: I tried calling my validation method inside onTabChangedListener in Parent Activity #1 but I got a Null Pointer Exception. Couldn't really trace it to anywhere. I did commented/deleted conflicting code but still I am not getting the bottleneck.StackTrace(PasteBin Link). Code for Parent Activity #1,Tab#1 Activity



Problem:
I want to validate data entered by user in the form field(s) in the individual tab activities onTabChanged event but I am unable to set more than a single setOnTabChangedListener.
Am I missing something here?
The listener(s) are set in their own tab# activities under oncreate method.

Apart from trying the above technique, I had tried setting up the listener in onResume() under the main Parent activity #1. But the on Resume() method was never invoked. I got a null pointer exception too.

Idea behind validation being: I want that while the user is changing tabs, the data should be validated before he can skip over a tab. So, ineffect I would require tab#1 to validate data in a event similar to onTabChanged if tab#2/tab#3 is selected.
Also, this would apply if current tab#2 is selected and user selects tab#1/tab#3

Any advice will be appreciated..
Thanks for reading..

beerBear
  • 969
  • 2
  • 17
  • 41
  • check this thread http://stackoverflow.com/questions/2243360/how-to-use-tabhost-ontabchangelistener-in-android – Ajit Jan 11 '13 at 08:26
  • @Ajit Thanks for replying. The problem I face is actually not in implementing onTabChanged Listener for only a single tab. It crops up when I implement the listener on more than 1 tab. Like in my question above ^ :( Only the `second` listener gets invoked and not the one in the `tab#1 activity`. – beerBear Jan 11 '13 at 08:38
  • I'm not sure I understand what you're trying to do but I guess you could do the validation in each of the tab activities(when the user acts on the fields) and based on this, keep updating a static boolean flag(meaning the state of the fields are in a true/false state) in the activity. Then you can check the status of those boolean fields in the parent's `OnTabChangedListener` and do whatever you want. Or have a look at this http://stackoverflow.com/questions/5399324/how-to-reference-child-activity-from-tabhost-to-call-a-public-function – user Jan 11 '13 at 10:33
  • @Luksprog Thanks for replying. But First: Tell me this that whether the `OnTabChangedListener` should be under the parent activity or the 3 activities which maintain tab(s) content? Which approach is best applicable? Secondly: I tried putting my `OnTabChangedListener` in Parent activity's `onResume` Method but that din't work, instead I got a Null Pointer Exception. – beerBear Jan 11 '13 at 10:47
  • In the parent Activity when you make the tabs(onCreate?). – user Jan 11 '13 at 10:49
  • @Luksprog I tried calling my validation method inside `onTabChangedListener` in Parent Activity #1 but I got a Null Pointer Exception. Couldn't really trace it to anywhere. I did commented/deleted conflicting code but still I am not getting the bottleneck. [StackTrace](http://pastebin.com/Yz1nL07V)(PasteBin Link). Code for [Parent Activity #1](http://pastebin.com/ZQujnYg6),[Tab#1 Activity](http://pastebin.com/e4DGhAq3) – beerBear Jan 11 '13 at 12:42

1 Answers1

1

I want to validate data entered by user in the form field(s) in the individual tab activities onTabChanged event but I am unable to set more than a single setOnTabChangedListener.

There is no need for a second OnTabChangeListener and even if you could set it it wouldn't help you. As you constructed the code you need access to the child activities. You can do this by using one of the answers in this question. The problem is, that those answers, except the accepted one, use deprecated methods.

My method, that I proposed in the comments is to have a static boolean field in each of the child activities used as tabs and let all of your activities update that boolean flag whenever there is a change of state of the views in those activities(if you check a CheckBox, enter something in an EditText etc). Then you can simply check the flag for the desired child activity in the OnTabChangeListener. My method should work but your code is a bit messy so you would have to modify it quite a bit.

I had tried setting up the listener in onResume() under the main Parent activity #1. But the on Resume() method was never invoked. I got a null pointer exception too.

It's normal that you get a NullPointerException with your code as I haven't seen where you initialize the references to the child activities that you use in the OnTabChangeListener.

Also:

Don't use TabActivity. It's been deprecated in favor of the Fragments framework which is more flexible. Those fragments could help you because, I think you want to stop changing the tabs if the validation of the current page fails and the OnTabChangeListener might come a bit late for that(but I may be mistaken about what you want).

As a side note, use equals in your code to test String equality and not ==.

Community
  • 1
  • 1
user
  • 86,916
  • 18
  • 197
  • 190
  • +1 Thanks for summing it up :) But I also tried to set the `onTabChangedListener` inside the `onCreate()` of the `Parent Activity #1` (as in the comment posted by me above ^) and it did got invoked :) the problem being now whenever I changed from tab#1 to tab#2 I get a Null Pointer Exception. :( Oh..I did tried [this](http://stackoverflow.com/a/8368811/1184579) answer from your given link. – beerBear Jan 13 '13 at 07:09
  • 1
    @quantumstates The problem isn't the `OnTabchangedListener` which will get called, the problem with your code is that you have references in your code to the tab activities(`q2cAccess`, `q2c2Access`) which aren't initialized **anywhere** in your code and this references throw the `NullPointerException`. That answer didn't worked meaning, exception, the activity is still null etc? Have you placed any log in there? – user Jan 13 '13 at 11:02
  • +1 Yeah, after carefully analyzing every bit I got the bottleneck. Thanks a lot for bringing it up here anyways :) – beerBear Jan 14 '13 at 07:23
  • 1
    I did a major restructuring of the code (removed absolute methods, segregated code into carefully planned methods now; though I still have to remove massive comment blocks :D) and cleaned it up too. Working absolutely as it should! :) – beerBear Jan 14 '13 at 12:21