如何在 Android 中完成当前活动
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果您正在执行加载屏幕,只需将参数设置为不将其保留在活动堆栈中即可。在你的manifest.xml中,你定义你的活动:
并且在你的代码中,不再需要调用.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:
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 ofprivate LoadingScreen loadingScreen;
我尝试使用这个例子,但它失败了。每次我在处理程序中调用 finish()/ finishactivity() 时,都会遇到这个危险的
java.lang.IllegalAccess Exception
。我不确定提出问题的人是如何工作的。相反,我找到的解决方案是在您的活动中创建一个方法,例如
从处理程序的 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
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?".
您需要从 UI 线程而不是后台线程调用
finish()
。实现这一点的方法是声明一个 Handler 并要求 Handler 在 UI 线程上运行一个 Runnable。例如: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:当您想要开始一项新活动并完成当前活动时,您可以执行以下操作:
API 11 或更高版本
API 10 或更低版本
我希望这可以帮助某人 =)
When you want start a new activity and finish the current activity you can do this:
API 11 or greater
API 10 or lower
I hope this can help somebody =)
只需调用
finish()
方法即可:Just call the
finish()
method:我正在做的是开始一项新活动,然后关闭当前活动。所以,请记住这个简单的规则:
而不是
What I was doing was starting a new activity and then closing the current activity. So, remember this simple rule:
and not
我找到了很多答案,但没有一个是简单的......
我希望这能帮助你...
所以,
这里的逻辑非常简单,因为我们知道,在java中,我们编写的代码在try块中可能会出现异常,并在catch块中处理该异常,但在finally块中,我们编写的代码必须以任何成本执行(无论是异常是否发生)。
I found many answers but not one is simple...
I hope this will help you...
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).
您还可以使用: finishAffinity()
You can also use: finishAffinity()
使用
要完成活动,
首先给出上下文:
或者
您可以在任何您想要的地方调用它
Button 或 Button 中
如果你想完成应用程序然后使用
Use
for finishing Activity
Give Context First Like:
Or
You Can Call it anywhere you want to like in
of Button or in
And If you want to finish the App then Use