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).
Related
Toast toast = Toast.makeText(this, "Toast!!!", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.AXIS_PULL_AFTER , 0, 0); toast.show();
what is the role of Gravity.AXIS_PULL_AFTER argument in order to positiong the Toast on the UI.
As per Android documentation:
Make a standard toast that just contains a text view.
Context: The context to use. Usually your Application or Activity
object.
Generally, this will be a reference to the activity calling Toast. However, if you are inside an anonymous class (for example, creating a click listener for a button), you will lose reference to your activity.
The parameter this is the object which tells the Toast where to show it, in your case is this your Activity which extends a Context. Note that this is used a lot around Android widgets. If you are using this inside an anonymous class e.g. an onClickListener(), use YourActivityName.this instead.
There is no clear specification for it according to Google documentation.
Its just a parameter that tells the current activity or application where makeToast function has to show Toast message. current activity or application
I just learned that I can use either:
Toast.makeText(MainActivity.this, R.string.some_string,Toast.LENGTH_SHORT).show();
or,
Toast.makeText(getApplicationContext(), R.string.some_string,Toast.LENGTH_SHORT).show();
To display a Toast in Android.
Earlier I thought context was actually a sort of handle to the parent window where it should be display but the documentation is unclear about this.
Then I came across this table:
It also doesn't seem to mention what context exactly to use for a Toast?
Edit:
Is context like a "handle to parent window" for the sub-window like Toast? or does it actually allow the Toast.makeText to get access to resources or something?
Why is it being used at all if the context doesn't matter?
Looking at Toast.java, I can see the Context is used only for:
obtaining Resources
obtaining Package Name
getText which is actually same as #1
So apparently there's no difference whether it's Activity or ApplicationContext, unless those resources depend on theme (which is not the case as far as I can tell).
And no, the context passed to Toast is not a handle to parent window in any sense.
I'd recommend to use the activity in your case. Since you're calling from the activity itself. The activity is a Context and you're using the method on the activity to get another context (the application). It is a little unnecessary.
However in the case you're calling a toast from somewhere else it might be a better idea to use the application, since the application will always be there while your app is active.
You can show Toast only from UI (main thread) context. If you want show this from Service (but this is contradicts Google guidelines), you can do this way: Show toast at current Activity from service
For toasts, which are short-lived, you can usually use whatever context you want. Typically, you would use the activity context, but application context is fine as well.
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
First of, sorry for the bad title, I couldn't think at all what to call this.
Say if I have a string with a value of "ActivityMain", and I have a Activity in my project called ActivityMain. Is there anyway I can get a new instance of the class by the string?
The overall idea is to request data from a server, what sends back some different Activity classes, then I want to start whatever activity is returned.
Assuming you can get the fully qualified name for the activity's class, you could follow this answer and use:
Class<?> act = Class.forName("com.bla.TestActivity");
I think if else if ladder may do what you want.
if(responsefromsever.equals(NameOfActivityInString)){
// instantiate the activity
}
use case:
I am assuming there exist a Activity class whose name is MainActivity. And what you receive from server is response, (i.e String)
String nameOfActivity = "MainActivity";
if(response.equals(nameOfActivity)){
MainActivity instantiation or whatever you want to do
}else if(response.equals("SomeOtherActivity")){
//SomeOtherActivity or whatever you want to do
}
The overall idea is to request data from a server, what sends back some different Activity classes, then I want to start whatever activity is returned.
A simple way hat is coming to me is that you can have some switch condition or if-then-else conditions which would compare the string received and accordingly start the desired activity. Eg:
if ( stringReceived.equals("ActivityMain"){
//start ActivityMain
} else{
//others...
}
This might be useful, if there are not many activities to start.
It seems like you are sharing implementation details to your server of your client program. What if you want to hook up the server to an iOS app? Then the whole activity name returning idea wouldn't work. "Drawing a different line" for your interfaces might be the best option.
I have a Fragment in which I use an AsyncTask to send a tweet. I send the tweet in doInBackground, and when it's sent, onPostExecute is called and I'd like to show a Toast (or any notification) to the user.
The issue is that if the Fragment is paused (e.g. the user changed screens during the twitter request), no more context is available to call
Toast.makeText(Context context, CharSequence text, int duration)
I'm aware of other similar questions, like this one, but I couldn't find any answer...
... and keeping a local copy of the context seems very wrong.
Any suggestion?
As per earlier comment:
You can extend the Application class and (since it's already a singleton) set it up with a singleton accessor. Toasting on the application context will work just fine. If you feel like something more fancy, you can always set up your own singleton which keeps a reference to the application context for toasting messages. See also here.