To show a toast when the user is in one of the applications.
You just need a link to the current activity and call it using this code example:
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 are many options for getting current activity, check this question: How to get the 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 action must have:
@Override protected void onResume() { super.onResume(); ((MyApplication) getApplication()).setCurrentActivity(this); } @Override protected void onPause() { super.onPause(); ((MyApplication) getApplication()).setCurrentActivity(null); }
Daniel De LeΓ³n Jun 13 '13 at 21:35 2013-06-13 21:35
source share