In my app I have 3 activities and I use onBackPressed
for first, second and third activities.
My need is I want to go from first activity
-> second activity
-> third activity
and
From third activity
-> second activity
-> first activity
-> finsh()
I go first activity->second activity->third activity by Hawaii
My problems is when I press back key of emulator it comes in the following direction
Third activity
-> second activity
-> first activity
-> second activity
-> first activity
-> first activity
-> and finsh()
. Instead of finsh()
, my app goes to second activity
-> first activity
-> first activity
.
How to solve the problem. I do not know where I am wrong. Please help me. My code:
第一项活动:
Button b1 = (Button)findViewById(R.id.first);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myintent=new Intent(first.this,second.class);
startActivity(myintent);
}
});
......
public void onBackPressed() {
Log.e("main-back","main-back");
finish();
super.onBackPressed();
}
第二项活动:
Button b2 = (Button)findViewById(R.id.second);
Log.e("main2", "main2");
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myintent=new Intent(second.this,third.class);
startActivity(myintent);
}
});
........
@Override
public void onBackPressed() {
Intent myintent=new Intent(second.this,first.class);
startActivity(myintent);
super.onBackPressed();
}
第三项活动:
public void onBackPressed() {
Intent myintent=new Intent(third.this,second.class);
startActivity(myintent);
super.onBackPressed();
}