Starting activity from onPostExecute(String result) doesn't set ui flags - android

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 ツ

Try to start your AsyncTask in the UI thread like :
runOnUiThread(new Runnable(){
#Override public void run(){
new GetJSONResult().execute("");
}
});

Related

Back press event of device to dismiss a window

I have created a window and i am showing it on screen through Broadcast receiver.But the problem is that it appears on the screen and i want to dismiss it once the back button is pressed.I am unable to get the event of button press on this view.My code for back press looks as follows-
view.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getKeyCode() == KEYCODE_BACK) {
Log.d("LOG", "back button is pressed");
}
return true;
}
});
But Nothing is happening.I tried to do the same through DISPATCHKEY but it was also of no use.Please help me what i am not figuring out.Wouldn't this work on the view.?
Maintain global reference for Window and override onBackPressed()
Try this:
#Override
public void onBackPressed() {
if (view != null && view.isShowing()) {
view.dismiss();
} else {
super.onBackPressed();
}
}

onBackPressed function not working under IMMERSIVE STICKY mode

I'm using Immersive Mode for an activity with videoView inside.
My goal is when touch on screen, the media controller and the system control bar show or disappear together. Everything works fine now.
The problem is I can't leave activity properly. When I press the back button once, the system bar just hide again and nothing happens. I have to press twice to exit the activity. I don't know why.
Here's my code.
I use an FullScreenActivity() activity to define IMMERSIVE MODE:
#TargetApi(Build.VERSION_CODES.KITKAT)
private void FullScreenActivity() {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav // bar
| View.SYSTEM_UI_FLAG_FULLSCREEN// hide status bar;
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
}
in OnCreate():
#Override
protected void onCreate(Bundle savedInstanceState) {
//getActionBar().setDisplayShowTitleEnabled(false);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
FullScreenActivity();
And in the media controller, I define the IMMERSIVE MODE again, because if I don't after the controller shows, the actionbar and system bar will be back again.
private class MyController extends MediaController {
public MyController(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public boolean onTouchEvent(MotionEvent event) {
//........
}
#Override
public void show() {
super.show();
FullScreenActivity();
}
#Override
public void hide() {
super.hide();
FullScreenActivity();
}
}
and at last, in the onBackPressed(), I implemented my function:
#Override
public void onBackPressed() {
Toast.makeText(getApplicationContext(), "onbackpressed", Toast.LENGTH_SHORT).show();
super.onBackPressed();
if (tgtIDArr != null && tgtIDArr.size() > 0) {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Exit Player!")
.setMessage("Are you sure you want to leave Player?")
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
.......
finish();
}
}).setNegativeButton("No", null).show();
}else{
........
finish();
}
}
I don't know why, but only when I press back button twice would trigger this onBackPressed() function.
Anyone knows why? thanks!!
If you want to exit the activity when the back button is pressed and the media controller is visible then you can intercept the back button with the following code snippet.
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
int keyCode = event.getKeyCode();
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
return true;
}
return super.dispatchKeyEvent(event);
}
Which is the same solution to a similar problem here Back button not working in Media player

Android Back Button Pressed Check

So I'm currently developing a turn based game app. And it displays toasts from a view on who's turn it is after each turn is taken. What's weird though, is if I hit the back button it will go back to the main menu(previous activity) but the toasts will still linger till they time out. Also if I hit back twice and it goes to the home screen, the toasts will still show until they finish. I want to implement a check or some way to cancel those toasts when the back button is pressed. I have to do this in my view too, my view contains all the toasts and all the code to the game, the gameActivity only has onCreate to create the view for the game. Any ideas?
In your Activity you have override method:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// do something
yourView.notifyBackPressed();
return false;
}
return super.onKeyDown(keyCode, event);
}
And in your View you have to implement method, for example notifyBackPressed().
Try This...
Try to use Toast.LENGTH_SHORT or
You should set duration (milliseconds) for custom Toast like this.
Custom toast:
LayoutInflater inflater = getActivity().getLayoutInflater();
View layoutToast = inflater.inflate(R.layout.custom_toast_layout,
(ViewGroup) getActivity().findViewById(R.id.toastcustom));
((TextView) layoutToast.findViewById(R.id.texttoast)).setText("I'm custom toast");
final Toast myToast = new Toast(
(EmployerNominationView) getActivity());
myToast.setView(layoutToast);
myToast.setDuration(300);
myToast.setGravity(Gravity.BOTTOM, 0, 45);
myToast.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
myToast.cancel();
}
}, 1000);
Generate Toast
Toast toast;
toast = Toast.makeText(getBaseContext(), "Messages", Toast.LENGTH_LONG);
toast.show();
Cancel the Toast you may use onKeyDown() or onBackPressed() .
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
toast.cancel();
return true;
}
return super.onKeyDown(keyCode, event);
}
//Try with making Toast Global and cancel the toast on Back Pr
Toast mToast = new Toast(ApplicationContext);
public void onBackPressed(){
mToast.cancel();
}
I found this and will hellp you
Android AppMsg (Crouton) Library
Implementation of in-layout notifications. Based on Toast notifications.
I try the below method to detect the back button pressed on the action bar in activity by the first method and the second one is used to detecting the mobile hardware button back or kill activity button.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onBackPressed() {
setResult(RESULT_CANCELED);
super.onBackPressed();
}

onBackPressed() does not work

I'm showing up a Dialog on app-start, where you have to select a config. Since it is essential to select one config, I want to "disable" the back-button via a empty onBackPressed().
I got the following code in a DialogFragment:
public class ChangeConfigDialogFragment extends DialogFragment {
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.dialog_config_change)
.setItems(R.array.config_array,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// The 'which' argument contains the index
// position
// of the selected item
if (which == 0){
Initiation.BAUDRATE = 500;
Toast.makeText(getActivity(), "Baudrate at " + Initiation.BAUDRATE, Toast.LENGTH_LONG).show();
if (Initiation.getADK() == null){
Initiation.initiateCAN();
}
} else if (which == 1) {
Initiation.BAUDRATE = 600;
Toast.makeText(getActivity(), "Baudrate at " + Initiation.BAUDRATE, Toast.LENGTH_LONG).show();
if (Initiation.getADK() == null){
Initiation.initiateCAN();
}
} else if (which == 2) {
Initiation.BAUDRATE = 700;
Toast.makeText(getActivity(), "Baudrate at " + Initiation.BAUDRATE, Toast.LENGTH_LONG).show();
if (Initiation.getADK() == null){
Initiation.initiateCAN();
}
}
}
});
// Create the AlertDialog object and return it
return builder.create();
}
public void onBackPressed(){
Log.d(getTag(), "are you there?");
}
}
The problem is, that the onBackPressed() is never been called. Even the log message does not appear.
I tried to clean the project, but no success. Also tried to use a onKeyDown-method from some other topics here on SO. Does anyone has a clue how to solve this?
EDIT:
It works now. .setCancelable(false); worked, but I was to stupid to add it to the Dialog from which the Fragment was called. (instead added it to the builder
Thanks for all your help and time.
From your question it seems that while your dialog is open you need not to close dialog untill user can select any 1 open from dialog if i am not wrong then,so for this you need not to disable back key but you have to set this two properties for dialog.
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
OR
If you want to disable back key then use the below code,
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//preventing default implementation previous to android.os.Build.VERSION_CODES.ECLAIR
return true;
}
return super.onKeyDown(keyCode, event); }
OnbackkeyPressed Requires Api Level 5 Or higher.
#Override
public void onBackPressed() {
}
Use builder.setCancelable(false)
http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setCancelable(boolean)
I did not try this, but it might work if you set the OnKeyListeneron the builder like this:
builder.setOnKeyListener(new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return false;
}
}
});
Try
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Handle the back button
if (keyCode == KeyEvent.KEYCODE_BACK) {
return false;
}
return super.onKeyDown(keyCode, event);
}
or
dialog.setCancelable(false);

how to listen key when popup is opened

I am writing an application to animate popup window. It was working great with my code.
I want to close the popup window(i.e to slide-down it), when back button is pressed on device.
But I couldn't listen any one key from device. I used setOnKeyListener of that popup window, I didn't even get log from it.
My Code is given below:
popup_layout = layoutInflater.inflate(R.layout.popup_addchannel, null);
popupWindow = new PopupWindow(popup_layout, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
subscribeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Log.d(TAG,
// "Button is clicked for animation.... Visibility is"
// + subscribeButton.getVisibility());
openMenu(view);
}
});
popup_layout.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
Log.d(TAG, "on key button click called.........");
return false;
}
});
public void openMenu(View view) {
if (!flag) {
popupWindow.setAnimationStyle(R.style.PopupWindowAnimation);
popupWindow.showAtLocation(view.findViewById(R.id.button1),
Gravity.CENTER, 0, 0);
popupWindow.setFocusable(true);
popupWindow.update();
flag = true;
} else {
popupWindow.dismiss();
popupWindow.setFocusable(false);
flag = false;
}
}
What is the problem behind this?
Shall i achieve my requirement?
Please, Guide me.
Thank you in Advance!
try this....
final PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,true);
...
popupWindow.getContentView().setFocusableInTouchMode(true);
popupMenu.getContentView().setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU &&
event.getRepeatCount() == 0 &&
event.getAction() == KeyEvent.ACTION_DOWN) {
// ... payload action here. e.g. popupMenu.dismiss();
return true;
}
return false;
}
});
The dialog has a property of cancelling the dialog on back press.
dialog.setCancelable(true);
EDIT: Check Qberticus answer on this link: Android popup window dismissal
and you can also see: Issue dismissing popup window
Yes you can use this
dialog_obj.setCancelable(true);
You should override the onKeyPressed() method of your activity as below.
#Override
public void onBackPressed() {
super.onBackPressed();
//your code to close the popup window.
popupWindow.setAnimationStyle(R.style.PopupWindowAnimation);
popupWindow.showAtLocation(view.findViewById(R.id.button1),
Gravity.CENTER, 0, 0);
opupWindow.setFocusable(true);
popupWindow.update();
flag = true;
}
popupWindow.getContentView().setOnClickListener(new OnClickListener()) {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}
});
Try this.
You can't listen a key press in popup window, but when you listen to system back key, you can override dismiss() method to intercept it.

Categories

Resources