How to finish current activity in Android
Solution 1
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
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
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 4
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 5
Just call the finish()
method:
context.finish();
Related videos on Youtube
Shah
I am a mobile Application Developer. Working on iOS/Android Apps Development.
Updated on July 05, 2022Comments
-
Shah almost 2 years
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?
-
Ted Hopp over 11 years@AndreiBuneyeu - There is a general rule (stated here): "Do not access the Android UI toolkit from outside the UI thread". While the term "Android UI toolkit" isn't defined anywhere in the docs (that I could find), it seems reasonable to consider the activity lifecycle methods to be part of it. (Clearly, some
Activity
methods--likeActivity.runOnUiThread(Runnable)
--do not fall under this restriction. I agree with you that the docs are not as clear as they need to be on this subject.) -
Zhen almost 10 yearsWhit this parameter it ends the activity with startActivity and also with startActivityForResult. It bite me today ;D
-
nurettin almost 8 yearswhat finish method? I don't see any this.finish() method in the activity
-
blueware over 4 years
noHistory
is not the same asfinish()