My overall goal here is to hide the navigation bars when I start an activity in my Android app.
I have two activities, the first one is a login screen and has an async task running and depending on the out come of the result I start my new activity
OnPostExecute in first 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();
}
}
The second activity immediately sets the ui flags to hide the nav bars
OnCreate of second activity
.....
@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);
}
when the second activity is started it is almost as if the flags are ignored and the bars are not hidden.
If i start the second activity straight from the onCreate of the first activity it works fine, but obviously i am by passing my login doing this.
Does anyone know why this happens and how to fix as I was under the impression onPostExecute runs on the main thread so there shouldn't be a problem starting an activity here.
Update!!
I have found what is causing the issue but i am still not sure why.
From the login activity i had added a listener so that when you typed the password in and pressed "done" it logged straight in rather than having to hit the login button. If i remove this listener and go back to using the login button it works fine. here is my listener code.
((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;
}
});
and here is my login button
LoginButton = (Button) findViewById(R.id.loginButton2);
LoginButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0)
{
new GetJSONResult().execute("");
}
});
So the question now is, why would calling the asyncTask from the listener on the EditText
cause the systemUI flags to be ignored on the new activity I start from the OnPostExecute
Simply return false from OnEditorActionListener
method.
Just a little bit change in your code:
((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;
}
});
I have checked it and it's working fine for me.
Hope this will help you ツ
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments