Need some help. I used double click on back button for exit app with Toast with next code:
#Override
public void onBackPressed() {
if (back_pressed + 2000 > System.currentTimeMillis()) {
super.onBackPressed();
} else {
Toast.makeText(getBaseContext(), "Нажмите еще раз для выхода", Toast.LENGTH_SHORT).show();
}
back_pressed = System.currentTimeMillis();
}
Found that fragment code here at stackoverflow. I guess this is the best solution for issue. But there is one ecxeption - toast is still on screen after exiting app. How to kill the toast when user click back button twice and application closed?
We can't kill toast when application closes. We can use toast like below code. Toast.LENGTH_SHORT).show(); less duration of toast when your app closes.
Toast.makeText(this, "Press again to exit.. ", Toast.LENGTH_SHORT).show();
Or, you can set Duration of Toast by code below:
Toast toast = Toast.makeText(getApplicationContext(), "Press again to exit..", Toast.LENGTH_SHORT);
toast.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
toast.cancel();
}
}, 500);
Or, you can use this :
private Toast toast = null;
toast = Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT);
#Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
//Toast.makeText(this, "Press again to exit..", Toast.LENGTH_SHORT).show();
toast.setText("Press again to exit..");
toast.show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
doubleBackToExitPressedOnce=false;
}
}, 2000);
}
#Override
protected void onStop () {
super.onStop();
toast.cancel();
}
You should be able to reach your goal by Using an helper class. In my applications I call this ToastManager.
In this class, you will manage every toast of your application and you can decide whether to dismiss or show them.
I did it to avoid the Toast queue, I'm dismissing the Toast when another one is shown.
You can implement it like below and call the dismissToast method in your custom application or Activity.
public class ToastManager {
private static Toast m_currentToast;
public static void showToast(Context ctx, String text)
{
try {
if (m_currentToast != null) {
m_currentToast.cancel();
}
m_currentToast = Toast.makeText(ctx, text, Toast.LENGTH_LONG);
m_currentToast.show();
}catch(Exception ex){
ex.printStackTrace();
}
}
public static void dismissToast(){
if(m_currentToast != null){
m_currentToast.cancel();
}
}
}
Obv you will have to create each tost by calling the showToast method.
hope this helps
Next code erase toast immediately after closing application:
#Override
protected void onStop () {
toast.cancel();
super.onStop();
}
I guess this is enough for my app, it's looking great.
Tnx Abhishek kumar )
Related
I'm using Toast to show some informations to the user, because I want to show the newest message without delay regardless of the previous messages, I do it like this (learned from old projects):
public class MainActivity extends Activity {
private Toast mToast;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mToast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
}
private void toast(final String message) {
runOnUiThread(new Runnable() {
#Override
public void run() {
mToast.setText(message);
mToast.show();
}
});
}
}
That is, the single Toast object is reused and showed multiple times, whenever I need show a new message, I just setText and show it again. It seems working fine, but after I did some searching on Google, I found most people will do it like this:
private void toast(final String message) {
runOnUiThread(new Runnable() {
#Override
public void run() {
mToast.cancel();
mToast = Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT);
mToast.show();
}
});
}
Which cancel the previous Toast then make a new one by Toast.makeText.
Are there any differences? Which one should I prefer?
You can cache current Toast in Activity's variable, and then cancel it just before showing next toast. Here is an example:
Toast m_currentToast;
void showToast(String text)
{
if(m_currentToast != null)
{
m_currentToast.cancel();
}
m_currentToast = Toast.makeText(this, text, Toast.LENGTH_LONG);
m_currentToast.show();
}
Another way to instantly update Toast message:
void showToast(String text)
{
if(m_currentToast == null)
{
m_currentToast = Toast.makeText(this, text, Toast.LENGTH_LONG);
}
m_currentToast.setText(text);
m_currentToast.setDuration(Toast.LENGTH_LONG);
m_currentToast.show();
}
Reference : How to immediately replace the current toast with a second one without waiting for the current one to finish?
You should try code something like this to avoid visibility of multiple toast at a time.
private void toast(final String message) {
try{ mToast.getView().isShown(); // true if visible
mToast.setText(message);
} catch (Exception e) { // invisible if exception
mToast = Toast.makeText(theContext, message, toastDuration);
}
mToast.show(); //finally display it
}
code help me is here
I have a service which is working in background.
How can I show Toast text message from Service that does not have UI?
No Activity,This is none-UI service.
Is it possible?
You can certainly do that. A service is a Context. So you can call
Toast.makeText(this, "My Information", Toast.LENGTH_SHORT).show();
Try this in your background thread.
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
Toast.makeText(context, "Running background", Toast.LENGTH_SHORT).show();
}
});
I have been showing it from dummy classes by using the application class instance, basically what this means that you can show it anywhere within you application (includes service).
public class DemosApplication extends Application {
private static DemosApplication instance;
private Toast toast;
public static DemosApplication getInstance() {
return instance;
}
#Override
public void onCreate() {
super.onCreate();
if (instance == null) {
instance = this;
}
}
public void showToast(int resID) {
showToast(getString(resID));
}
public void showToast(String text) {
if (toast != null) {
toast.cancel();
}
toast = Toast.makeText(this, text, Toast.LENGTH_SHORT);
toast.show();
}
}
Usage from anywhere
DemosApplication.getInstance().showToast("Lalalala");
I have the following code:
protected MyCallback getMyCallbackHandler() {
return new MyCallback() {
#Override
public void onReady() {
// DO STUFF
if (stuff!= null) {
mThread = new MyThread(stuff,MyActivity.this);
mThread.execute(mSensors.getLocation());
}
else {
Toast.makeText(MyActivity.this, "No target detected! Try again later.", Toast.LENGTH_LONG).show();
mSensors.registerCallback(null);
}
runOnUiThread(new Runnable(){
#Override
public void run() {
comp.setVisibility(View.VISIBLE);
}
});
}
};
}
The problem is that if stuff==null when Toast.makeText(...).show()is called, first the toast message doesn't appear and second the next lines seems to be not executed.
If I comment just the Toastline, everything run as expected.
I tried to put as context MyActivity.this, getParent().getApplicationContext()and also getApplicationContext()but it doesn't work.
you can't use Toast here you should use Log.d() , if you really want to debug the applications start playing with Logcat !
if you want to use Toast inside this pass context to getMyCallbackHandler(Context c)
and use that in your Toast like
else {
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(c, "No target detected! Try again later.",
Toast.LENGTH_LONG).show();
}
});
mSensors.registerCallback(null);
}
I think you have to make the call to Toast on the main UI thread.
I created this class but I am not able to get pop up message which should be generated after success in storing in stackmob.
public class TaskActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
csuper.onCreate(savedInstanceState);
//setContentView(R.layout.TaskActivity;
setContentView(R.layout.activity_task);
StackMobAndroid.init(getApplicationContext(), 0, "010686ac-9fb2-4c70-bbec-c2d92ccdc39d");
Task myTask = new Task("Learn even more about StackMob", new Date(0));
myTask.save(new StackMobModelCallback() {
#Override
public void success() {
// the call succeeded
// Toast.makeText(this, "No camera on this device",0).show();
Toast msg = Toast.makeText(TaskActivity.this,
"i have done it", Toast.LENGTH_LONG);
msg.show();
}
#Override
public void failure(StackMobException e) {
// the call failed
}
});
}
Do you know if the success function will be called on the UI thread?
If not, you should change it like this:
public void success() {
// the call succeeded
TaskActivity.this.runOnUiThread(new Runnable() {
#Override
void run() {
Toast msg = Toast.makeText(TaskActivity.this,
"i have done it", Toast.LENGTH_LONG);
msg.show();
});
}
See runOnUiThread
I have an IntentService that downloads some files. The problem is that I create a Toast inside the IntentService like this
Toast.makeText(getApplicationContext(), "some message", Toast.LENGTH_SHORT).show();
The Toast will never disappear event if I exit the app. The only way to destroy it is to kill the process.
What am I doing wrong?
The problem is that IntentService is not running on the main application thread. you need to obtain a Handler for the main thread (in onCreate()) and post the Toast to it as a Runnable.
the following code should do the trick:
#Override
public void onCreate() {
super.onCreate();
mHandler = new Handler();
}
#Override
protected void onHandleIntent(Intent intent) {
mHandler.post(new Runnable() {
#Override
public void run() {
Toast.makeText(MyIntentService.this, "Hello Toast!", Toast.LENGTH_LONG).show();
}
});
}
This works for me:
public void ShowToastInIntentService(final String sText) {
final Context MyContext = this;
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
Toast toast1 = Toast.makeText(MyContext, sText, Toast.LENGTH_LONG);
toast1.show();
}
});
};
IntentService will create a thread to handle the new intent, and terminated it immediately once the task has done. So, the Toast will be out of controlled by a dead thread.
You should see some exceptions in the console when the toast showing on the screen.
For people developing in Xamarin studio, this is how its done there:
Handler handler = new Handler ();
handler.Post (() => {
Toast.MakeText (_Context, "Your text here.", ToastLength.Short).Show ();
});
To show a toast when the user is in one of the application activity.
Just need a reference of the current activity, and call it with this sample code:
public void showToast(final String msg) {
final Activity a = currentActivity;
if (a != null ) {
a.runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(a, msg, Toast.LENGTH_SHORT).show();
}
});
}
}
There is a lot of options to get the current activity, check this question:
How to get current foreground activity context in android?
But I use this approach:
The application must have:
private Activity currentActivity = null;
public Activity getCurrentActivity() {
return currentActivity;
}
public void setCurrentActivity(Activity mCurrentActivity) {
this.currentActivity = mCurrentActivity;
}
Each activity must have:
#Override
protected void onResume() {
super.onResume();
((MyApplication) getApplication()).setCurrentActivity(this);
}
#Override
protected void onPause() {
super.onPause();
((MyApplication) getApplication()).setCurrentActivity(null);
}
You shouldn't create Toasts from a Service. You should use a Notification instead.