シンプルな2画面アプリがあります。最初の画面にはログイン オプションが表示され、2 番目の画面にはログイン時にコンテンツが表示されます。
ユーザーがログインしたら、 でキーを設定しますFlow.get( view ).set( Screens.CHAT )。
すべてが素晴らしくダンディですが、ビューが次のようになる問題に遭遇しました。
このビューの再描画は、ユーザーがホーム ボタンを押してアプリを離れ、アプリに戻ったときに発生します (基本的には の後onRestart( ))。未入力RecyclerViewがビュー階層に再び追加されます。
私のDispatcher論理はこれです:
public void dispatch(Traversal traversal, TraversalCallback callback) {
Object dest = traversal.destination.top();
Object source = traversal.origin == null ? null : traversal.origin.top();
ViewGroup root = (ViewGroup) activity.findViewById(R.id.root);
if (traversal.origin != null) {
int childCount = root.getChildCount();
// Our container has a root view with an ImageView in it.
// If child count > 1, we need to remove the child at position = 1
// because ImageView is at position = 0
if (childCount > 1) {
// save state
traversal.getState(traversal.origin.top()).save(root.getChildAt(1));
// remove the added views
removeAllViewsAfter(root,0);
}
}
@LayoutRes int layout;
if(dest.equals(Screens.WELCOME)){
layout = R.layout.view_welcome;
}else if(dest.equals(Screens.CHAT)){
layout = R.layout.view_chat;
}else{
throw new IllegalArgumentException("Unrecognized Screen");
}
View incomingView = LayoutInflater
.from(traversal.createContext(dest, activity))
.inflate(layout, root, false);
traversal.getState( traversal.destination.top() ).restore(incomingView);
root.addView( incomingView );
callback.onTraversalCompleted();
}
//----------------------------------------------------------------------------------------------
private void removeAllViewsAfter(ViewGroup root, int index){
for( int i = root.getChildCount() - 1; i > index; i-- ){
root.removeViewAt( i );
}
}
//----------------------------------------------------------------------------------------------
Dispatcher同じレイアウトが膨張して再度追加されないようにするには、ロジックで何を変更すればよいですか?
