片段中的onCreate()
,onCreateView()
和onActivityCreated()
之间有什么区别?它们各自用于什么?
答案 0 :(得分:358)
<强>的onCreate():强>
onCreate()
中的Fragment
方法在Activity
的{{1}} 之后被调用,但在此onAttachFragment()
之前Fragment
。
在此方法中,您可以分配变量,获取onCreateView()
个附加内容,以及不涉及View层次结构的任何其他内容(即非图形初始化)。这是因为当Intent
的{{1}}未完成时可以调用此方法,因此尝试访问View层次结构可能会导致崩溃。
onCreateView():
调用Activity
后(在onCreate()
中),onCreate()
的{{1}}被调用。您可以指定Fragment
个变量,执行任何图形初始化。您应该从此方法返回Fragment
,这是主要的UI视图,但如果您的onCreateView()
不使用任何布局或图形,则可以返回View
(由默认如果你不覆盖)。
<强> onActivityCreated():强>
如名称所示,在View
的{{1}}完成后调用。它在Fragment
之后调用,主要用于最终初始化(例如,修改UI元素)。
总结......
......他们都在null
被召唤,但在不同时间被召唤
首先调用Activity
,进行任何非图形初始化。接下来,您可以在onCreate()
中分配和声明要使用的任何onCreateView()
变量。然后,使用Fragment
完成所有内容完成后要执行的任何最终初始化。
如果您想查看官方Android文档,可以在此处找到:
- onCreate()
- onCreateView()
- onActivityCreated()
Stack Overflow上也有一些略有不同但不太发达的问题/答案:
答案 1 :(得分:104)
对于那些寻找简洁,图片化答案的人来说:
https://hanaskuliah.wordpress.com/2015/12/07/android-5-development-part-6-fragment/
和
答案 2 :(得分:7)
import boto3
import json
import re
import pprint
import os
import pandas as pd
from decimal import *
import itertools
import sys
in_bucket_name = 'my_bucket'
s3 = boto3.client('s3', region_name='us-east-1')
amazon_comprehend_medical = boto3.client('comprehendmedical', region_name='us-east-1')
# Table for the Amazon Comprehend Medicaltable.
ddt_amazon_compmed = 'compmed_table'
# Table for the connected words list table.
ddt_connected_words = 'connected_words_list'
# Table for the source documents table.
ddt_source_documents = 'source_documents'
# Table for the document metadata table.
ddt_document_metadata = 'document_metadata'
# Create a reference to DynamoDB.
dynamoDBResource = boto3.resource('dynamodb', region_name = 'us-east-1')
# Create references to our DynamoDB tables.
table_compmed = dynamoDBResource.Table(ddt_amazon_compmed)
table_connected_words = dynamoDBResource.Table(ddt_connected_words)
table_source_documents = dynamoDBResource.Table(ddt_source_documents)
table_document_metadata = dynamoDBResource.Table(ddt_document_metadata)
def truncateTable(tableToTruncate, theProjectionExpression, funcBuildDeletionKey):
"""Given a reference to a DynamoDB table, a projection expression with the list
item attributes that make up the primary key, and a function that takes a
DynamoDB item and returns a key built from the item that can be used to delete
it, delete all of the records in that table using a repetitive batch delete
operation."""
scan = None
with tableToTruncate.batch_writer() as batch:
numDeletions = 0
while scan is None or 'LastEvaluatedKey' in scan:
if scan is not None and 'LastEvaluatedKey' in scan:
scan = tableToTruncate.scan(
ProjectionExpression=theProjectionExpression,
ExclusiveStartKey=scan['LastEvaluatedKey'],
)
else:
scan = tableToTruncate.scan(ProjectionExpression=theProjectionExpression)
for item in scan['Items']:
if numDeletions % 1000 == 0:
print(numDeletions)
deletionKey = funcBuildDeletionKey(item)
batch.delete_item(Key=deletionKey)
numDeletions = numDeletions + 1
def truncateAllTables():
"""Truncate all of our DynamoDB tables. Remember to update this function
if we add more tables."""
print("Truncating table: ", "table_compmed")
theProjectionExpression = 'ROWID'
funcBuildDeletionKey = lambda item: { 'ROWID': item['ROWID']}
truncateTable(table_compmed, theProjectionExpression, funcBuildDeletionKey)
print("Truncating table: ", "table_connected_words")
theProjectionExpression = 'n_gram, connected_word'
funcBuildDeletionKey = lambda item: { 'n_gram': item['n_gram'], 'connected_word': item['connected_word']}
truncateTable(table_connected_words, theProjectionExpression, funcBuildDeletionKey)
print("Truncating table: ", "table_source_documents")
theProjectionExpression = 'n_gram, source_document_id'
funcBuildDeletionKey = lambda item: { 'n_gram': item['n_gram'], 'source_document_id': item['source_document_id']}
truncateTable(table_source_documents, theProjectionExpression, funcBuildDeletionKey)
print("Truncating table: ", "table_document_metadata")
theProjectionExpression = 'paper_id'
funcBuildDeletionKey = lambda item: { 'paper_id': item['paper_id']}
truncateTable(table_document_metadata, theProjectionExpression, funcBuildDeletionKey)
print("Table truncation finished.")
现在不建议作为片段Version 1.3.0-alpha02
现在不建议使用onActivityCreated()方法。代码触摸 片段的视图应在onViewCreated()中完成(这称为 紧接onActivityCreated())和其他初始化代码之前 应该在onCreate()中。要专门接收回调 活动的onCreate()已完成,则LifeCycleObserver应该是 在onAttach()中注册了活动的生命周期,并被删除了一次 收到onCreate()回调。
可以找到详细信息here