如何在 Android 中完成当前活动

发布于 2024-10-17 13:50:35 字数 848 浏览 5 评论 0原文

我有一个 Android 应用程序。我正在制作一个带有进度条的加载屏幕。

我在onCreate方法中输入了延迟。当计时器结束时,我想完成当前活动并开始新的活动。

它只是在调用 finish() 方法时给了我一个异常。

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();
    }
}

如何更改代码,使其在进度条完成时结束?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

不打扰别人 2024-10-24 13:50:35

如果您正在执行加载屏幕,只需将参数设置为不将其保留在活动堆栈中即可。在你的manifest.xml中,你定义你的活动:

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

并且在你的代码中,不再需要调用.finish()。只需执行 startActivity(i);

也无需将当前活动的实例保留在单独的字段中。您始终可以像 LoadingScreen.this.doSomething() 那样访问它,而不是 private LoadingScreen loadingScreen;

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;

冷心人i 2024-10-24 13:50:35

我尝试使用这个例子,但它失败了。每次我在处理程序中调用 finish()/ finishactivity() 时,都会遇到这个危险的 java.lang.IllegalAccess Exception。我不确定提出问题的人是如何工作的。

相反,我找到的解决方案是在您的活动中创建一个方法,例如

void kill_activity()
{ 
    finish();
}

从处理程序的 run 方法内部调用此方法。这对我来说就像一个魅力。希望这可以帮助任何遇到“如何从不同线程关闭活动?”的人。

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

愛放△進行李 2024-10-24 13:50:35

您需要从 UI 线程而不是后台线程调用 finish()。实现这一点的方法是声明一个 Handler 并要求 Handler 在 UI 线程上运行一个 Runnable。例如:

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();
    }
}

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();
    }
}
哆啦不做梦 2024-10-24 13:50:35

当您想要开始一项新活动并完成当前活动时,您可以执行以下操作:

API 11 或更高版本

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

API 10 或更低版本

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

我希望这可以帮助某人 =)

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

帅冕 2024-10-24 13:50:35

只需调用 finish() 方法即可:

context.finish();

Just call the finish() method:

context.finish();
傲影 2024-10-24 13:50:35

我正在做的是开始一项新活动,然后关闭当前活动。所以,请记住这个简单的规则:

finish()
startActivity<...>()

而不是

startActivity<...>()
finish()

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()
染火枫林 2024-10-24 13:50:35

我找到了很多答案,但没有一个是简单的......
我希望这能帮助你...

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

所以,
这里的逻辑非常简单,因为我们知道,在java中,我们编写的代码在try块中可能会出现异常,并在catch块中处理该异常,但在finally块中,我们编写的代码必须以任何成本执行(无论是异常是否发生)。

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

最初的梦 2024-10-24 13:50:35

您还可以使用: finishAffinity()

完成此活动以及当前任务中紧邻该活动的所有具有相同关联性的活动。

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.

亣腦蒛氧 2024-10-24 13:50:35

使用

.finish();

要完成活动,

首先给出上下文:

getApplicationContext.finish();

或者

context.finish();

您可以在任何您想要的地方调用它

按钮的OnClickListener

Button 或 Button 中

OnBackPressed();方法

如果你想完成应用程序然后使用

finishAffinity();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文