问题
I was reading through a lot of topics today regarding this. However, all the solutions that I had seen, failed to be a solution for me. I am new in Android development. My problem is that I had two buttons in my main page - namely PlayGame and Help. The PlayGame onClicked() will direct the user to the "difficulty page" where the user can choose Easy, Medium or Hard mode. What I want is that when the user pressed the Android back button in the "difficulty page", it will go back to the main menu (i.e. where the PlayGame and Help can be seen), because what is happening in my case is that it goes back to the Android menu. I finished my activity as shown below and I tried different approach in my two buttons (Help and PlayGame) but did not give me any luck.
This is my mainmenu.
package com.kfc;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
public class NewKFCActivity extends Activity {
/** Called when the activity is first created. */
ImageButton bPlay, bHelp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
bPlay = (ImageButton) findViewById(R.id.playGame);
bPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(v.getContext(), SelectDifficulty.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
NewKFCActivity.this.startActivity(intent);
}
});
bHelp = (ImageButton) findViewById(R.id.Help);
bHelp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(v.getContext(), HelpPageOne.class);
startActivity(intent);
finish();
}
});
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
回答1:
Don't finish your activity on click of the help menu. Once you call finish, the activity will be destroyed and removed from the activity stack (so when you press back, it's no longer the previous activity).
You should only finish the activity when it is truly done (i.e. the user has completed whatever it is that the activity was supposed to let them do). You need to think of the set of activities in your application as a stack. Each time you start an activity, it is placed at the top of the stack. If you want the back button to take you back to the previous activity, don't finish it (since each time you call finish it removes it from the stack). Every time the user presses back, the current activity is "popped" from the stack and the system resumes the activity below it. If there are no more activities from your app on the stack when the user presses back, your app is finished and the user goes back to the home screen.
回答2:
One problem is at the end of this activity. You finish the activity when you onPause. The activity is not on the stack anymore and you press back to load it again...you just can't load what's not on the stack anymore :)
regards,
回答3:
In the manifest.xml if there is
android:launchMode="singleInstance"
for the related activity just try to remove it, sync and try again.
来源:https://stackoverflow.com/questions/7980627/pressing-back-button-did-not-go-back-to-previous-activity-android