【问题标题】:What is the best way to store Date in savedInstanceState?在 savedInstanceState 中存储日期的最佳方法是什么?
【发布时间】:2016-12-22 19:17:36
【问题描述】:

我正在使用 onSaveInstanceState 方法来存储和检索片段内的局部变量。 我的一个片段中有以下代码(简化):

private final SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy - HH:mm");
private Date myDate;

public Date getDate() { return myDate; }

public void setDate(Date date) { this.myDate = date; }

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.main, container, false);

    TextView tvDate = (TextView) rootView.findViewById(R.id.textview_date);

    if (savedInstanceState != null) {
        try {
            myDate = dateFormat.parse(savedInstance.State.getString("DATE"));
        catch (ParseException e) {
        }
    }

    //myDate is being set by a different fragment using the setDate method

    if (tvDate != null)
        tvDate.setText(dateFormat.format(myDate));

    return rootView;
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putString("DATE", dateFormat.format(myDate));
}

这样,当我的视图被破坏和重新创建时,我可以存储和检索日期值,例如通过旋转屏幕,这是主要问题。 这行得通,但我可以说这不是有效的。如果我像这样存储更多变量,则视图创建需要更长的时间(加载包含字符串和布尔值的 10 个变量需要几秒钟)。

我想知道是否有更好的方法来做到这一点。欢迎提出任何建议。

【问题讨论】:

  • 难以置信。十个变量不算什么。与此同时,你在做什么困难的事情?
  • 如果从Bundle 加载 10 个变量需要几秒钟时间,您的代码还有其他问题。

标签: android date time storage store


【解决方案1】:

DateSerializable。停止将其转换为字符串。直接放在Bundle,使用putSerializable(),通过getSerializable()检索。

如果这不能解决您的潜在性能问题,请使用开发工具(例如 IDE 中的方法跟踪)来确定您将时间花在哪里。

【讨论】:

  • 在执行 getSerializable() 时是否必须将其转换为 (Date) ?
  • @KaveeshKanwal:如果 IDE 要求您这样做,那么您就这样做。 :-)
【解决方案2】:

您的代码似乎不会导致性能问题,并且是在 Android 上存储状态的正常有效方式。我在下面的代码中添加了一些 cmets/adjustments,但是这可能会解决其他错误或加快处理速度。

    private static final String STATE_DATE = "state:date";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, e Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.main, container, false);

        TextView tvDate = (TextView) rootView.findViewById(R.id.textview_date);

        // Check if the savedInstanceSTate contains the date String key.
        if (savedInstanceState != null
                && savedInstanceState.containsKey(STATE_DATE)) {

            try {
                // Consider doing complex tasks like parsing etc. in the background.
                // Parsing a date shouldn't be the issue here but doing many things like this may 
                // be the cause of the performance issue. 
                myDate = dateFormat.parse(savedInstanceState.getString(STATE_DATE));
                catch(ParseException e){
                }
            }

            //myDate is being set by a different fragment using the setDate method

            if (tvDate != null)
                tvDate.setText(dateFormat.format(myDate));

            return rootView;

        }
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        // Remove the date formatter here. Try to format your date immediately when storing it in the variable.
        // When restoring your date from your savedInstanceState you're already formatting it.
        savedInstanceState.putString(STATE_DATE, myDate);
    }

【讨论】:

    【解决方案3】:

    Android 也使用 onSavedInstanceState() 方法来保存它的状态,比如 Activity 被销毁时的片段状态,还有一些小部件保存了它的状态,比如 EditText,我们不需要保存Edittext 的状态,Android 在 onSavedInstanceState() 的帮助下处理它。

    所以我只想告诉你它保存状态的有效方法。

    【讨论】:

      【解决方案4】:

      首先,就像其他人已经指出的那样,我认为十个变量不会引起问题。肯定有其他原因导致延迟。

      其次,一种更简单有效的方法是使用 date.getTime() long 值,而不是解析您的日期字符串并以同样的方式恢复它。

      以重申为代价,您的滞后是由于其他原因。您可以通过记录每个生命周期甚至完成并找出罪魁祸首所花费的时间来验证这一点。

      【讨论】:

        猜你喜欢
        • 2013-06-14
        • 2017-09-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-03
        • 2016-05-27
        相关资源
        最近更新 更多