Skip to content

Commit

Permalink
Retain backstack on rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
lexs committed Sep 11, 2013
1 parent 5815148 commit 6b7b4d9
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions sample/src/main/java/com/example/flow/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import static android.view.MenuItem.SHOW_AS_ACTION_ALWAYS;

public class MainActivity extends Activity implements Flow.Listener {
private static final String BUNDLE_BACKSTACK = "backstack";

@InjectView(R.id.container) FrameLayout containerView;

private MenuItem friendsMenu;
Expand All @@ -41,13 +43,18 @@ protected void onCreate(Bundle savedInstanceState) {
final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);

flow = new Flow(Backstack.single(new App.ConversationList()), this);
flow = new Flow(getInitialBackstack(savedInstanceState), this);
activityGraph = ObjectGraph.create(new ActivityModule());

invalidateOptionsMenu();
go(flow.getBackstack(), Flow.Direction.FORWARD);
}

@Override protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);

outState.putParcelable(BUNDLE_BACKSTACK, flow.getBackstack());
}

@Override public boolean onCreateOptionsMenu(Menu menu) {
friendsMenu = menu.add("Friends")
.setShowAsActionFlags(SHOW_AS_ACTION_ALWAYS)
Expand Down Expand Up @@ -89,6 +96,14 @@ protected void onCreate(Bundle savedInstanceState) {
friendsMenu.setVisible(!hasUp);
}

private Backstack getInitialBackstack(Bundle savedInstanceState) {
if (savedInstanceState != null) {
return savedInstanceState.getParcelable(BUNDLE_BACKSTACK);
} else {
return Backstack.single(new App.FriendList());
}
}

private View getView(Screen screen) {
ObjectGraph graph = activityGraph.plus(screen);
Context scopedContext = new ScopedContext(this, graph);
Expand Down

0 comments on commit 6b7b4d9

Please sign in to comment.