onSaveInstanceState是否总是在Fragment中保存我的成员变量?

时间:2016-01-21 04:21:27

标签: android android-fragments

我在调用onDestroyedView()时切换片段,将保留成员变量,并且当调用onDestroy()时,我可以从bundle中恢复它们,因为在onDestroy()之前调用onSaveInstanceState。但是现在我遇到了一个问题成员变量重置为初始值,我查了很多文档,但我不知道为什么会发生这种情况?

1 个答案:

答案 0 :(得分:1)

  

onSaveInstanceState是否总是将我的成员变量保存在Fragment中?

没有。 Bundle保存您作为参数传入的Bundle中的任何内容。然后,您可以从onCreate()或其他片段生命周期方法中的#Adapted from https://djangosnippets.org/snippets/2619/ @register.tag(name='counter') def do_counter(parser, token): """ Counter tag. Can be used to output and increment a counter. Usage: - {% counter %} to output and post-increment the counter variable - {% counter reset %} to reset the counter variable to 1 - {% counter last %} to access the last counter variable without incrementing """ try: tag_name, request = token.contents.split(None, 1) except ValueError: request = None if request == 'reset': reset = True last = False elif request == 'last': last = True reset = False else: reset = False last = False return CounterNode(reset, last) class CounterNode(template.Node): def __init__(self, reset, last): self.reset = reset self.last= last def render(self, context): # When initializing or resetting, set counter variable in render_context to 1. if self.reset or ('counter' not in context.render_context): context.render_context['counter'] = 1 # When resetting, we don't want to return anything if self.reset: return '' # When ask for last, return previous count if self.last: return context.render_context['counter']-1 counter = context.render_context['counter'] # Increment counter context.render_context['counter'] += 1 # Return counter number return counter 中获取值。