Rakesh Gondaliya
Rakesh Gondaliya

Reputation: 1050

Android : Finish Activity from other Activity

I am having application in which i am using menu , on tap on menu item i am redirecting to specified activity.

I want to make sure that when i am redirected to another menu item my current all activity should be finished to reduce the stackflow of the activity and better performance.

So when i tap on back from my tapped activity from the selected menu activity i should be redirected to another activity and finish current activity.

So i am wondering is there any way by which i can finish another activity from my current activity. Or should i override the OnKeyDown Method..

Any help on this

Thanks in advance

Upvotes: 1

Views: 3989

Answers (2)

Diwas Poudel
Diwas Poudel

Reputation: 847

Intent i=new Intent (currentclass.this , nextclass.class);

startActivity(i);

finish();

Use finish() as shown above ,and here we are redirectin to nextclass.class so all activity of currentclass should be finished to reduce the stack over flow ..
and mind it finish() calls onDestroy() .and onDestroy() is executed...and destroy current activity(in my case currentclass)..

also mind it. onDestroy() isn't a destructor (as in c++) .It doesn't actually destroy the object of current activity... That's it....

Upvotes: 0

Guido
Guido

Reputation: 47665

You can do:

Intent intent = new Intent(....) ; // intent to launch the new activity
// fire intent
finish(); // finish current activity

Upvotes: 3

Related Questions