How to finish current activity in Android

前端 未结 8 1409
长发绾君心
长发绾君心 2020-11-28 03:19

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 finis

相关标签:
8条回答
  • 2020-11-28 03:43

    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;

    0 讨论(0)
  • 2020-11-28 03:51

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

    0 讨论(0)
提交回复
热议问题