Android 有一个演示者有 3 个片段
Android having a single presenter with 3 fragments
这更像是Android中MVP的概念性问题。我有 3 个片段,我正在尝试对所有 3 个片段使用 1 个演示者。
我对这些事情比较怀疑 -
a) 在片段的 onCreate()
中创建演示者的新实例,我喜欢这样 -
presenter = new MyPresenter();
presenter.setView(this);
因此打开 3 个片段将创建 3 个演示者实例。这是个好主意吗?
b) 1 个片段的视图必须在第 2 个和第 3 个片段中被覆盖,尽管它们没有被使用并且它们只是空方法
使我的第二个和第三个片段的代码长度变长。这是个好主意吗?
c) 虽然我在 onDestory()
片段 presenter.onDestroy()
中做了,但演示者会做 super.onDestory()
。
我拥有的 BasePresenter
会像这样破坏视图 -
public void onDestroy() {
this.view = null;
}
虽然视图被破坏了,但是presenetr垃圾被回收了吗?因为下一个片段将创建演示者的新实例而我不想要多个实例
运行.
So 3 fragments opened will create 3 instances of presenter. Is it a good idea?
你应该这样做,每个片段的每个演示者实例。但我建议您为特定的片段创建特定的演示者。清晰,易于改进和维护。
The view of 1 fragment must be overriden in 2nd and 3rd fragment although they are not used and they are just empty methods making my code length of 2nd and 3rd fragments long. IS this a good idea?
当然不,那是错误的代码。
Though the view is destroyed, is the presenetr garbage collected? Because next fragment will create new instance of presenter and I dont want several instances running.
它将被 GC 清除。您在每次 onCreate() 调用时都创建了新的演示者实例,因此这里有三个实例。它会很干净,因为它只与特定的一个片段相关联。
希望对您有所帮助!
这更像是Android中MVP的概念性问题。我有 3 个片段,我正在尝试对所有 3 个片段使用 1 个演示者。
我对这些事情比较怀疑 -
a) 在片段的 onCreate()
中创建演示者的新实例,我喜欢这样 -
presenter = new MyPresenter();
presenter.setView(this);
因此打开 3 个片段将创建 3 个演示者实例。这是个好主意吗?
b) 1 个片段的视图必须在第 2 个和第 3 个片段中被覆盖,尽管它们没有被使用并且它们只是空方法 使我的第二个和第三个片段的代码长度变长。这是个好主意吗?
c) 虽然我在 onDestory()
片段 presenter.onDestroy()
中做了,但演示者会做 super.onDestory()
。
我拥有的 BasePresenter
会像这样破坏视图 -
public void onDestroy() {
this.view = null;
}
虽然视图被破坏了,但是presenetr垃圾被回收了吗?因为下一个片段将创建演示者的新实例而我不想要多个实例 运行.
So 3 fragments opened will create 3 instances of presenter. Is it a good idea?
你应该这样做,每个片段的每个演示者实例。但我建议您为特定的片段创建特定的演示者。清晰,易于改进和维护。
The view of 1 fragment must be overriden in 2nd and 3rd fragment although they are not used and they are just empty methods making my code length of 2nd and 3rd fragments long. IS this a good idea?
当然不,那是错误的代码。
Though the view is destroyed, is the presenetr garbage collected? Because next fragment will create new instance of presenter and I dont want several instances running.
它将被 GC 清除。您在每次 onCreate() 调用时都创建了新的演示者实例,因此这里有三个实例。它会很干净,因为它只与特定的一个片段相关联。
希望对您有所帮助!