How to detect when a fire tv application is minimized? - android-tv

Is there a way to detect when a user minimizes the fire tv application by pressing the home icon on the remote?

No there is no key event to detect the Home button key press but u can
check for onStop method of the activity and where u can add additional
condition whether backkey pressed or not as its always called before
onStop().
boolean flag=false;
#Override
public void onBackPressed() {
super.onBackPressed();
flag=true;
}
#Override
protected void onStop() {
super.onStop();
if(flag){
//back button pressed
}
else{
// Home button pressed
}
}

Related

Xamarin.Forms MvvM Prism Software and Hardware Back Button

I have problem with implementation Code which resolve problem with confirm software nad hardware button back. I need confirm and save state page field. When confirm is true and save state have no error I want close page and when confirm is false or save state have error I want stop closing page. I use xamarin.forms with mvvm prism.
If you mean want to custom the method of Software and Hardware Back Button of Android device, you could override OnOptionsItemSelected and OnKeyDown method in MainActivity.cs to achieve that.
Software Back Button code in MainActivity.cs:
protected override void OnCreate(Bundle savedInstanceState)
{
...
Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
SupportActionBar.SetHomeButtonEnabled(true);
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
if(item.ItemId == Android.Resource.Id.Home)
{
Console.WriteLine("software back button press");
return false;
}
else
{
return base.OnOptionsItemSelected(item);
}
}
Hardware Back Button code in MainActivity.cs:
public override bool OnKeyDown([GeneratedEnum] Keycode keyCode, KeyEvent e)
{
if(e.Action == KeyEventActions.Down && keyCode == Keycode.Back)
{
Console.WriteLine("hardware back button press");
}
return false;
}

How to Handle Back Button in Persistent Search Library

In my App i am Using Navigation Drawer and Persistent Search Library in Action
bar https://github.com/KieronQuinn/PersistentSearch
So when i am in my Home Activity and Search View is not Shown and i press back Button
App is Exit Normally (No Issue)
But when Search View is Open and i Press back button there is an Exception Occurs
So i want to know how to handle Back Button In Persistent Search Library
Here is the Exception Details
Exception image
I figure out the way of handling this as
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (this.search.isActivated()) {
closeSearch();
}
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
But Same Exception Occures
Any help will be Appreciated
Put this code in your activity to handle backpress event for search view
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getKeyCode() == 4 && binding.appBrLt.searchbox.getVisibility() == View.VISIBLE) {
//here write the code
return true;
} else {
return super.dispatchKeyEvent(e);
}
}

Save & Restore Page in Xamarin Forms

I'm looking to save the current navigation stack on the OnSleep Event in my Xamarin Forms page and restore it on the OnResume Event. Is it possible to do this?
Cheers!
I think you should not memorize all navigation stack. Your device decide to kill your app or to restart from the last page you have seen when it comes up from background. I think you can memorize if you are "Logged in" or not: if you are "Logged in" you can restart from the first page "after the login", otherwise start "from the login".
For this case you can take a look to this link and use Properties
public class App : Xamarin.Forms.Application
{
public App ()
{
}
protected override void OnStart()
{
// Handle when your app starts
Debug.WriteLine ("OnStart");
checkLogin();
}
protected override void OnSleep()
{
// Handle when your app sleeps
Debug.WriteLine ("OnSleep");
}
protected override void OnResume()
{
// Handle when your app resumes
Debug.WriteLine ("OnResume");
checkLogin();
}
}
void checkLogin(){
if (Application.Current.Properties.ContainsKey("IsLogged"))
{
var IsLogged = Application.Current.Properties ["IsLogged"] as bool;
// do something with IsLogged
if(IsLogged)
MainPage = new MyFirstPage();
else
MainPage = new MyLoginPage();
}
else
MainPage = new MyLoginPage();
}
then, when you have logged in
Application.Current.Properties ["IsLogged"] = true;

how to invoke fragmentB method in another fragmentA

I have a fragment_A with tabs, consider tabs as fragment_B and C. And am implementing custom keypad with "Done" key in it. In my main Activity iam calling the listener to press the done button
// Used when "Done" button pressed in keyboard
#Override
public void keylisten() {
((Housing) fragmentStack.lastElement()).whenokkeypressed();
}
Now i want to call a method from fragment_B which goes into the whenokkeypressed() of the fragment_A;
There are 2 things you should do
Make whenonkeypressed() static and call it from class name of the fragment like Fragment_A.whenonkeypressed()
(optional) instead of using keylisten of done in mainActivity you should prefer an anonymous inner class like
editText.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
whenonkeypressed();
} });

JavaFX How to stop cursor from moving to a new line in a TextArea when Enter is pressed

I have a Chat application. I'd like the cursor in the chatTextArea to get back to the position 0 of the TextArea chatTextArea.
This, however, won't work:
chatTextArea.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent ke) {
if (ke.getCode().equals(KeyCode.ENTER)) {
ChatClient.main(new String[]{"localhost", String.valueOf(4444), chatTextArea.getText()});
chatTextArea.setText("");
chatTextArea.positionCaret(0);
}
}
});
How can I get it to work? Thank you.
The TextArea internally does not use the onKeyPressed property to handle keyboard input. Therefore, setting onKeyPressed does not remove the original event handler.
To prevent TextArea's internal handler for the Enter key, you need to add an event filter that consumes the event:
chatTextArea.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent ke) {
if (ke.getCode().equals(KeyCode.ENTER)) {
ChatClient.main(new String[]{"localhost", String.valueOf(4444), chatTextArea.getText()});
chatTextArea.setText("");
// chatTextArea.positionCaret(0); // not necessary
ke.consume(); // necessary to prevent event handlers for this event
}
}
});
Event filter uses the same EventHandler interface. The difference is only that it is called before any event handler. If an event filter consumes the event, no event handlers are fired for that event.

Resources