0

I have developed a small android app with 3 activities. In first activity there has a button(Next_Button). If i click the button then the second activity will appear. In second activity there also a button which will forward to third activity after clicking it. In third activity there is a button (Home_Button). If i click this button(Home_Button) then first activity will appear. Now i want to kill second activity when i click the Home_Button in the third activity to make first activity visible. How can i do this? Please help.

Best wishes Md. Fazla Rabbi

3 Answers3

2

In third activity, write the below code for your Home button click event:

  btnHome.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent(this, firstActivity.class)
                 .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            }

        });

For example, for back key pressed, we can override onBackPressed function:

@Override
public void onBackPressed() {
 startActivity(new Intent(this, first firstActivity.class)
 .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 return;
}

Update:

For your reference, this one is the same as your requirement: Android Intent.FLAG_ACTIVITY_SINGLE_TOP AND Intent.FLAG_ACTIVITY_CLEAR_TOP,

And one more example: Go home feature with FLAG_ACTIVITY_CLEAR_TOP

Community
  • 1
  • 1
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • Hi Paresh Mayani, Many many thanks for your quick response. It helps me a lot. Now i have some sound clips which is loaded when the application is started i.e. when first_activity is started. Now my question is if i press the Home_button in the third activity according to your solution given above will it again load the sound clips? If not than ok but if yes then how can i solve this without reloading the sounds again and again i.e i want to load the sounds for once though i move to another activity. – Md.Fazla Rabbi opu Jun 03 '11 at 11:13
0

You don't need to "kill" your activity. In your third activity, if you start the first one with startActivity, you first activity will be shown.

The system will decide if your second activity (or your third) should be paused or destroyed.

Guillaume
  • 131
  • 2
-1

just call finish() method of the second activity on Home button click.

Dinesh Sharma
  • 11,533
  • 7
  • 42
  • 60