Reputation: 51
I'm making my first android application, and when I press the back button it shuts down the app, instead of going back to the previous activity. Does anyone know how I can fix this??
Thanks
This is my "Hoofdscherm" page, from here you can go to the "Acties" page
package com.WNF;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
public class Hoofdscherm extends Activity {
// aanroepen van een bundle, kan je elke naam geven die je maar wilt,
//zolang de bundle als de onCreate maar dezelfde naam hebben
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// de setContentView is niets meer dan de gegevens van de
//View ophalen uit de R.layout.naamvandeXML
// Onthoud goed dat je dezelfde XMLs voor meerdere pagina's
//kan gebruiken.
setContentView(R.layout.hoofdscherm);
Button b = (Button) findViewById(R.id.button1);
ImageButton i = (ImageButton) findViewById(R.id.imageButton1);
b.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Intent in = new Intent(Hoofdscherm.this,Acties.class);
startActivity(in);
finish(); //deze activity wordt gestopt
}
});
i.setOnClickListener(new OnClickListener(){
public void onClick(View g){
Intent ib = new Intent(Hoofdscherm.this,Acties.class);
startActivity(ib);
finish();
}
});
}
}
And this is the "Acties" page
package com.WNF;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
public class Acties extends Activity{
// aanroepen van een bundle, kan je elke naam geven die je maar wilt,
//zolang de bundle als de onCreate maar dezelfde naam hebben
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// de setContentView is niets meer dan de gegevens van de
//View ophalen uit de R.layout.naamvandeXML
// Onthoud goed dat je dezelfde XMLs voor meerdere pagina's
//kan gebruiken.
setContentView(R.layout.acties1);
getIntent();
}
}
Upvotes: 0
Views: 130
Reputation: 9839
you can override the behavior of the button by overriding the onKeyDown method in an activity, and using a simple if statment to find out if the key that was pressed was the back key
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
// your code here
return true;
else return false
return super.onKeyDown(keyCode, event);
}
and inside the statement just write the code that will call you're other activity.
EDIT I see you added your code, you've called finish() after calling your 2nd activity, that will kill your first activity, either remove the finish(), or summon the 1st activity via an intent from the 2nd activity.
Upvotes: 0
Reputation: 33534
Here is your Solution
Remove the finish()
method.
b.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Intent in = new Intent(Hoofdscherm.this,Acties.class);
startActivity(in);
finish(); // Remove this..and it will work as you want it to
}
});
finish() method avoids storing of the activity on the Back Stack
Upvotes: 0
Reputation: 15052
From your Hoofdscherm
Activity you call your Acties
Activity. And then in Acties
you press back and your application closes. Right?
What is happening is, ideally, from Acties
when you press back, it should go to Hoofdscherm
, but since you are calling finish();
in your Hoofdscherm
Activity, it no longer exists. Hence your application exits.
If you want to go back to Hoofdscherm
from Acties
, remove the finish()
call in your Hoofdscherm
Activity.
EDIT:
Here's a bit more about finish()
.
Remember - only call finish()
when you want to close your Activity, if you want to go back to it,don't call finish()
.
Upvotes: 1