调用同一类的onCreate方法 [英] calling the onCreate method from the same class

查看:211
本文介绍了调用同一类的onCreate方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以从相同的活动调用特定活动的onCreate方法?就像在点击一个按钮,我将创建一个新的标签,我想运行相同的code载再次当前活动的onCreate方法。

Can I call the onCreate method of a particular activity from the same activity ? Like upon clicking a button ,I will create a new tab and I want to run the same code as contained by the onCreate method of the current activity again.

推荐答案

活动#的onCreate()是一个生命周期方法和不应该由你自己的$ C直接调用$ C。如果你想要一块逻辑,要素出来的重用到它自己的方法,并调用同一个无论从的onCreate()并要重用它的地方。

Activity#onCreate() is a lifecycle method and should never be called directly by your own code. If you want to reuse a piece of logic, factor it out into its own method and call the same from both the onCreate() and the place where you want to reuse it.

在理想情况下,设置code,一个活动要求建立和初始化本身应该是只有一次在其一生中运行。所以,把东西还需要一个被称为按钮,单击那里,指着更大的问题与你的code。

Ideally, the setup code that an Activity requires to build and initialize itself should be run only once in its lifetime. So, placing something that also needs to be called on a button-click there, points at bigger problems with your code.

这篇关于调用同一类的onCreate方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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