Md.Fazla Rabbi opu
Md.Fazla Rabbi opu

Reputation: 11

Problem in killing activity

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

Upvotes: 0

Views: 220

Answers (3)

Paresh Mayani
Paresh Mayani

Reputation: 128448

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

Upvotes: 2

Guillaume
Guillaume

Reputation: 131

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.

Upvotes: 0

Dinesh Sharma
Dinesh Sharma

Reputation: 11571

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

Upvotes: -1

Related Questions