本文介绍了的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文档中的相关章节:

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:

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

1403页,肝出来的..

09-06 18:59