Android 活动更新
我正在从其内部调用一个活动 - 基本上我有一个新故事列表和两个过滤按钮,当单击这些按钮时,会重新启动该活动,并传递改变新闻故事的意图。
当我运行应用程序时,它可以工作,但有一秒钟我得到了旧的活动 UI,而应用程序从新的 xml 源读取,然后 UI 更新。有什么方法可以阻止这种情况发生并让活动重新启动冷状态。
这是我当前附加到 onclicklistener 的代码,
public void openFootballNews(View v) {
Intent i = new Intent(this, News_Landing.class); // News_landing class is the class this code is in
Bundle bundle = new Bundle();
bundle.putString("code", "football"); // this, if set, changes the xml feed to read
i.putExtras(bundle);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
this.onCreate(null); //this has halved the time the old UI is on the screen for but I cant get rid of it completely
startActivity(i);
}
任何帮助都会很棒,谢谢!
I am calling an activity from within itself - basically i've a list of new storys and two filter buttons that when clicked restart the activity with an intent passed that changes the news stories.
When i run the app it works, but for a second i get the old activity UI while the app reads from the new xml feed and then the UI updates. Is there any way to stop this from happening and get the activity to restart cold.
here's the code I am currently attaching to the onclicklistener
public void openFootballNews(View v) {
Intent i = new Intent(this, News_Landing.class); // News_landing class is the class this code is in
Bundle bundle = new Bundle();
bundle.putString("code", "football"); // this, if set, changes the xml feed to read
i.putExtras(bundle);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
this.onCreate(null); //this has halved the time the old UI is on the screen for but I cant get rid of it completely
startActivity(i);
}
any help would be great, thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从活动本身开始没有多大意义(除非您的目标是做一些深奥的递归事情;))。另外,我可能是错的,但我相信活动是保存在堆栈中的,这样当您在新闻报道之间切换时,您就会将一个又一个几乎相同的活动堆积起来。我同样认为手动调用
onCreate()
是一种不好的形式。需要查看您的所有代码,但我的猜测是您正在阅读您的提要并在
onCreate()
中创建列表,并且您最好的选择是将其重构为openNews (String sport)
方法,您可以在onCreate()
中调用一次该方法,并在侦听器中再次调用该方法。Starting an activity from itself doesn't make much sense (unless your aim is to do something esoterically recursive ;) ). Also, I may be mistaken, but I believe activities are kept in a stack so that as you flip between news stories, you're piling up one nearly-identical activity after another. I'd similarly think calling
onCreate()
by hand is bad form.Would need to see all of your code, but my guess is that you are reading your feed and creating your list inside
onCreate()
, and that your best bet is to refactor that into aopenNews(String sport)
method, which you call once inonCreate()
and again in your listener(s).