How to check input in tab on change tab?


I have an application structure like this:
App structure
Background:
Parent activity # 1 simply holds the tab host, tab widget, and also sets 3 tabs so that their contents are set to 3 activities tab (tab1, tab2, tab3).

Update: I tried calling my validation method inside onTabChangedListener in parent activity # 1, but I have a null pointer exception. I could not trace it anywhere. I did commented / deleted the conflicting code, but still I do not get a bottleneck. StackTrace (link PasteBin). Code for Parent Activity # 1 , Tab # 1 Activity



Problem:
I want to check the data entered by the user in the form field in the individual actions of the onTabChanged event tab , but I can not set more than a single setOnTabChangedListener .
Did I miss something here?
Listeners are set in their actions on the # tab under the oncreate method.

Besides trying to use the above method, I tried onResume() up the listener in onResume() under the main parent activity # 1. But the on Resume() method was never called. I also have a null pointer exception.

The idea behind validation: I want that when the user changes the tabs, the data must be checked before he can skip the tab. So ineffect, I need tab # 1 to check the data in an event like onTabChanged if tab # 2 / tab 3 is selected. Also, this will apply if current tab # 2 is selected and the user selects tab # 1 / tab # 3

Any advice would be appreciated.
Thank you for reading..

0
source share
1 answer

I want to check the data entered by the user in the form field in the individual actions of the onTabChanged tab, but I cannot set more than one setOnTabChangedListener.

There is no need for OnTabChangeListener seconds, and even if you can install it, it will not help you. As you create the code, you need access to child actions. You can do this using one of the answers to this question . The problem is that these answers, with the exception of accepted ones, use outdated methods.

My method, which I suggested in the comments, is to have a static logical field in each of the child actions used as tabs, and to allow all your actions to update this logical flag whenever there is a change in the state of the views in these (if you check CheckBox , type something in 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 have to change it a bit.

I tried to configure the listener in onResume () under the main Parent activity # 1. But the Resume () method has never been called. I also got a null pointer exception.

It's normal that you get a NullPointerException with code, as I have not seen where you initialize the references to the child actions that you use in OnTabChangeListener .

also:

Do not use TabActivity . It is deprecated in favor of the Fragments framework, which is more flexible. These snippets can help you because, I think, you want to stop changing tabs if checking the current page fails, and OnTabChangeListener may be a little late for this (but I may be wrong in what you want).

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

+1
source

Source: https://habr.com/ru/post/1342614/


All Articles