简体   繁体   English

Fragment.onCreate()和Activity.onCreate()之间的关系

[英]Relationship between Fragment.onCreate() and Activity.onCreate()

Dear fellow developers, 亲爱的开发人员,

The relationship between a Fragment's onCreate() method and that of an Activity is not yet crystal clear to me. Fragment的onCreate()方法和Activity的关系尚不十分清楚。 I'm attaching a Fragment to an Activity like this. 我将片段附加到这样的活动。

//happens in the Activity:
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
    FragmentManager fragMan = this.getSupportFragmentManager();
    Fragment dataModelFragment = fragMan.findFragmentByTag("data_model");
    if (dataModelFragment == null) {
        dataModelFragment = new DataModelFragment();
        fragMan.beginTransaction().add(dataModelFragment,"data_model").commit();
    }
    System.out.println("Executing Activity.onCreate()");
}

My Fragment's onCreate() has a Sysout.println() for testing purposes to see when exactly it executes. 我的片段的onCreate()具有Sysout.println(),用于进行测试以查看其确切执行时间。 What makes me wonder is: why does the Activity's onCreate finish execution first, before even the Fragment's onCreate() is called? 让我感到奇怪的是:为什么在调用Fragment的onCreate()之前,Activity的onCreate首先完成执行? What happens is that the Sysout.println shown in the above code actually executes BEFORE the other Sysout.println, which is in the onCreate() of my Fragment. 发生的是,上面代码中显示的Sysout.println实际上在另一个Sysout.println之前执行,该Sysout.println在我的Fragment的onCreate()中。 I would expect the Fragment's onCreate() to be executed before I add it to the Activity via the fragment transaction. 我希望在通过片段事务将片段的onCreate()添加到Activity之前,先执行该片段的onCreate()。 Is it the way it is supposed to work? 它是应该工作的方式吗? Or is the problem what I read in the docs that the commit() method does not execute immediately but places the fragment transaction only in the queue? 还是我在文档中看到的问题不是commit()方法立即执行而是将片段事务仅放在队列中?

What I would like to achieve is this: 我想实现的是:

  • I'm using a singleton instance of a Fragment as a data model to be retained across activity lifecycles. 我正在使用Fragment的单例实例作为要在活动生命周期内保留的数据模型。
  • I'd like to add this fragment to the activity, then let this fragment perform its initialization (creating a database connection and loading the data into its data members) 我想将此片段添加到活动中,然后让该片段执行其初始化(创建数据库连接并将数据加载到其数据成员中)
  • Finally this data from the fragment should be used with AdapterViews in the Activity's View. 最后,片段中的此数据应与“活动”视图中的AdapterViews一起使用。

So the three steps: initialize fragment with data, attach fragment to activity, create adapters in the activity from the fragment's data. 因此,这三个步骤是:使用数据初始化片段,将片段附加到活动,根据片段的数据在活动中创建适配器。 My question: in which methods should these events occur to ensure the proper order of execution? 我的问题:这些事件应以哪种方法发生,以确保正确的执行顺序?

Many thanks! 非常感谢!

From the Android FragmentTransaction.Commit() docs : 来自Android FragmentTransaction.Commit() docs

Schedules a commit of this transaction. 安排此事务的提交。 The commit does not happen immediately; 提交不会立即发生; it will be scheduled as work on the main thread to be done the next time that thread is ready. 它将在下次线程准备好时安排为主线程上的工作进行安排。

If you want your transaction to be processed immediately call FragmentManager.ExecutePendingTransactions() from your UI thread. 如果要立即处理事务,请从UI线程调用FragmentManager.ExecutePendingTransactions()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM