When I click the back button Android goes to the previous activity.
Is it possible to set for every activity a custom (back) activity
or to set the back button to the home menue of the app?
Help or hints would be great :)
You will have to override onBackPressed() from your activity:
#Override
public void onBackPressed()
{
super.onBackPressed();
startActivity(new Intent(ThisActivity.this, NextActivity.class));
finish();
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent i = new Intent(this.class, yourcustomclass);
startActivity(i);
finish();
}
}
Yes it's possible, just add this method to your activity:
public void onBackPressed() {
//Do the stuff you want on backbutton pressed.
}
Yes you should #override the onBackPressed() function and create an Itent to go wherever you want.
You can override the
#Override
public void onBackPressed(){
}
If you need to go back what ever activity, when click on ActionBar back arrow(Home). overide onSupportNavigateUp()
#Override
public boolean onSupportNavigateUp() {
//onBackPressed(); //this will be go to parent activity
//******************//
// Your intent here //
//******************//
return true;
}
Related
When you press back in ActionBar, it causes onCreate on the previous activity. How to do that with device back button aswell? In preference activity, if a certain preference is changed, I want main activity to be re-created when user presses back on bottom of their device.
You can override onBackPressed() and start your Activity yourself.
You call onNavigateUp() at any time in your Activity to get the same behavior as clicking the up button.
For example:
#Override
public void onBackPressed() {
if (myPreferenceHasChanged) {
onNavigateUp();
} else {
// Use the default behavior
super.onBackPressed();
}
}
Did you try something like this?This should solve your question
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// your code
return true;
}
return super.onKeyDown(keyCode, event);
}
I have two activity let A and B. A is listActivity and B is webViewActivity. Now in webview, when i back pressed then it works fine within webview but in last back pressed, it resides in activity B but I want to go back to Activity A (listActivity).
My code for back pressed
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webView.canGoBack()){
webView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
Thanks.
This may help you...
http://rickluna.com/wp/2014/04/disable-android-back-button-in-inappbrowser/
here it explains that you can disable back button in webview... so make this works in your logic.
#Override
public void onBackPressed() {
super.onBackPressed();
}
onbackpressed function call when you pressed back button then finish current activity.
In our application we have one parent linear layout which contains another linear layout as child.Child linear layout is nothing but treating as dialog box with fixed width and height. As expected if i click on back key of device entire activity is getting removed from stack. I just want if dialog box is open then only dialog box should get removed i.e. only child linear layout should be gone.How to dismiss only view not the activity on back key press?
Please do needful help.
Thanks,
AA.
You need to override Activity.onBackPressed() and do what you need to do:
#Override
public void onBackPressed() {
if (mDialogView.getVisibility() == View.VISIBLE) {
mDialogView.setVisibility(View.GONE);
} else {
super.onBackPressed();
}
}
Well, you could override onBackPressed to something like:
#Override
public void onBackPressed() {
if(!closeDialogsAndStuff()) {
super.onBackPressed();
}
}
private boolean closeDialogsAndStuff() {
/**
* check if dialogs should be closed and if so, close them and return true
* otherwise return false
* */
return true;
}
Implement onBackPressed method(), and inside that method dismiss your dialog
#Override
public void onBackPressed()
{
// code here to show dialog
super.onBackPressed(); // optional depending on your needs
dialog.dismiss();
}
You can ask if you have any further queries :)
put this code in your activity
public boolean onKeyDown(int keyCode, KeyEvent event) { //if user click back from device
if (keyCode == KeyEvent.KEYCODE_BACK) {
if(dialog.isShowing()) dialog.dismiss(); //dismiss dialog if shown
else finish(); //finish current activity
}
return true;
}
I have a problem in a very simple application.
I have a main activity, and on button click I open a second activity:
newEntryButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Intent intent = new Intent(MainActivity.this, NewSpendingActivity.class);
MainActivity.this.startActivity(intent);
return true;
}
});
When I close this activity (for example by tapping the back button) or by calling finish() the view loads once again. It only closes if I tap the back button again. What may be the cause of this?
It's probably because you're using an OnTouchListener, which triggers on press, move and release. Try using an OnClickListener instead.
This should not give you problems:
newEntryButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent activityChangeIntent = new Intent(MainActivity.this,NewSpendingActivity.class);
MainActivity.this.startActivity(activityChangeIntent);
}
});
I know we have back button in android to move us back on the previous form, but my team leader asked to put a back button functionality on button click
How can I do this?
You should use finish() when the user clicks on the button in order to go to the previous activity.
Button backButton = (Button)this.findViewById(R.id.back);
backButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
Alternatively, if you really need to, you can try to trigger your own back key press:
this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));
Execute both of these.
If you need the exact functionality of the back button in your custom button, why not just call yourActivity.onBackPressed() that way if you override the functionality of the backbutton your custom button will behave the same.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// your code here
return false;
}
return super.onKeyDown(keyCode, event);
}
layout.xml
<Button
android:id="#+id/buttonBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="finishActivity"
android:text="Back" />
Activity.java
public void finishActivity(View v){
finish();
}
Related:
Android - Return to calling Activity
Android: Go back to previous activity
If you are inside the fragment then you write the following line of code inside your on click listener,
getActivity().onBackPressed();
this works perfectly for me.
With this code i solved my problem.For back button paste these two line code.Hope this will help you.
Only paste this code on button click
super.onBackPressed();
Example:-
Button backButton = (Button)this.findViewById(R.id.back);
backButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
super.onBackPressed();
}
});
Well the answer from #sahhhm didn't work for me, what i needed was to trigger the backKey from a custom button so what I did was I simply called,
backAction.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MyRides.super.onBackPressed();
}
});
and it work like charm. Hope it will help others too.