How to finish current activity in Android

AndroidActivity Finish

Android Problem Overview


I have an Android application. I am making a loading screen with a progress bar.

I entered a delay in the onCreate method. When the timer finishes, I want to finish the current activity and start a new one.

It just gives me an exception when it calls the finish() method.

public class LoadingScreen extends Activity{
    private LoadingScreen loadingScreen;
	Intent i = new Intent(this, HomeScreen.class);
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loading);
	       
        CountDownTimer timer = new CountDownTimer(10000, 1000) //10 second Timer
        {
            public void onTick(long l) 
            {

            }

            @Override
            public void onFinish() 
            {
                loadingScreen.finishActivity(0);
                startActivity(i);
            };
        }.start();
	}
}

How can I change the code so that it ends when the progress bar is done?

Android Solutions


Solution 1 - Android

If you are doing a loading screen, just set the parameter to not keep it in activity stack. In your manifest.xml, where you define your activity do:

<activity android:name=".LoadingScreen" android:noHistory="true" ... />

And in your code there is no need to call .finish() anymore. Just do startActivity(i);

There is also no need to keep a instance of your current activity in a separate field. You can always access it like LoadingScreen.this.doSomething() instead of private LoadingScreen loadingScreen;

Solution 2 - Android

I tried using this example but it failed miserably. Every time I use to invoke finish()/ finishactivity() inside a handler, I end up with this menacing java.lang.IllegalAccess Exception. i'm not sure how did it work for the one who posed the question.

Instead the solution I found was that create a method in your activity such as

void kill_activity()
{ 
    finish();
}

Invoke this method from inside the run method of the handler. This worked like a charm for me. Hope this helps anyone struggling with "how to close an activity from a different thread?".

Solution 3 - Android

You need to call finish() from the UI thread, not a background thread. The way to do this is to declare a Handler and ask the Handler to run a Runnable on the UI thread. For example:

public class LoadingScreen extends Activity{
	private LoadingScreen loadingScreen;
	Intent i = new Intent(this, HomeScreen.class);
	Handler handler;
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handler = new Handler();
        setContentView(R.layout.loading);

        CountDownTimer timer = new CountDownTimer(10000, 1000) //10seceonds Timer
        {
             @Override
             public void onTick(long l) 
             {

             }

             @Override
             public void onFinish() 
             {
            	 handler.post(new Runnable() {
            	     public void run() {
            	         loadingScreen.finishActivity(0);
            	         startActivity(i);
            	     }
                 });
             };
        }.start();
    }
}

Solution 4 - Android

When you want start a new activity and finish the current activity you can do this:

API 11 or greater

Intent intent = new Intent(OldActivity.this, NewActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);

API 10 or lower

Intent intent = new Intent(OldActivity.this, NewActivity.class);
intent.setFlags(IntentCompat.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);

I hope this can help somebody =)

Solution 5 - Android

Just call the finish() method:

context.finish();

Solution 6 - Android

What I was doing was starting a new activity and then closing the current activity. So, remember this simple rule:

finish()
startActivity<...>()

and not

startActivity<...>()
finish()

Solution 7 - Android

I found many answers but not one is simple... I hope this will help you...

try{
    Intent intent = new Intent(CurrentActivity.this, NewActivity.class);
    startActivity(intent);
} finally {
    finish();
}

so, Very simple logic is here, as we know that in java we write code that has some chances of exception in a try block and handle that exception in catch block but in finally block we write code that has to be executed in any cost (Either the exception comes or not).

Solution 8 - Android

You can also use: finishAffinity()

> Finish this activity as well as all activities immediately below it in the current task that have the same affinity.

Solution 9 - Android

Use

> .finish();

for finishing Activity

Give Context First Like:

getApplicationContext.finish();

Or

context.finish();

You Can Call it anywhere you want to like in

> OnClickListener of Button

of Button or in

> OnBackPressed(); Method

And If you want to finish the App then Use

finishAffinity();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShahView Question on Stackoverflow
Solution 1 - AndroidAlex OrlovView Answer on Stackoverflow
Solution 2 - AndroidSdrView Answer on Stackoverflow
Solution 3 - AndroidTed HoppView Answer on Stackoverflow
Solution 4 - AndroidDaniel S.View Answer on Stackoverflow
Solution 5 - AndroidThiagoView Answer on Stackoverflow
Solution 6 - AndroidbluewareView Answer on Stackoverflow
Solution 7 - AndroidHi-Tech GeniusView Answer on Stackoverflow
Solution 8 - AndroidCarlos RodríguezView Answer on Stackoverflow
Solution 9 - AndroidRehan KhanView Answer on Stackoverflow