i have two activities. On 1st activity i have button after click on button it will show dialog window and after click on dialog button i will jump to 2nd activity.
On 2nd acitivity i have custom button which is calling onBackPressed() method:
private void btnBackListener(){
btnBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
My Problem is when im on 2nd activity and i call onBackPressed(); it will jump into 1nd activity but also dialog is visibile. How to prevent showing that dialog after btnBack clicked in 2nd activity?
In your case, I think best option would be to close dialog before opening 2nd activity.
Related
In my code:
#Override
public void onBackPressed() {
}
It works fine, but if I call a dialog, for example
final Dialog dialogPopupGewonnen = new Dialog(Start.this, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
dialogPopup.setContentView(R.layout.popup);
I can use the back button (the back button close the dialog popup). But I want to disable the back button in all layouts and dialogs.
You should override onBackPressed in all of your activities and for Dialog you can use setCancelable(false) like:
final Dialog dialogPopupGewonnen = new Dialog(Start.this, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
dialogPopup.setContentView(R.layout.popup);
dialogPopup.setCancelable(false);
I am not sure this workaround is the correct way to achieve my goal of having a prompt text in a spinner. What happens with this application is the spinner navigates to another Activity via an Intent and when the user navigates back to the Main Activity with the spinner they have two ways back. One with a Button and a click event the other by clicking the device BACK button. I am trying to call the code in the click event from the method that manages the device BACK button
I do not know how to call the click event from the device BACK button Method
#Override
public void onBackPressed() {
Toast.makeText(getApplicationContext(),"Use BACK BUTTON\n\n"+"On the Screen",Toast.LENGTH_LONG).show();
// I want to call goBack(View view) from here
// +++++++++++++++++++++++++++++++++++++++++++
}
public void goBack(View view){
Intent i = new Intent( PageTwo.this, MainActivity.class );
startActivity( i );
}
The reason I use this Intent to navigate BACK to the Main Activity is it reloads the variables in the Spinner
It looks like goBack(View) is most likely from an onClick setup in your layout XML. Since you aren't using the view, just pass null:
#Override public void onBackPressed() {
goBack(null);
}
I don't know if I get you right, if you just want to go back to the activity which started another activity, you can just call finish() method of Activity class:
#Override
public void onBackPressed() {
finish();
}
finish() reference
I am trying to implement simple back button on activity to go to previous activity, but some weird behavior is happening when I am calling method finish() - the problem is I have to press back button TWO times? Why is this happening? In the back method i have only finish(). What is the other way to go to previous saved activity without instantiate a completely new Intent?
public void back1 (View view){
this.finish();
}
This is second try with the same result:
ImageButton buttonback = (ImageButton) findViewById(R.id.imageButton6);
buttonback.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onBackPressed();
}
});
You are most likely starting the activity twice.
When pressing back you are finishing one, and the other one is coming forward. Check your onCreate and onResume for anything that may be using intents or starting any activity
#Override
public void onBackPressed() {
super.onBackPressed();
}
add that to your code the super call closes the activity no need to call finish()
If you just want the devices back button to function, you dont need to override the onBackPressed method in Activity.
If you want to place a custom button in view and set back action to that button, then you need to write button clicklistener to the same
backbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
I have an app with a home screen and a bunch of buttons on it, and therefore listeners for each. After the user clicks on one of the buttons, a new layout is brought up and that layout has a back button with a listener.
The problem is that whenever the user presses the back button, the home screen layout is brought back up but none of the listeners work for the buttons anymore.
Here is some sample code:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // return to home screen
// sets up a listener for when the GCF main screen button is clicked.
GCFButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
setContentView(R.layout.gcf); // change to the gcf layout
Button back = (Button)findViewById(R.id.btnBack); // set up the back button in the gcf layout
back.setOnClickListener(new View.OnClickListener() // put a listener on back button
{
public void onClick(View v)
{
setContentView(R.layout.main); // return to home screen
}
});
Button GCFCalculate = (Button)findViewById(R.id.btnCalculate); // set up the gcf button in the gcf layout
GCFCalculate.setOnClickListener (new View.OnClickListener() // put listener on gcf button in gcf layout
{
public void onClick(View v)
{
// do stuff
}
});
}
});
}
You should not change a screen with setContentView(). Screens are changed in Android by starting a new Activity with startActivity(new Intent(...)) or with Fragments like recommended by Malimo (which is a bit more difficult to do but much nicer). You call two times setContentView() where one is destroying the other one.
in my opinion you should use fragments for your contentviews. so every fragment will be responsible for its contentview and can add listeners each time it is displayed...
http://developer.android.com/guide/components/fragments.html
I'm sure that there is a method built into Android that allows you to do this, but my first thought is recursion.
The problem is that your listeners are in the onCreate method, which means that after they are run through, they won't repeat. In the back button listener,
when you set the content view to be the home screen again, that won't set up the listeners again, that will just change the content view.
To fix that, you would have to call the onCreate method again, once the back button is clicked, because then it would run your whole code with all the listeners
from the home screen again.
I suggest putting all of the listeners in a listeners() method, and then calling that method recursively when needed. It would need to be called in onCreate(...),
as well as when the back button is clicked:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
listeners(); // set up all the listeners for the buttons
}
public void listeners()
{
setContentView(R.layout.main); // return to home screen
// sets up a listener for when the GCF main screen button is clicked.
GCFButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
setContentView(R.layout.gcf); // change to the gcf layout
Button back = (Button)findViewById(R.id.btnBack); // set up the back button in the gcf layout
back.setOnClickListener(new View.OnClickListener() // put a listener on back button
{
public void onClick(View v)
{
listeners(); // recursively call the listeners again to 'start over'
}
});
Button GCFCalculate = (Button)findViewById(R.id.btnCalculate); // set up the gcf button in the gcf layout
GCFCalculate.setOnClickListener (new View.OnClickListener() // put listener on gcf button in gcf layout
{
public void onClick(View v)
{
// do stuff
}
});
}
});
}
I would also recommend putting the back button listener in its own method, so that it can be called every time the layout is changed.
I have a dialog that pops up in my activity. I want it to finish the entire activity when the back button is pressed on the dialog. How would I do this?
set the onCancelListener for the dialog and call finish() when its called.
setOnCancelListener(new OnCancelListener()
{
#Override
public void onCancel(DialogInterface dialog) {
removeDialog(DIALOG_ALERT);
finish();
}
})