I have an app structure like this:
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..
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 ==.
Related
I have a code that requires two edittexts, this information is used to display direction on a map. How can I submit this data automatically on activity start, without the need of a button.
Side question, is there a way to hide these edittexts? To prevent further manipulation by users.
EditTexts can be either disabled using editText.setEnabled(false); or hidden using editText.setVisibility(View.INVISIBLE);.
If you want to execute code on an activity start just write it into the onCreate(...) method.
I am not sure if this is a naive question, I am new to android and couldn't find a answer to this.
I have a SnackbarLayout and I want to hide it so I use the method SnackbarLayout.hide(), now is it necessary to check for SnackbarLayout.isShown() before hiding it?
Does performing this check have significant advantage over not performing this check?
Another scenario is when the SnackbarLayout is in different fragment and hence not shown on screen but I want to hide it so that if user goes back to the fragment it should be hidden, what can I do in this case? I cannot hide based on whether isShown is true or not, it does not work.
Updating what I found:
For the second part to hide if it is visible in other fragment, I used isSnackbarVisible() instead of isShown(). As the name suggests, the former will be true when it is not seen by user but if its set to visble in for some other fragment)
I am following the Google tutorial for building your first android application. I got to the point where I needed to implement the actionbar actions with the functions openSearch() and openSettings().
I implemented all of this in the MainActivity.java file.
My question is this:
In the example app you can type a message and then send it and it displays it in a second activity. In the second activity, the top action bar changes and does not display my Search icon or perform the action when the settings button is clicked. In order to have these icons displayed in the action bar for this activity as well, do I need to add those methods and update onOptionsItemSelected method in DisplayMessageActivity.java as well as in MainActivity.java? Is this the only way to carry the action bar icons/actions over? To retype the same methods in each activity that you want them in? Or is there a better way to do it?
My other somewhat related curiosity is this. The method openSettings() is called when I click the 3 vertical dots and then settings. These 3 vertical dots show up on every activity, and settings is always in the list. However clicking settings obviously doesn't perform the call to openSettings() when in the DisplayMessageActivity and not MainActivity. How is it that settings and the vertical dots are carried over?
Second to last, how can I add other selections to the drop down list from the options/vertical dots in the action bar? Settings is always there although it responds differently in each activity which was my first question. But I would like to add certain things to the options menu that are on all activities, and some things that are unique to some activities. I assume there must be a better way than repeating switch statements and methods in every Activity.java file.
And finally, what is the best practice to implement an action bar over multiple activities?
Obviously different activities will often have different icons/actions in the action bar, however some things like the 3 vertical dots(options) and settings within that would obviously be acceptable to have in every Activity, while it would be nice to add other things to the options list I don't see why settings should ever change across activities. Yet as I stated before the method is not called in DisplayMessageActivity unless I repeat the code in DisplayMessageActivity.java that I had added to MainActivity.java. I'm confused as to where I can add these so that they are displayed on all activities without repeating code. And I'm confused as to how the actionbar's options/vertical dots are carried over to all activities while others require the repeating of code in each activities' java file that I want them to show up in.
I know this was a bit of a long winded quesiton, I will clarify if necessary. I'm just a bit confused. I was able to make it through the tutorial fine as I have a decent understanding of java. However google's guide isn't written that well and the Android environment is very confusing to a beginner.
I do understand how things work to a degree, I just want to ensure that I'm actually doing it in a way that when my app grows in complexity it won't be a mess of unnecessarily repeated statements and methods.
Thanks in advance for any assistance and tips.
In order to have these icons displayed in the action bar for this activity as well, do I need to add those methods and update onOptionsItemSelected method in DisplayMessageActivity.java as well as in MainActivity.java? Is this the only way to carry the action bar icons/actions over? To retype the same methods in each activity that you want them in? Or is there a better way to do it?
That is certainly one solution, but as you obviously know, it's not a very good one. There are at least two alternative solutions:
Create a MenuActivity class which implements all the logic for common menu items and then extend this class from all of your activities, rather than extending the standard Activity class.
Use fragments to implement your UI. Fragments are similar to activities in that they create UI elements from an XML layout. One difference is that they live inside a "host activity". In this particular case, the host activity will provide the common menu functionality and each fragment can customize it further depending on your needs.
How is it that settings and the vertical dots are carried over?
Most likely your DisplayMessageActivity overrides onCreateOptionsMenu() and inflates a menu XML layout which was created by Android Studio (or Eclipse?) when you created the activity class.
When my program start I would like to do some settings before is really starting. Forexample choose the user, check the updates and so on. After these settings I would like to start the main program with the appropriate.
Which is the best way to do this?
You can run an AyncTask, or multiple if you need one for each check, in your onCreate() and show a ProgressDialog while the data is being fetched then cancel it in onPostExecute() and move on to the rest of the MainActivity depending on the data that is downloaded. If you need help getting started with AsyncTask you can see this SO answer on the basic structure.
If you use a ProgressDialog then the app will still start but the users will see something and know that data is loading so they won't feel like it is freezing or taking too long to load (or at least they will know why it isn't loaded right away).
AsyncTask Docs
Edit after comment
For what you said you want in your comment you can do this easily with an Activity that has a Dialog Theme. This will give you the functionality you need (a couple Buttons and store the values) but it will look like a little popup. You can't use an actual Dialog as they need an Activity, the same with any menus, AFAIK. Just create your Activity and make it the launcher and main using the Intent-filters then also add the following line to that Activity's tag in the manifest
android:theme="#android:style/Theme.Dialog"
This approach should give you what you need
There are numerous ways to do that.
First - your app is doing some heavy stuff and this may be freezing user interface. In that version do:
1. Create and activity on what you will override onCreate method and set some content with a spinner - so something will be alive and user will see that something is being done.
2. after you will compute all the things that your app need and may I suggest write it to some global variables override onStart method in what change layout to what suit you and give a user a great UI!
Second - you app is not heavy lifting here just throw everything into override of onStart method.
Handy material here for educating:
I'm looking at some demo code that shows how to use a Fragment Adapter (Tab Adapter in this case). I'm curious as to what exactly the instantiate() method does. I see it used in the following demo code on this page:
http://developer.android.com/reference/android/support/v4/view/ViewPager.html
(see the getItem() method within the TabsAdapter class)
If I'm reading the demo code correctly, every time a user clicks on one of the tabs, a new Fragment is created? And thus the fragment starts the entire life-cycle again (onAttach()...onCreate()... etc)? This sounds awfully inefficient. I would think that the fragment that will represent the content for each tab should be instantiated only once (perhaps in the addTab() method), and then saved into some collection where it can be fetched when getItem() is called.
Please correct me if I'm mistaken in any of this. I'm trying to better understand how to manage fragments.
My money would be on that the setCurrentItem() function doesn't actually destroy the existing Fragment being shown in that tab. Otherwise there's really not much of a reason for the adapter to have a List of available tabs. Likely, when you switch from one tab to another, setCurrentItem() just detaches the UI from the currently active Fragment (or calls its onPause() method) and then re-attaches the UI for the newly selected Fragment (or calls its onResume() method).
But, if you're in doubt, you could read the source :)
Hope it helps,
David
I was able to find an explanation for my question here