I have a simple two screen app. The first screen presents the user with login options and the second screen is where they see the content upon login.
Once the user logs in, I set the key with Flow.get( view ).set( Screens.CHAT )
.
All is nice and dandy but I run into a problem that makes my view look like this:
This re-drawing of view happens when the user leaves the app by pressing home button and then comes back to it (basically after onRestart( )
). The un-populated RecyclerView
is added to the view hierarchy again.
My Dispatcher
logic is this:
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 );
}
}
//----------------------------------------------------------------------------------------------
What do I change in my Dispatcher
logic to avoid the same layout from being inflated and added again ?
The problem is that your
root
ViewGroup isn't getting cleared.Here's diff that I believe will fix your issue (
<
remove,>
add).The Flow repo has an example with something pretty close to what you already have.