App exits on back button click in android qtquick - android

I am developing an app which will run firstly on Android. However, I have an issue with back button: each time it is tapped the application quits. I don't want this behaviour. I have checked a lot of approaches and I've tried to implement some code but nothing worked.
Here is my code:
Item {
id: student_home_page;
focus: true
// ///////////////////////////////////////////////////////////////
Keys.onReleased: {
console.log("TEST for Back ");
if (event.key == Qt.Key_Back) {
console.log("BAck Button HAndled");
event.accepted = true;
}
}
}
When I click back button after reaching this page, it does not print anything on console as it is not going inside
I only get this message on console of Qt Creator:
/uniActivity(15431): onStop
I/AndroidRuntime(15431): VM exiting with result code 0, cleanup skipped.
Any idea why it is not handling this key event or not any at all inside?

You should handle the back button inside the StackView itself, not a child page, see my answer here

Maybe this could be useful for you. Try to override the activity onBackPressed() method:
#Override
public void onBackPressed() {
// Do something or call finish()
}

Related

How to control what the home button on my android wearable device does when pressed?

I have a certain activity that begins when I tap my smart watch's screen. There is a timer and bunch of stuff that happens, but the process is crucial, so I am handling certain cases or things that might happen that would disturb the flow of things.
So basically, I want to prevent the home button of my watch to exit the app and go to the homescreen while my timer is running. I keep looking this up and most people say to override the onBackPressed method. But this was for the back button, and I I realized the button is a home button not a back button.
frameLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
clicked = clicked + 1;
if (clicked == 2)
{
Toast.makeText(toolandmode.this, "Clicks:" + clicked, Toast.LENGTH_SHORT).show();
startTimer();
}
else if (clicked >= 4)
{
Toast.makeText(toolandmode.this, "Clicks:" + clicked, Toast.LENGTH_SHORT).show();
AlertMessage();
}
}
return true;
}
});
this is the main method I use
Just override the onBackPressed function.
#Override
public void onBackPressed ()
{
//Control the flow
}
Use third part library in case if it solves your problem.
Here is the link:
https://github.com/shaobin0604/Android-HomeKey-Locker
The general consensus is that you can't override the home button behavior on a Wear OS device, just like you can't override the home button on an Android phone. This is by design to prevent developers from preventing the user to leave an application. Even if there is a hacky way to do it, this is not officially supported and may stop working at any time in future OS versions. I highly suggest not doing it since it goes against the basic navigation model of the device.
More details, and some workarounds for common use cases where people think they need to override the home button can be found in this blog post.

Android Device Backbutton should not work

First of all i am learning android, I am testing my Andorid app in my Samsung Galaxy S1.
My app Function is: while i am pressing RandomNumber button, it will generate Random numbers and displaying in the screen in TextArea.
But i am facing the below issues.
The Device back button is allow user to go back. How i can avoid that? ( I have buttons defined in the program dynamically, only that Back button should work )
While shaking the phone or change the position of the phone, then the Random numbers are automatically generating. How to avoid that?. Please advise.
Button Creation Dynamic
final Button buttonToAdd = new Button(this);
buttonToAdd.setText("RandomNumber");
Listener:
buttonToAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Strvalue = (String) buttonToAdd.getText();
if (Strvalue.equals("RandomNumber"))
{
Randomnumbergeneration();
}
}
});
You can overwrite the Activities onBackPressed() method to handle the back-button click event.
#Override
public void onBackPressed() {
// put some code here or just do nothing
// don't call super.onBackPressed() if you want to disable the back function
}
But if you want to publish your application you should follow the official design guidelines and do not disable this behaviour because every android user is used to it and will find it unlikely if the back button does not work anymore.

How to refresh the screen when back button is pressed

I have scenario with two screens.
Screen 1 shows data from from API in list format
There is a "+" button in menu bar
Clicking this button takes user to screen 2
User can enter some info on screen 2 and press the "save" button on top of this screen. This does a POST to my API and saves the data.
After saving, I would like to put the user back to screen 1. I've done that with this:
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
if (menuItem.getTitle().toString().equalsIgnoreCase("save")) {
new CreateSomethingTask(this,enteredName.getText().toString(), id).execute();
Intent listscreen = new Intent(getApplicationContext(), ShowListActivity.class);
startActivity(listscreen);
return true;
}
return true;
}
However, the added item is not shown. If I close my app and open it again then the item shows up.
Is there a good way to handle this? I like how the Github Android app handles this when creating a new Gist. But I'm not sure how to implement that.
You should start your screen2 with startActivityForResult(). That way you could send a result back and a code and proceed to refresh your screen1. See example : How to manage `startActivityForResult` on Android?
Below function maybe help you, didn't tried.
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
callFunctionToRefreshList();
//or redraw data from api
//setContentView(R.layout.activity_book);
}

Actionbar keeps hiding

I'm currently trying to build an Android app with Xamarin Studio (aka monodroid). My problem is as follows:
In my Activity's OnCreate I call
this.RequestWindowFeature(WindowFeatures.ActionBar);
this.RequestWindowFeature(WindowFeatures.ActionModeOverlay);
Also, I added this override to the Activity:
public override Boolean OnTouchEvent(MotionEvent e)
{
if (this.ActionBar.IsShowing)
{
this.ActionBar.Hide();
}
else
{
this.ActionBar.Show();
}
return base.OnTouchEvent(e);
}
This, however, doesn't work as I expect it to: When I first tap on the Activity, the bar is hidden. The next tap shows it, but it doesn't even fully slide into view before it retreats again. Also, it seems to "cram" the contents of the Activity, which it shouldn't do since I added WindowFeatures.ActionModeOverlay.
What am I missing?
EDIT:
I am certain that the method itself is only triggered once per touch (I logged a counter to diagnose).
Also, I don't show()/hide() the ActionBar anywhere else.
EDIT 2: I was certain, but probably messed up the diagnostic approach. :/
After adding
if (e.Action == MotionEventActions.Cancel
|| e.Action == MotionEventActions.Up)
{
return base.OnTouchEvent(e);
}
it behaves as expected.
Motion Event is catching an event when you are releasing a touch, you need to filter the event further using e.Action for touch down and release events. I think down is triggering show, and release is hiding it again

how to handle device back button on sencha touch application

In Sencha touch if I use navigation view i can get back button. This is pretty fine.
But what if user hit device backbutton? it is direct exiting the applicaiton. In my requirement it should not exit the application it has to go back to previous screen.How can i do this?.
You can handle hardware back button like this:
if (Ext.os.is('Android')) {
document.addEventListener("backbutton", Ext.bind(onBackKeyDown, this), false);
function onBackKeyDown(eve) {
eve.preventDefault();
//do something
alert('back button pressed');
}
}
I didn't find the instructions on the history support page that useful when trying to do this; I couldn't see anyway to use routes when dealing with a navigation view which can have a large stack of views on it at anytime.
If you just want the back button to work though, you can use the popstate and pushstate functions (see https://developer.mozilla.org/en-US/docs/DOM/Manipulating_the_browser_history for a reference). The idea is that you push a state when adding a view and pop it off when removing one. The physical back button on an Android phone, or the back button on a desktop browser effectively calls history.back(); so all you need to do is ensure that pressing the back button on the titlebar does the same, and it is that which triggers the nav view to pop.
To make use it work in Sencha Touch, I add the following to the main controller:
In refs I have references to the main view (an instance of Ext.navigation.View) and to its titlebar, from which you can hook onto the event of the back button e.g.:
refs: {
main: 'mainview',
mainTitleBar: 'mainview titlebar',
}...
I attach the following functions via the control config object..
control: {
main: {
push: 'onMainPush'
},
mainTitleBar: {
back: 'onBack'
},
...
These are defined as:
onMainPush: function(view, item) {
//do what ever logic you need then..
history.pushState();
},
onBack: function() {
history.back(); //will cause the onpopstate event to fire on window..
//prevent back button popping main view directly..
return false;
},
I then attach a function to execute when the state is popped via the init function of..
init: function() {
/* pop a view when the back button is pressed
note: if it's already at the root it's a noop */
var that = this;
window.addEventListener('popstate', function() {
that.getMain().pop();
}, false);
},
Now, pressing back on the titlebar, executes history.back(), this in turn fires the popstate event which then causes the main view to pop.
If you want to see this working on a real application, there is a (v. basic!) property finder app using this technique on github here.

Categories

Resources