How to Finish an Android Activity

Suppose you have two Activity

Activity A and Activity B

You want to navigate A to B and also want finish the previous Activity (in this case Activity A).
put this code inside your Activity A

A.this.finish(); // Activity_Name.this.finish(); 

So where you will put that line, well then you have to understand the life cycle of an Android Activity.

So i guess now you know, where you should put that line.

	protected void onPause() {

Special case: If you just put it like this, with out any checking, than your app will not act like normally, because in screen rotation onPause() is also called. So as a result your app will finish when user rotates the screen.

	protected void onPause() {
        // put some logic here 

enjoy 🙂

Well i edited the solution, it seems finishing the Activity in onStop() creates unknown bug like it calls the onDestroy() later, which creates serious bug in the code. So i move it to onPause() and its working great, thanks to Jamael Tanveer Vaia 🙂


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s