的onCreate总是叫,如果导航回意图 [英] onCreate always called if navigating back with intent

查看:227
本文介绍了的onCreate总是叫,如果导航回意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为HomeActivity的活动,有一个SurfaceView,并显示相机preVIEW图片。这项活动是安静的沉重,感觉很慢,如果您要启动/重新启动它。

所以,我做了一些调查,发现,不知何故总是onCreate方法被调用。在我看来,这不应该发生,如果活动已经开始了吗?

借助文档说: 第一次创建活动时调用。这是你应该做的所有的正常静态设置:创建视图,数据绑定到列表等等。这种方法还提供了一个Bundle含有活性的previously冻结状态,如果有的话。 总是跟着ONSTART()。

下面是方法,处理回去:

 保护无效gotoHome(){
    最终意向意图=新的意图(SomeOtherActivity.this,HomeActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(意向);
}
 

编辑:

下面是我如何离开HomeActivity ...没什么特别的:

 最终意图I =新的意图(HomeActivity.this,SomeOtherActivity.class);
startActivity(ⅰ);
 

解决方案

是的,当你想回到HomeActivity,你需要使用这些标志:

  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
 

下面是从Intent.FLAG_ACTIVITY_CLEAR_TOP文档中的相关章节:

  

活性B在上面的例子中,当前正在运行的实例将   要么接受新的意图,你已经开始在这里的   onNewIntent()方法,或可本身完成,重新开始与新的   意图。如果它已经宣布启动模式是多(中   默认设置),尚未设置FLAG_ACTIVITY_SINGLE_TOP在同一   意图,那么它将被完成并重新创建;对于所有其他发射   模式或者FLAG_ACTIVITY_SINGLE_TOP被设置,那么这个意向会   传递到当前实例的onNewIntent()。

I have an activity called HomeActivity that has a SurfaceView and shows a camera preview picture. This activity is quiet heavy and feels slow if you are starting/restarting it.

So I made some investigations and found out, that somehow always the onCreate method is being called. In my opinion this should not happen if the Activity has already been started?

The documentation says : Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state, if there was one. Always followed by onStart().

Here is the method, that handles going back:

protected void gotoHome() {
    final Intent intent = new Intent(SomeOtherActivity.this, HomeActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
}

Edit:

Here is how I am leaving HomeActivity ... nothing special:

final Intent i = new Intent(HomeActivity.this, SomeOtherActivity.class);
startActivity(i);

解决方案

Yes, when you want to return to the HomeActivity, you need to use these flags:

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);

Here's the relevant section from the documentation on Intent.FLAG_ACTIVITY_CLEAR_TOP:

The currently running instance of activity B in the above example will either receive the new intent you are starting here in its onNewIntent() method, or be itself finished and restarted with the new intent. If it has declared its launch mode to be "multiple" (the default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same intent, then it will be finished and re-created; for all other launch modes or if FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be delivered to the current instance's onNewIntent().

这篇关于的onCreate总是叫,如果导航回意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆