I was doing an application which uses some Toasts.
If a Toast appears and in the meanwhile I quit the app, it normally doesen't disappear.
Is there a way to stop the Toast if I quit the app on my phone?
In Activity onStop or ondestroy use cancel() method
public class MainActivity extends Activity {
private Toast toast = null;
#SuppressLint("ShowToast")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toast = Toast.makeText(getApplicationContext(), "", Toast.LENGTH_LONG);
showMyToast();
}
public void showMyToast()
toast.setText(" test toast");
toast.show();
}
#Override
protected void onStop () {
super.onStop();
toast.cancel();
}
while quitting from add need to write this
toast.cancel();
Toast.makeText returns a Toast object. Call cancel() on this object to cancel it.
Toast toast = Toast.makeText(this, "Hello..", Toast.LENGTH_LONG);
toast.cancel();
use android activity life-cycle and override onstop() or onPause mthodes
onPause() called when activity is not visible to the user.
onStop() called when activity is no longer visible to the user.
#Override
protected void onPause () {
super.onPause();
toast.cancel();
}
or
#Override
protected void onStop () {
super.onStop();
toast.cancel();
}
Related
When I run this app I am getting "On Resume" first instead of "On start" even "On create" not showing up, please tell me why? and "On Restart" toast is not displaying but test is getting updated.
public class MainActivity extends AppCompatActivity {
public int test=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast t=Toast.makeText(getApplicationContext(),"On create",Toast.LENGTH_SHORT);
t.setGravity(Gravity.CENTER_VERTICAL,20,20);
t.show();
}
#Override
protected void onRestart() {
super.onRestart();
Toast t=Toast.makeText(getApplicationContext(),"On restart",Toast.LENGTH_LONG);
t.setGravity(Gravity.CENTER_VERTICAL,20,20);
TextView num=(TextView) findViewById(R.id.textNum);
test++;
num.setText(String.valueOf(test));
t.show();
}
#Override
protected void onStart() {
super.onStart();
Toast.makeText(getApplicationContext(),"On start",Toast.LENGTH_SHORT).show();
}
#Override
protected void onResume() {
super.onResume();
Toast t=Toast.makeText(getApplicationContext(),"On resume",Toast.LENGTH_SHORT);
t.setGravity(Gravity.CENTER_VERTICAL,10,20);
t.show();
}
#Override
protected void onPause() {
super.onPause();
Toast t= Toast.makeText(getApplicationContext(),"On Pause",Toast.LENGTH_SHORT);
t.setGravity(Gravity.CENTER_VERTICAL,20,20);
t.show();
}
#Override
protected void onStop() {
super.onStop();
Toast.makeText(getApplicationContext(),"On Stop",Toast.LENGTH_SHORT).show();
}
#Override
protected void onDestroy() {
super.onDestroy();
Toast.makeText(getApplicationContext(),"On destroy",Toast.LENGTH_SHORT).show();
}
}
Using Toast as a debugging method is really bad idea. Use logging and look at your logcat. You will see that the methods get called in the order that they should. Just don't do this.
If the user clicks on the Home button for example, the methods onPause() and onStop() are called.
I want to call onDestroy() from the onStop() method after 1mn, unless the user goes back on the app (which calls onResume() and onStart() methods).
I tried to implement Timer:
It fails, saying it cannot call the onDestroy if Looper not implemented.
When I implement the Looper, the onDestroy() method is never called.
Maybe calling onDestroy() from onStop() is not the good thing to do, and another "clean" solution exists to get the behavior I want. I just want to kill the app after 1mn no use.
In this case, please propose.
If my wish is the good way to proceed, could you share how to implement it ?
dont call onDestroy() directly , instead call finish() after the period you want
and to support the scenario you mentioned make sure not to kill the activity if the user resumed the activity
here's a piece of code i wrote for you .
the activity will kill its self if not resumed in 1 second ;
boolean notResumed;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startActivity(new Intent(this,Main2Activity.class));
}
#Override
protected void onResume() {
super.onResume();
notResumed=false;
}
#Override
protected void onStop() {
super.onStop();
notResumed=true;
Handler handler=new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
if(notResumed)
finish();
}
},1000);
}
#Override
protected void onDestroy() {
super.onDestroy();
Log.d("debug","onDestroyCalled");
}
This answer is largely inspired from Abdelrahman's post above.
I just adapted few things to reinitialize the delay counter each time I go out of my app.
boolean notResumed;
//Declare my Handler in global to be used also in onResume() method
Handler myHandler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startActivity(new Intent(this,Main2Activity.class));
}
#Override
protected void onResume() {
super.onResume();
notResumed=false;
//Remove callbacks on the handler if it already exists
if (myHandler != null) {
//I send null here to remove all callbacks, all messages,
//and remove also the reference of the runnable
myHandler.removeCallbacks(null);
}
}
#Override
protected void onStop() {
super.onStop();
notResumed=true;
myHandler=new Handler();
myHandler.postDelayed(new Runnable() {
#Override
public void run() {
if(notResumed)
finish();
}
},10000);
}
#Override
protected void onDestroy() {
super.onDestroy();
Log.d("debug","onDestroyCalled");
}
Thanks a lot again to Abdelrahman Nazeer for its fast and accurate answer.
Please comment if something is not correctly done here. At least it works as expected...
I want to kill my Activity process when I pause it by answering a call or something like that
but when i try to start my app it closes instantly. Any solutions? Sample code below
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Things to do
}
//#Override
public void onPause() {
super.onPause();
android.os.Process.killProcess(android.os.Process.myPid());
}
You should implement a logical scenario based on activity's lifecycle methods.
update your code with
#Override
public void onPause() {
super.onPause();
this.finish();
}
I created one login screen but before login screen to appear I wanted a image to flash on the screen. For this I use Toast.
But the problem is before flashing the Image Login screen appears for a while and the image flashes after that again Login Screen appears. I wants to flash the image first before any thing appears in the screen. Here is my code :
setContentView(R.layout.main);
ImageView iv = new ImageView(this);
iv.setImageDrawable(getResources().getDrawable(R.drawable.start));
Toast t = new Toast(this);
t.setView(iv);
t.show();
t.setDuration(5);
Thanks
Deepak
You Need to use Handler Class To Hold the present LoginWindow for few seconds , Handler Class Provides A method which can be used display image before the screens is displayed,
if is not possible with Handler method then please make use of Activity LifeCycle Methods like OnStart() etc there are lot activity methods u can use
Here is some useful code to u..
private Handler handler;
private final static String DEBUG_TAG = "splashScreen";
public void onCreate(Bundle savedInstanceState) {
Log.i(DEBUG_TAG, "onCreate executes ...");
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscr);
handler = new Handler();
}
public void onResume()
{ Log.i(DEBUG_TAG, "onResume executes ...");
handler.postDelayed(new Runnable()
{
public void run()
{
Intent myIntent= new Intent(SplashScreen.this,TabCls.class);
startActivity(myIntent);
}
}, 1000);
super.onResume();
}
protected void onStart()
{
super.onStart();
Log.i(DEBUG_TAG, "onStart executes ...");
}
protected void onRestart()
{
super.onRestart();
Log.i(DEBUG_TAG, "onRestart executes ...");
}
protected void onPause()
{
super.onPause();
Log.i(DEBUG_TAG, "onPause executes ...");
}
protected void onStop()
{
super.onStop();
Log.i(DEBUG_TAG, "onStop executes ...");
}
protected void onDestroy()
{
super.onDestroy();
Log.i(DEBUG_TAG, "onDestroy executes ...");
}
}
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.