How to finish current activity in Android
Asked Answered
M

9

83

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?

Marvelofperu answered 15/2, 2011 at 7:11 Comment(0)
J
134

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;

Jake answered 15/2, 2011 at 7:19 Comment(2)
Whit this parameter it ends the activity with startActivity and also with startActivityForResult. It bite me today ;DPyrazole
noHistory is not the same as finish()Arteriovenous
F
54

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?".

Fuchsin answered 5/10, 2011 at 19:23 Comment(0)
T
14

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();
    }
}
Thaliathalidomide answered 15/2, 2011 at 7:20 Comment(1)
@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--like Activity.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.)Thaliathalidomide
F
14

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 =)

Formication answered 16/5, 2016 at 7:0 Comment(0)
I
7

Just call the finish() method:

context.finish();
Invariable answered 4/3, 2015 at 3:27 Comment(1)
what finish method? I don't see any this.finish() method in the activityGynecocracy
A
6

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()
Arteriovenous answered 18/2, 2020 at 10:27 Comment(0)
J
3

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).

Jefferson answered 31/12, 2018 at 11:59 Comment(0)
V
2

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.

Vargueno answered 13/6, 2018 at 17:0 Comment(0)
A
0

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();
Adduce answered 17/3, 2022 at 15:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.