I have overwritten the functions of the back and the homebutton, to prevent users to use the phone as a phone. We´re giving phones to clients (students) and we don´t want them to abuse the phones in a certain mode.
The thing is, the HOME button is disabled, but when I first open the option menu and then don´t select an option, but press the HOME button, the HOME still works as normal.
Is there anyway to overwrite this and use a boolean, sometimes yes, sometimes no..
What I got now is this:
#Override
public void onBackPressed() {
if (buttonslocked) {
//backbutton blocked!
} else {
super.onBackPressed();
}
}
#Override
public void onAttachedToWindow()
{ //HOMEBUTTON
if(buttonslocked)
{
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
else
{
this.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION);
super.onAttachedToWindow();
}
}
You cannot override the home button functionality.
Just as a clarification, although you may see some hacks that might provide what you are looking for.. This is unsupported and discouraged!
Related
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.
I develop an android application and I need to prevent the user to close the application from navigation bar buttons .. there is another way to close the application.
I search a lot and didn't find any way to Permanently hide bottom navigation bar or at least stop all button control
Use this method to disable the Home button
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
If you meant to disable the back button on device, you could override
#Override
public void onBackPressed() {
super.onBackPressed(); //delete or comment this line
.......//do something you want
}
i know that may duplicate some threads but i can#t figure out what i wrong. I have slider dreawer where are fragments and in fragments there are webview. Everything is working fine at least one thing, namely when i press back button it closes the app. I have tried some other possible solutions but anything is not working. I even don't get any errors. I even tried this easy solution but without any progress
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
My Main activity:
And this is one of my fragments:
By default back button shall close the app, if you are at the main/landing activity(there are other ways as well). If you want to override backbutton behavior, you should be overriding onBackPressed(), which you are doing right, but you should avoid calling super.onBackPressed() (since this gives you the default behavious, i.e. closing the activity OR finish(), with this you're closing the activity yourself. which is what you want to avoid.
Hope it helps.
#Override
public void onBackPressed() {
if (mSlidingDrawer.isOpened()) {
mSlidingDrawer.close()
} else {
Toast.makeText(MyTestApplication.getAppContext(), "Closing application", Toast.LENGTH_SHORT).show();
super.onBackPressed();
}
}
I noticed that my app was receiving few clicks on the InterstitialAd, and I saw that the Ad could be instantly closed by pressing the Back button. Usually the ad takes a few seconds to appear, so the user can close it even before seeing it (when only a black screen appears).
I don't think that it's fair, is almost useless to show it. Is there anyway to prevent this from happening?
I did this, but nothing happened...
// When creating the InterstitialAd, I set the following listener
mInterstitial.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
isShowingInterstitialAd = true;
mInterstitial.show();
}
#Override
public void onAdClosed() {
isShowingInterstitialAd = false;
mAdView.setVisibility(View.VISIBLE);
mAdViewClose.setVisibility(View.VISIBLE);
}
});
// On the Activity's class
#Override
public void onBackPressed() {
if (isShowingInterstitialAd) {
return;
}
// ...
}
My understanding is that AdMob draws its own Activity on top of yours, so it has its own implementation of onBackPressed(), which means that you don't have control of anything that happens once you call mInterstitial.show(); until AdMob gives control back to your activity.
Don't sweat the little things like this.
You are much better off spending energy making your app better.
Some people will never click on ads. But if you provide an inapp purchase that removes the ads they may well buy that. That is a better return on investment for your dev effort.
IMHO
Depending on your OS, there are a few ways to override that back button. Search for back button override to find them.
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.