I have open pdf file through my application.when click on device back button it is automatically come back to my application .It is working fine.Here i want catch back button event in device.I override the back button.but it is not working.please help me.
This is an example of what you are asking:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK ) {
//do your stuff
}
return super.onKeyDown(keyCode, event);
}
just call the foolowing function this will close current activity and move you to the previous screen
finish();
You would override the onKeyDown event in your Activity class and look for the keyCode of KEYCODE_BACK. To prevent further propagation you would return true to stop the system from handling it and this should stop the back button from taking your user out of wherever you are.
This violates the rules of what a user expects to happen, though, and is not recommended unless you're overriding the back button for something like going back through pages in a WebView or something like that.
public void onBackPressed() {
Intent intent = new Intent(CurrentActivity.this,
PreviousActivity.class);
startActivity(intent);
return;
}
If you have handled pdf file related operations(like reading etc) in your application i.e. there is Activity or Fragment handling this pdf file operations in your application then you can overrided onBackPressed() method to put your logic there. But if you are opening pdf via intent i.e using other application using intents action parameter then you can not overrided there onBackPressed() or onKeyDown() event in your application. But instead of that you can put your logic inside overriding onActivityResult() method in your activity from which you are opening the pdf file.
Related
I am developing an application in API level 2.2. In my application whenever I press Home button it forces my app to exit, and when I relaunch that app by clicking on its icon, it starts from where it exited. I have tried to change/ override functionality of Home button but it does not works.
I have tried as
`
public boolean onKeyDown (int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_BACK)
{
startActivity(new Intent(CategoryFirstActivity.this,LastActivity.class));
finish();
}
else if (keyCode == KeyEvent.KEYCODE_HOME)
{
startActivity(new Intent(CategoryFirstActivity.this,LastActivity.class));
finish();
}
return false;
}`
But it works for Back button only and not for Home button.
I have also tried to get keyCode of Home key but I didn't get it.
I need solution to change Home Button functionality.
Simply put: better you do not change the behavior of your home button...
Instead of trying to reprogram your Home button, you could try using some of the Activity callbacks that Android provides. You can take a look at the Activity lifecycle here:
http://developer.android.com/reference/android/app/Activity.html
For example, you could try overriding the onPause callback in an effort to achieve the effect you're going for.
#Override
public void onPause() { /* do stuff */ }
In your case, do stuff might be returning the application to its initial state or forcing it to exit. I've never attempted a force quit myself, but there are some answers on this StackOverflow post:
How to exit from the application and show the home screen?
Best of luck!
I have an app that allows user to select a txt file from a list and then goes off to the internet to get the contents of that file. All works well except when the user accidentally or deliberately presses the hardware back button to go and see the list again.
Now, when the user clicks a new item from the list (a new file that is), instead of loading a the new file, the app continues off from where it was suspended.I do not want that to happen. I know this is related to the life cycle of the activity.
How do I make sure that it loads the new file rather than continuing from where it left off ?
I suppose you're loading the file in onCreate(). You should do that in onResume() instead.
Do not force Activities to close (e.g. use finish()). First, this does not guarantee the Activity will be closed, and second, this is better left to Android to decide.
Another Method to kill an activity is by calling following method.
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
You can override the onBackPressed method in the activity and call finish() to get the desired outcome.
You just need to finish your activity in onBackPressed() method by calling activity.finish();
To destroy activity on back press, use this code.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//Destroys activity.
finish();
}
I have created an Android app.
I need to close or exit my application when I click the back button from my mobile.
How can I achieve that?
you have to handle the back button functionality
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
finish();
}
When you press back button activity is popped form the stack and destroyed. The previous activity in the stack takes focus.
Suppose your have 3 activities. A, B and C. You navigate to C. A to B to c. From C you can navigate to A using the below code.
You can override the back button pressed and call finish().
If you are in activity A you can simply press back button to exit.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
onBackPressed();
}
return super.onKeyDown(keyCode, event);
}
public void onBackPressed() {
Intent myIntent = new Intent(C.this, A.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//clear the backstack
startActivity(myIntent);
finish();
return;
}
Edit:
Some developers and some people on stackoverflow think that back button should atleast go back to the previous activity. It is meant to be like that. So overriding the default functionality and clearing back stack might not be a good idea.
You may also want to consider using Navigation Drawer
http://developer.android.com/design/patterns/navigation.html
Also check this
Is quitting an application frowned upon?
#Override
public void onBackPressed() {
this.finish();
}
Try this
Say if you are in Some inner activity set some boolean on Application Class and check for that boolean in resume of every other activity then finish it if the boolean is set
You don't need to invoke any method to close the app; Android takes care of it. Whenever you press the back button, finish() is called on that Activity and the activity is destroyed. You are not asking about Service right? Because a service runs in the background and you have to take care of closing the service according to your need.
Pleeeease read this answer by CommonsWare: Is quitting an application frowned upon?
It's a very good breakdown on why this is not a good approach for Android app design. Concluding:
Similarly, I would counsel you against attempting to port your
application to the Web, since some of the same problems you have
reported with Android you will find in Web apps as well (e.g., no
"termination"). Or, conversely, someday if you do port your app to the
Web, you may find that the Web app's flow may be a better match for
Android, and you can revisit an Android port at that time.
Try this.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
finish();
}
return false;
}
In my application, There is an activity UserMenu, in which (at on create) subsequent loading is done. I want that next time when the application is opened, this screen should not be reloaded again - I want to save the state of the UserMenu screen as it is.
I have been moving through this, but I am unable to look what I have to implement, I dont want to save any values, simply I want to recreate the same screen as I can get on pressing the home button in android.
Kindly help me regarding my matter.
Thanks
You can do like this
In your UserMenu Activity class
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
goToHome();
return true;
}
return super.onKeyDown(keyCode, event);
}
private void goToHome() {
moveTaskToBack(true);
}
So when you will press back button the Activity will no be destroyed and move to Home and once you will long press Home Button or start your app from menu it wont load the Activity again.
Thanks
It is not possible according to me as like what you are saying about home button. when home button is pressed your activity goes in onPause() and when you reopen it comes in onResume(). You can do what is suggested in the link you are following i.e you can save the values of your Actvity elements using onSavedInstanceState() also it is no guaranteed to execute read this onSavedInstanceState
you need to follow the Activity Lifecycle to make it simple.just make a function call from onCreate() method to load those data,save the data in some standalone class using ArrayList(),and in onResume() display the stored data from ArrayList().and yes,inside onCreate() check for the size of that ArrayList() just to decide whether the data has already been loaded or not.if it has not been loaded,load it.otherwise don't load.
on the ui i am having a back button displayed. there are three activities say A,B,C. Activity A is splash screen. Activity B is home screen. Activity C is edit screen. on edit screen only i am having a back button. when the user press that i open the activity B which also contains some sort of http request and response. i want that the previous activity from stack should get displayed? how can i achieve that? i am able to open previous activity on pressing the back button of device?
when i press back button on device there doesnt goes any http request? i want to achieve this behaviour when i press the ui back button.
I think in your back button you may call the intent for activity B and your http request and response code is in onCreate function
But the back button on device will not call onCreate
There is Two solution for this
One as Macarse say, listen onKeyDown
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
Intent i = new Intent(ActivityC.this,ActivityB.class);
startActivity(i);
return true;
}
return super.onKeyDown(keyCode, event);
}
And the second method is to write you code on onStart of ActivityB
protected void onStart() {
//http request and response code
}
This onStart will call all the time this when ActivityB open
Hope this help you
First of all, it's not good to have a back button displayed in the ui. Every android device has a back button in it.
If you want to handle the back button in a different way, check this link.
The solution with onKeyDown might work but using onBackPressed is much easier in my opinion.
You can intercept the Back-key with following override:
#Override
public void onBackPressed()
{
//logic here, for example an intent
Intent intent = new Intent(this, ActivityB.class);
startActivity(intent);
}
And for the other way around super.onBackPressed(); returns to previous Activity in history.