然而在一次使用中,还没等到被调用的 Activity 返回,onActivityResult() 就被执行了。
找了半于,才得知,这与 Activity 的加载模式(launchMode)有关,该属性可以在 AndroidManifest.xml 中设置。原先将其设为 launchmode="SingleTask",经测试,所有需要传递或接收的 Activity 不允许设置该属性,或只能设为标准模式,否则系统将在 startActivityForResult() 后直接调用 onActivityResult()。
解决 Toast 长时间轮流显示问题
//要用的时候调用showToast(context, msg, duration);
private Toast mToast= null;
public void showToast(Context context, String msg, int duration) {
if (mToast == null)
{
mToast = Toast.makeText(context, msg, duration);
}
else {
//Update the text in a Toast that was previously created using one of the makeText() methods.
mToast.setText(msg);
}
mToast.show();
}
Android中Toast重复显示每次都延时累计造成提示框一直显示完累计的时间才退去的问题。
在使用Toast作为提示信息时,Toast会显示在屏幕下方,一般用来提示用户的误操作。当用户在某些情况下,用户连续误操作多次时,会导致出现很多个Toast,依次显示,会在页面上停留很长时间,这个会严重影响软件的用户亲和性。我们可以通过一下方法来实现在一个Toast没有结束的时候再显示Toast不累加时间,而是打断当前的Toast,显示新的Toast。这样Toast就不会停留在界面很久。而最多显示一个Toast提示时间的。
import android.widget.Toast;
--------------------------------------------------------------------------------
//使用的地方1
showTextToast(getString(R.string.toast_irregular_number));
//使用的地方2
showTextToast(getString(R.string.toast_irregular_number2));
--------------------------------------------------------------------------------
private Toast toast = null;
private void showTextToast(String msg) {
if (toast == null) {
toast = Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT);
} else {
toast.setText(msg);
}
toast.show();
}