从 onPostExecute(String result) 开始 activity 不设置 ui 标志
Starting activity from onPostExecute(String result) doesn't set ui flags
我的总体目标是在我的 Android 应用程序中启动 activity 时隐藏导航栏。
我有两个活动,第一个是登录屏幕并有一个异步任务 运行 根据结果,我开始我的新活动 activity
OnPostExecute 在第一个activity
.....
@Override
protected void onPostExecute(String result)
{
try
{
if (result.equals("true"))
{
Intent intent = new Intent(mContext, MainRota.class);
startActivity(intent);
finish();
}
else
{
CharSequence text = "One or more fields are incorrect!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(mContext, text, duration);
toast.show();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
第二个 activity 立即设置 ui 标志以隐藏导航栏
第二个 activity
的 OnCreate
.....
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
View decorView = getWindow().getDecorView();
// Hide both the navigation bar and the status bar.
// SYSTEM_UI_FLAG_FULLSCREEN is only available on Android 4.1 and higher, but as
// a general rule, you should design your app to hide the status bar whenever you
// hide the navigation bar.
int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
decorView.setSystemUiVisibility(uiOptions);
}
当第二个 activity 启动时,几乎就像忽略了标志并且没有隐藏栏一样。
如果我直接从第一个 activity 的 onCreate 开始第二个 activity 它工作正常,但显然我是通过我的登录来执行此操作的。
有谁知道为什么会发生这种情况以及如何解决,因为我的印象是 onPostExecute 在主线程上运行,所以在这里启动 activity 应该没有问题。
更新!!
我已经找到导致问题的原因,但我仍然不确定原因。
从登录 activity 我添加了一个侦听器,这样当您输入密码并按下 "done" 时,它会直接登录,而不必点击登录按钮。如果我删除此侦听器并返回使用登录按钮,它可以正常工作。这是我的监听器代码。
((EditText)findViewById(R.id.password)).setOnEditorActionListener(
new EditText.OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if (actionId == EditorInfo.IME_ACTION_SEARCH ||
actionId == EditorInfo.IME_ACTION_DONE ||
event.getAction() == KeyEvent.ACTION_DOWN &&
event.getKeyCode() == KeyEvent.KEYCODE_ENTER)
{
new GetJSONResult().execute("");
return true;
}
return false;
}
});
这是我的登录按钮
LoginButton = (Button) findViewById(R.id.loginButton2);
LoginButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0)
{
new GetJSONResult().execute("");
}
});
所以现在的问题是,为什么从 EditText
上的侦听器调用 asyncTask 会导致在新 activity 上忽略 systemUI 标志 我从 OnPostExecute
开始
尝试在 UI 线程中启动 AsyncTask,例如:
runOnUiThread(new Runnable(){
@Override public void run(){
new GetJSONResult().execute("");
}
});
只是 return false 来自 OnEditorActionListener
方法。
只需稍微更改一下您的代码:
((EditText)findViewById(R.id.password)).setOnEditorActionListener(
new EditText.OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if (actionId == EditorInfo.IME_ACTION_SEARCH ||
actionId == EditorInfo.IME_ACTION_DONE ||
event.getAction() == KeyEvent.ACTION_DOWN &&
event.getKeyCode() == KeyEvent.KEYCODE_ENTER)
{
new GetJSONResult().execute("");
}
return false;
}
});
我已经检查过了,它对我来说工作正常。
希望对你有所帮助ツ
我的总体目标是在我的 Android 应用程序中启动 activity 时隐藏导航栏。
我有两个活动,第一个是登录屏幕并有一个异步任务 运行 根据结果,我开始我的新活动 activity
OnPostExecute 在第一个activity
.....
@Override
protected void onPostExecute(String result)
{
try
{
if (result.equals("true"))
{
Intent intent = new Intent(mContext, MainRota.class);
startActivity(intent);
finish();
}
else
{
CharSequence text = "One or more fields are incorrect!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(mContext, text, duration);
toast.show();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
第二个 activity 立即设置 ui 标志以隐藏导航栏
第二个 activity
的 OnCreate.....
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
View decorView = getWindow().getDecorView();
// Hide both the navigation bar and the status bar.
// SYSTEM_UI_FLAG_FULLSCREEN is only available on Android 4.1 and higher, but as
// a general rule, you should design your app to hide the status bar whenever you
// hide the navigation bar.
int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
decorView.setSystemUiVisibility(uiOptions);
}
当第二个 activity 启动时,几乎就像忽略了标志并且没有隐藏栏一样。
如果我直接从第一个 activity 的 onCreate 开始第二个 activity 它工作正常,但显然我是通过我的登录来执行此操作的。
有谁知道为什么会发生这种情况以及如何解决,因为我的印象是 onPostExecute 在主线程上运行,所以在这里启动 activity 应该没有问题。
更新!!
我已经找到导致问题的原因,但我仍然不确定原因。
从登录 activity 我添加了一个侦听器,这样当您输入密码并按下 "done" 时,它会直接登录,而不必点击登录按钮。如果我删除此侦听器并返回使用登录按钮,它可以正常工作。这是我的监听器代码。
((EditText)findViewById(R.id.password)).setOnEditorActionListener(
new EditText.OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if (actionId == EditorInfo.IME_ACTION_SEARCH ||
actionId == EditorInfo.IME_ACTION_DONE ||
event.getAction() == KeyEvent.ACTION_DOWN &&
event.getKeyCode() == KeyEvent.KEYCODE_ENTER)
{
new GetJSONResult().execute("");
return true;
}
return false;
}
});
这是我的登录按钮
LoginButton = (Button) findViewById(R.id.loginButton2);
LoginButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0)
{
new GetJSONResult().execute("");
}
});
所以现在的问题是,为什么从 EditText
上的侦听器调用 asyncTask 会导致在新 activity 上忽略 systemUI 标志 我从 OnPostExecute
开始
尝试在 UI 线程中启动 AsyncTask,例如:
runOnUiThread(new Runnable(){
@Override public void run(){
new GetJSONResult().execute("");
}
});
只是 return false 来自 OnEditorActionListener
方法。
只需稍微更改一下您的代码:
((EditText)findViewById(R.id.password)).setOnEditorActionListener(
new EditText.OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if (actionId == EditorInfo.IME_ACTION_SEARCH ||
actionId == EditorInfo.IME_ACTION_DONE ||
event.getAction() == KeyEvent.ACTION_DOWN &&
event.getKeyCode() == KeyEvent.KEYCODE_ENTER)
{
new GetJSONResult().execute("");
}
return false;
}
});
我已经检查过了,它对我来说工作正常。
希望对你有所帮助ツ