2

私のカスタムビューでは、リストアしたいフィールドがいくつかあります。驚くべきことに、私はそのビューステートが復元されているかどうかを確認する方法

Examleクラス

class MyView extends View { 
    private Calendar calendar; 
    public MyView(Context context) { 
     super(context); 
     if(!restoring()) 
      calendar = Calendar.getInstance(); 
    } 

    @Nullable 
    @Override 
    protected Parcelable onSaveInstanceState() { 
     Bundle b = new Bundle(); 
     b.putParcelable("state", super.onSaveInstanceState()); 
     b.putLong("calendar", calendar.getTimeInMillis()); 
     return b; 
    } 

    @Override 
    protected void onRestoreInstanceState(Parcelable state) { 
     if (state instanceof Bundle) { 
      Bundle b = ((Bundle) state); 
      super.onRestoreInstanceState(b.getParcelable("state")); 
      calendar = Calendar.getInstance(); 
      calendar.setTimeInMillis(b.getLong("calendar")); 
     } 
    } 
} 

のための答えを見つけられませんでした私は、ビューが復元されるかどうかを判断するために行うことができますカスタムビュー内のいくつかのチェックがありますか? Activity

onCreate()

に渡さBundle savedInstanceStateがある私は、静的なブールを作ると考えるが、それは進むべき道ではありません。

答えて

2

ビューを復元するかどうかを判断するためのカスタムビュー内のチェックはありますか?

Viewクラス内は、このようなAPIは存在しませありません。

あなたに与えられている唯一の縫い目はnull状態で呼び出されることはありません」になるonRestoreInstanceState(Parcelable)、です。したがって、あなたがonRestoreInstanceState()になると、ビューが復元されていると仮定できます。それ以外の場合はではなく、です。

+0

私の論理を再構成する必要があるので、それは残念です。説明をありがとう – Tuby

関連する問題