According to your experience, would you handle dialogs on an Activity level, or would let the Fragment itself take care of them.
Let me give you a more concrete example. Say you have a fragment that asks the user for a username and password to log in into your app. would you create an interface with the following methods
onSuccessfulLogIn() // takes the user to another activity
onInvalidCredentials() // shows a toast reading "invalid credentials"
onNoInternetError() // shows a toast reading "Make sure you're
connected to the internet. turn on wifi".
onNetworkError() // shows a toast reading "sorry, there was a problem
with the server, try again"
showWaitingProgressDialog() shows a progress dialog.
or would you just show those toast / dialogs inside the fragment.
Both ways work just fine. I'm just curious about which one is better.
Basically, I'm asking:
Dummy UI fragments and a monolithic activity vs Modular, encapsulated fragments with activities that just handle interactions between them.
Better keep it inside the fragment if not necessary. If u have to pass some data in the methods, then only u should go with interfaces. Moreover, I will recommend using getTargetFragment and setTargetFragment stuff to pass data.
Here is good example for that : https://github.com/alexfu/TargetFragmentExample
Related
In android app, having a few activities with multiple fragments.
Those activities or fragment could be running alive even if it's not on the top of the backStack, and receiving notifications from different services.
When some event happens it is required to show a dialog to communicate with user. The activity or fragment on top of the stack may not have the handler for that event. Any other activity or fragment who is interested should react to open one dialog to the user.
The problem is the listeners in the activities and fragments to handle the events independently don't know if there has been already the same dialog displayed.
What is best way to tell whether the handler should open the dialog or not?
Since this dialog is same for the same event so it may help if could have it as a singleton.
Anyone have suggestion how to make the dialog a singleton for this type of situation?
You can't make Dialog singletone, because Dialogs are linked with current view. And your current view may change - dialog may be sown from different instances of the activities (one or many).
But you can implement a simple singletone class to store all dialog data - save data into onPause and retrieve it in Dialog's onCreateView.
So you will get singleton instance with all data, but Dialogs may vary according to current view.
Also you can store a weak link to the shown dialog in that singletone class. Using such method, you can detect is your dialog currently shown or not.
I am creating a login page which will have forgot password right now:
Login Page is one Activity and Forgot Password page is another Activity.
Each one calls an AsyncTask for http post which returns a json response.
Hence I have callback listener in each `Activity.
In the call back listener I get the json response, which I parse in separate Activity for each Login and separate one for Forgot password
I need to know whether its good practice to do this way or use fragments. Also any other better way to developing it?
I would suggest using a Fragment per page, so one for "Login" and one for "Forgot Password", and one Activity that handles the async http request. You would then be able to use the fragment methods such as fragment.replace() to swap out the screens without each fragment having to have a callback listener. The activity then can also parse the callback listener once instead of twice in each separate activity. Also, the way Android is heading, Fragments are better to use for UI elements than separate Activities as it makes UI updates quicker and smoother then starting a new activity.
There is an Android Developer API Guide loaders available to every Activity and Fragment. Why not check it out. On that same site you can also find guides to use fragments in your activity.
I have a ListView full of elements filling the screen. When I tap one, I want a popup to appear asking for a confirmation (so an Ok and a Cancel button), and then conveniently close the dialog and access the user response so that my main activity can do something with it. I may want to add extra inputs to the dialog later on, and therefore I may also need a more complex form of user response than just ok/cancel.
There are plenty of questions about this here, but they're all focused on a specific way to do it. I haven't even gotten that far yet. There seems to be at least three different ways that it could be done (Dialogs, AlertDialogs, and DialogFragments), and out of those I'm not sure if I'm supposed to subclass them, pass them to the FragmentManager, use a combination of them, or sacrifice a lamb to Poseidon.
I really need a detailed explanation, I tried this code example but I can't get it working the way I want (can't get a custom layout on it - I tried setView but nothing it happening).
I think what you're looking for here is a combination of a customized Dialog with an interface between the current fragment/activity and the dialog it self.
Create a dialog that looks just as you like. (Just as explained at the link above)
Then create an interface and declare methods to be called when things happen in the dialog (like data input and such)
Implement activity as listener of that interface, and when calling the create-dialog method, pass the activity in the parameters.
In the creation method require a Listener of the interface to be passed in the params (your activity)
Call whatever method in the interface whenever inside the dialog like activity.methodOfInterface();
If you implemented correctly methodOfInterface() in activity, the method there will be called.
I'm doing my first app, a RSS Feed application, for learning the multiple technologies associated, like xml, parsing, connecting to the internet, getting the information, processing it, etc.
I've decided to use the newest Android elements, such as the Action Bar and Fragments. So I've done an action bar with a few options, like Refresh (which refreshes the RSS list), Preferences, About and Exit. The main issue is with the Refresh.
I'm pressing Refresh and the option creates an object which will get the XML, which should return the information for the newsList Fragment. But I can't pass the information to the fragment, but I also can't Toast the xml information to the screen, so I cannot test if I'm getting everything correctly.
My programming background is not in Java, I'm used to Web Developing (PHP) and scripting (Shell) so I guess I'm missing some basic stuff, which I apologize in advance.
Can anybody at least give me some hints, in order for me to know what to search and get back in the right path?
Thanks a lot!
So you have the class that handles the downloading/parsing of the RSS feed, but you don't know how to pass the processed feed-info back to the ListFragment right?
There're multiple ways to solve this. You could use the callback pattern for your rss-retrieving class for example. So the Activity starts off this rss-retrieving object asynchronously, and registers itself as a listener of this object. When the rss-stuff is done, the object notifies your Activity about this through the listener-interface.
In response, the Activity can get a reference for the ListFragment (using the FragmentManager) and call a method on it that refreshes the list, passing in the parsed RSS-info as a parameter.
I have a question. Is it possible to display a toast message (in if condition) from HttpDownload class to AnimalBadger class? (Both classes extend Activity)
if (((Node) textNodes.item(i)).getNodeValue().equals("a waning quarter moon"))
{
Toast.makeText(HttpDownload.this, "Some text...", Toast.LENGTH_LONG).show();
}
Thanks for the answers...
The first argument is just to get the Context to create the Toast with. You can use either activity or even getApplicationContext(). For simplicity, you usually use the closest available Context, which in this case would be your containing activity.
Toasts are not sent between application components, they take the form of small notifications usually at the bottom of the screen, and are a way to communicate low-priority messages to the user.
You may want to read the Creating Toast Notifications article in the documentation.
You could use a call back function and register it with HttpDownload class. That way the call back is called which will throw the toast(pun intended).