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.

http://developer.android.com/reference/android/app/Activity.html

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

     @Override
	protected void onPause() {
		super.onPause();
		A.this.finish();
	}

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.

     @Override
	protected void onPause() {
		super.onPause();
        // put some logic here 
		A.this.finish();
	}

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 🙂

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s