在应用程序中返回按钮
我需要编写一个按钮以返回主应用程序活动。
public void onGotoMainActivity(View View)
{
Intent intent = new Intent(View.getContext(), MainActivity.class);
this.startActivity(intent);
}
Main 活动已经开始并且尚未被销毁。所以我不认为这将是一个“新”意图,也不应该“开始活动”?难道它不应该只是让主要活动重新聚焦吗?
I need to code a button to go BACK to the main application activity.
public void onGotoMainActivity(View View)
{
Intent intent = new Intent(View.getContext(), MainActivity.class);
this.startActivity(intent);
}
The Main activity is already started and has not been destroyed. So I don't think this would be a "new" intent nor should it "start activity"? Shouldn't it merely call the main activity back to focus?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您应该设置
FLAG_ACTIVITY_CLEAR_TOP
,这样它就不会启动Activity
的新实例,而是清除堆栈顶部的所有Activities
并传递意图到(现在在顶部)带有新Intent
的Activity
you should set
FLAG_ACTIVITY_CLEAR_TOP
so instead of launching a new instance of theActivity
it will clear allActivities
on the top of the stack and deliver the intent to (on top now)Activity
with a newIntent
您也可以使用这个。
You can use this also.
只需在
onClick()
中使用finish()
即可,或者如果您有超过 1 个活动,则添加此
just use
finish()
in theonClick()
or add this if you're more than 1 activity in
你的意思是模拟后退按钮吗?
Do you mean to simulate a back button?
这对我来说一切都清楚了。
谢谢大家让我走上正轨。活动的生命周期无疑是有价值的信息。
This cleared it all up for me.
Thank you everyone for getting me on the right track. The activities' life cycle was defiantly valuable information.