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 click buttons
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:
In first activity:
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();
}
In second activity:
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();
}
In third activity:
public void onBackPressed() {
Intent myintent=new Intent(third.this,second.class);
startActivity(myintent);
super.onBackPressed();
}
Stop launching activities in the method
onBackPressed(I am assuming this is the method where you want to finish the current activity).So in the second and the third activities method
onBackPressedcallfinish(), just like the first activityonBackPressedmethod.