I'm trying to make custom lock screen. So, there I need not to allow user to press Home Button. In the beginning I write
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lock_screen);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
|WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
Then I override OnKeyDown
#Override
public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)||(keyCode == KeyEvent.KEYCODE_POWER)||(keyCode == KeyEvent.KEYCODE_VOLUME_UP)||(keyCode == KeyEvent.KEYCODE_CAMERA)) {
//this is where I can do my stuff
return true; //because I handled the event
}
if((keyCode == KeyEvent.KEYCODE_HOME)){
return true;
}
return false;
}
Here I override onAttacheToWindow
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
super.onAttachedToWindow();
}
But it's giving me error IllegalArgumentException: Window type can not be changed after the window is added. Where is my mistake?
How can I handle Home Button?
There is no "good" way to handle the home button to "do nothing". You really only have two options which may or may not be good enough for you:
Create your own home launcher. Users must select your app as their home launcher.
You could also create a Service where you add a view to your window and it gets plastered on top of everything else. See my previous answer here with "displaying" a webview in a Service: Android: Using WebView outside an Activity context. You will need to modify params to fill the screen and also fix WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE to use the right flags to allow clicks. This won't actually block the home button but it will basically disable it's functionality. Lastly, this might not work on Android 5.0+ as the Status bar's dropdown may overlay your window.
In my android app, when the back button is pressed I want the app to save. This is my code:
#Override
public void onBackPressed() {
super.onBackPressed();
Log.d("VIVZ", counter + "Was Saved");
}
So when i press the back button It exits the app and then when I go back into the app It has not saved.
After 'back' press the Activity is expected to be finished - its instance state is lost. You won't e.g. get onSaveInstanceState() invoked - which is a method helping in preserving activity instance state.
If you want to save app state (if your counter belongs there) it is probably the easiest to use SharedPreferences. Even in that case probably you could consider placing state-saving code in onStop() rather than in onBackPressed()
Try this
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//Do your saving code
}
return super.onKeyUp(keyCode, event);
}
Try to use the activity lifecycle methods instead.
#Override
onPause(){
//use some kind of SharedPreferences class or FileOutputStream
//because onPause() is called when the activity is removed from the ui
}
I want my activity (a normal activity with a digital clock at the center of it) to close when I tap it anywhere. Is there a sort of Activity.setOnClickListener or a way to do it?
Yes on an Activity you override the existing handlers, on controls you add touch listeners, and [activityinstance].finish() gracefully closes your app.
#Override
public boolean onTouchEvent(MotionEvent event) {
this.finish();
return true;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
// Write Code Goes Here means >> this.finish();
return false;
}
This will help you.
use onUserInteraction() for finishing your activity this method called Called whenever a key, touch, or trackball event is dispatched to the activity. as doc says
i want to if the user press home button my service will be stop , this is my code but it's not working for me, please help me:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HOME) {
stopService(new Intent(this, MyService.class));
return true;
}
return super.onKeyDown(keyCode, event);
}
Thanks
If you're trying to make sure the service only runs when the related activity is visible, a better solution would be to stop the service in the onPause() method of whatever activity the user is running. That way, the event is handled when the user presses "back" and when the user presses "home".
Likewise, you can bind your service to the activity (or activities). The bound service will be destroyed when all activities bound to it stop.
public static final int KEYCODE_HOME
This key is handled by the
framework and is never delivered to applications.
Source
Make a root activity and add this,
#Override
public void onTrimMemory(int level) {
super.onTrimMemory(level);
if (level == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
// Get called every-time when application went to background.
System.exit(0);
}
}
extend each activity with rootActivity, it will shutdown service on home/menu button press
On pressing the back button, I'd like my application to go into the stopped state, rather than the destroyed state.
In the Android docs it states:
...not all activities have the behavior that they are destroyed when BACK is pressed. When the user starts playing music in the Music application and then presses BACK, the application overrides the normal back behavior, preventing the player activity from being destroyed, and continues playing music, even though its activity is no longer visible
How do I replicate this functionality in my own application?
I think there must be three possibilities...
Capture the back button press (as below) and then call whatever method(s) the home button calls.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Log.d(this.getClass().getName(), "back button pressed");
}
return super.onKeyDown(keyCode, event);
}
Capture the back button press and then spoof a home button press.
Capture the back button press, then start an Activity of the home screen, effectively putting my application's Activity into the stopped state.
Edit:
I know about services and am using one in the application to which this problem is related. This question is specifically about putting the Activity into the stopped state rather than the destroyed state on pressing the back button.
Most of the time you need to create a Service to perform something in the background, and your visible Activity simply controls this Service. (I'm sure the Music player works in the same way, so the example in the docs seems a bit misleading.) If that's the case, then your Activity can finish as usual and the Service will still be running.
A simpler approach is to capture the Back button press and call moveTaskToBack(true) as follows:
// 2.0 and above
#Override
public void onBackPressed() {
moveTaskToBack(true);
}
// Before 2.0
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
I think the preferred option should be for an Activity to finish normally and be able to recreate itself e.g. reading the current state from a Service if needed. But moveTaskToBack can be used as a quick alternative on occasion.
NOTE: as pointed out by Dave below Android 2.0 introduced a new onBackPressed method, and these recommendations on how to handle the Back button.
Use the following code:
public void onBackPressed() {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
}
If you want to catch the Back Button have a look at this post on the Android Developer Blog. It covers the easier way to do this in Android 2.0 and the best way to do this for an application that runs on 1.x and 2.0.
However, if your Activity is Stopped it still may be killed depending on memory availability on the device. If you want a process to run with no UI you should create a Service. The documentation says the following about Services:
A service doesn't have a visual user interface, but rather runs in the background for an indefinite period of time. For example, a service might play background music as the user attends to other matters, or it might fetch data over the network or calculate something and provide the result to activities that need it.
These seems appropriate for your requirements.
try to override void onBackPressed() defined in android.app.Activity class.
if it helps someone else, I had an activity with 2 layouts that I toggled on and off for visibilty, trying to emulate a kind of page1 > page2 structure. if they were on page 2 and pressed the back button I wanted them to go back to page 1, if they pressed the back button on page 1 it should still work as normal. Its pretty basic but it works
#Override
public void onBackPressed() {
// check if page 2 is open
RelativeLayout page2layout = (RelativeLayout)findViewById(R.id.page2layout);
if(page2layout.getVisibility() == View.VISIBLE){
togglePageLayout(); // my method to toggle the views
return;
}else{
super.onBackPressed(); // allows standard use of backbutton for page 1
}
}
hope it helps someone,
cheers
Working example..
Make sure don't call super.onBackPressed();
#Override
public void onBackPressed() {
Log.d("CDA", "onBackPressed Called");
Intent setIntent = new Intent(Intent.ACTION_MAIN);
setIntent.addCategory(Intent.CATEGORY_HOME);
setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(setIntent);
}
In this way your Back Button act like Home button . It doesn't finishes your activity but take it to background
Second way is to call moveTaskToBack(true); in onBackPressed and be sure to remove super.onBackPressed
Even better, how about OnPause():
Called as part of the activity lifecycle when an activity is going into the background, but has not (yet) been killed. The counterpart to onResume().
When activity B is launched in front of activity A, this callback will be invoked on A. B will not be created until A's onPause() returns, so be sure toenter code here not do anything lengthy here.
This callback is mostly used for saving any persistent state the activity is editing and making sure nothing is lost if there are not enough resources to start the new activity without first killing this one.
This is also a good place to do things like stop animations and other things that consume a noticeable amount of CPU in order to make the switch to the next activity as fast as possible, or to close resources that are exclusive access such as the camera.
Override onBackPressed() after android 2.0.
Such as
#Override
public void onBackPressed() {
moveTaskToBack(true);
}
I have use #Mirko N. answser using made the new Custom EditText
public class EditViewCustom extends EditText {
Button cancelBtn;
RelativeLayout titleReleLayout;
public EditViewCustom(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public EditViewCustom(Context context, AttributeSet attrs) {
super(context, attrs);
}
public EditViewCustom(Context context) {
super(context);
}
public void setViews(Button cancelBtn,RelativeLayout titleReleLayout){
this.cancelBtn = cancelBtn;
this.titleReleLayout = titleReleLayout;
}
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
Log.d("KEYCODE_BACK","KEYCODE_BACK");
cancelBtn.setVisibility(View.GONE);
this.setFocusableInTouchMode(false);
this.setFocusable(false);
titleReleLayout.setVisibility(View.VISIBLE);
return super.onKeyPreIme(keyCode, event);
}
return super.onKeyPreIme(keyCode, event);
}
}
Then set data from your activity
searchEditView.setViews(cancelBtn, titleRelativeLayout);
Thank you.
I've tried all the above solutions, but none of them worked for me. The following code helped me, when trying to return to MainActivity in a way that onCreate gets called:
Intent.FLAG_ACTIVITY_CLEAR_TOP is the key.
#Override
public void onBackPressed() {
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}