Share
Embed
IllegalStateException: Can not perform this action after onSaveInstanceState
There is one thing we all agree about Fragments -- it is easy to fall into problems with them. They were created for a noble reason, but using them involves effort because they can behave in unexpected ways.
When our team got a chance to refactor a legacy codebase, we decided to drop the traditional Android navigation pattern, in favour of a ViewGroup-driven UI stack with a single Activity. This talk is an exploration of our journey so far.
Saket Narayan
April 21, 2018
Transcript
106 slides
-
Introduced in Android 3.0 “An important goal for Android 3.0
is to make it easier for developers to write applications that can scale across a variety of screen sizes, beyond the facilities already available in the platform” - 2011 -
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1109)
at android.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:399) at android.app.Activity.onBackPressed(Activity.java:2066) at android.app.Activity.onKeyDown(Activity.java:1962) at android.view.KeyEvent.dispatch(KeyEvent.java:2482) at android.app.Activity.dispatchKeyEvent(Activity.java:2274) at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1668) at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112) at android.app.Activity.dispatchKeyEvent(Activity.java:2269) at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1668) at android.view.ViewRoot.deliverKeyEventPostIme(ViewRoot.java:2851) at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2824) at android.view.ViewRoot.handleMessage(ViewRoot.java:2011) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:132) at android.app.ActivityThread.main(ActivityThread.java:4025) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:491) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) at dalvik.system.NativeStart.main(Native Method) -
It’s too easy to fail Race conditions are difficult to
avoid with asynchronous lifecycle -
Too many gotchas @Override void onActivityResult(int requestCode, int resultCode, Intent
data) { if (resultCode == RESULT_OK) { SuccessDialog.show(); } } -
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1109)
at android.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:399) at android.app.Activity.onBackPressed(Activity.java:2066) at android.app.Activity.onKeyDown(Activity.java:1962) at android.view.KeyEvent.dispatch(KeyEvent.java:2482) at android.app.Activity.dispatchKeyEvent(Activity.java:2274) at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1668) at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112) at android.app.Activity.dispatchKeyEvent(Activity.java:2269) at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1668) at android.view.ViewRoot.deliverKeyEventPostIme(ViewRoot.java:2851) at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2824) at android.view.ViewRoot.handleMessage(ViewRoot.java:2011) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:132) at android.app.ActivityThread.main(ActivityThread.java:4025) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:491) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) at dalvik.system.NativeStart.main(Native Method) -
Too many gotchas @Override void onActivityResult(int requestCode, int resultCode, Intent
data) { if (resultCode == RESULT_OK) { pendingResumeRunnable = () -> { SuccessDialog.show(); }; } } @Override void onResume() { pendingResumeRunnable.run(); } -
Too many gotchas @Override void onActivityResult(int requestCode, int resultCode, Intent
data) { if (resultCode == RESULT_OK) { lifecycle().onResume() .take(1) .subscribe(o -> { SuccessDialog.show(); }); } } -
Too many gotchas class Activity { @BindView(R.id.text) TextView textView; @Override
void onCreate(Bundle s) { super.onCreate(s); setContentView(...); ButterKnife.bind(this); showFragment(...); } void setText(String text) { textView.setText(text); } } class Fragment { @Override void onAttach(Activity a) { a.setText("Blrdroid"); } } -
Too many gotchas class Activity { @BindView(R.id.text) TextView textView; @Override
void onCreate(Bundle s) { super.onCreate(s); setContentView(...); ButterKnife.bind(this); showFragment(...); } void setText(String text) { textView.setText(text); } } class Fragment { @Override void onAttach(Activity a) { a.setText(“Blrdroid"); } } -
Too many gotchas class Activity { @BindView(R.id.text) TextView textView; @Override
void onCreate(Bundle s) { super.onCreate(s); setContentView(...); ButterKnife.bind(this); showFragment(...); } void setText(String text) { textView.setText(text); } } class Fragment { @Override void onAttach(Activity a) { a.setText("Blrdroid"); } } -
Too many gotchas class Activity { @BindView(R.id.text) TextView textView; @Override
void onCreate(Bundle s) { super.onCreate(s); setContentView(...); ButterKnife.bind(this); showFragment(...); } void setText(String text) { textView.setText(text); } } class Fragment { @Override void onAttach(Activity a) { a.setText(“Blrdroid"); } } -
Too many gotchas class Activity { @BindView(R.id.text) TextView textView; @Override
void onCreate(Bundle s) { super.onCreate(s); setContentView(...); ButterKnife.bind(this); showFragment(...); } void setText(String text) { textView.setText(text); } } class Fragment { @Override void onAttach(Activity a) { a.setText(“Blrdroid"); } } NullPointerException! -
Too many gotchas Fragments are not resumed when Activity#onResume() is
called. Use Activity#onResumeFragments() instead. -
Fragments are like Bhagwan An extra layer of uncertainty in
life isn’t worth it. -
Fragments are like Bhagwan An extra layer of uncertainty in
life isn’t worth it. - Saket Narayan -
Basic necessities • Lifecycle • Backstack • State persistence •
Start a screen for result • Screen change animations • Nested screens • Dialog lifecycle and callbacks -
Options • Conductor (Blueline) • Flow (Square) • Scoop (Lyft)
• Magellan (Wealthfront) • RIBs (Uber) -
Why not Conductor Designed as a drop-in replacement for fragments.
public class HomeFragment extends Fragment { @Override View onCreateView(LayoutInflater inflater, ViewGroup container) { View view = inflater.inflate(R.layout.home, container,...); return view; } } -
Why not Conductor Designed as a drop-in replacement for fragments.
public class HomeController extends Controller { @Override View onCreateView(LayoutInflater inflater, ViewGroup container) { View view = inflater.inflate(R.layout.home, container,...); return view; } } -
Why not Conductor Designed as a drop-in replacement for fragments.
void startActivityForResult(...) void requestPermissions(...) void setHasOptionsMenu(...) void onCreateOptionsMenu(...) -
Why not Conductor It is not simple. It feels as
exhaustive as Fragments. -
Why not Conductor It is not simple. It feels as
exhaustive as Fragments. void onContextAvailable() void onContextUnavailable() void onSaveViewState(...) void onSaveInstanceState(...) -
Why Flow Is a library for navigating between scopes (and
not screens) Billing Dashboard User preferences -
Why Flow Scopes can be nested Logged in Billing Dashboard
User preferences Search -
Why Flow Pro: Extremely simple. Flow is just a back-stack
for scopes. Con: No batteries included. Flow does not even inflate Views. -
Options • Conductor (Blueline) • Flow (Square) • Scoop (Lyft)
• Magellan (Wealthfront) • RIBs (Uber) -
Setup public DashboardScreenKey implements ScreenKey { @LayoutRes int layoutResId() {
return R.layout.dashboard; } } -
Setup public PreferencesScreenKey implements ScreenKey { @LayoutRes int layoutResId() {
return R.layout.preferences; } } -
Setup class ScreenRouter { void push(ScreenKey key) { flow.push(key); }
void pop() { flow.goBack(); } ... } -
Setup class TheActivity { @Inject ScreenRouter screenRouter; @Override void onAttachBaseContext(Context
c) { super.onAttachBaseContext(screenRouter.install(c)); } @Override void onBackPressed() { BackPressCallback callback = screenRouter.pop(); if (!callback.popped()) { super.onBackPressed(); } } } -
Screen routing class DashboardScreen extends FrameLayout { public static final
ScreenKey KEY = DashboardScreen.Key.create(); public DashboardScreen(Context context, AttributeSet attrs) { super(context, attrs); } } -
Screen routing class DashboardScreen extends FrameLayout { public static final
ScreenKey KEY = DashboardScreen.Key.create(); public DashboardScreen(Context context, AttributeSet attrs) { super(context, attrs); } } -
Screen routing class LogInScreen extends FrameLayout { @Inject ScreenRouter screenRouter;
void openDashboard() { screenRouter.push(DashboardScreen.KEY); } } -
Screen routing class LogInScreen extends FrameLayout { @Inject ScreenRouter screenRouter;
void openDashboard() { screenRouter.push(DashboardScreen.KEY); } } -
Screen routing class LogInScreen extends FrameLayout { @Inject ScreenRouter screenRouter;
void openDashboard() { screenRouter.push(DashboardScreen.KEY_BUILDER .theme(Theme.DARK) .key1(“value1”) .key2("value2") .build()); } } -
Screen routing class DashboardScreen extends FrameLayout { @Inject ScreenRouter screenRouter;
void onAttachedToWindow() { super.onAttachedToWindow(); DashboardScreen.Key key = screenRouter.key(this); applyTheme(key.theme()); } } -
Screen routing class DashboardScreen extends FrameLayout { @Inject ScreenRouter screenRouter;
void onAttachedToWindow() { super.onAttachedToWindow(); DashboardScreen.Key key = screenRouter.key(this); applyTheme(key.theme()); } } -
Screen routing <?xml version="1.0" encoding="utf-8"?> <is.uncommon.DashboardScreen> android:layout_width="match_parent" android:layout_height="match_parent"> <!-- Child
Views will go here.--> </is.uncommon.DashboardScreen> -
Inflating screens @Override void changeKey(State outgoing, State incoming, Direction direction,
...) { if (frame.getChildCount() > 0) { View outgoingView = frame.getChildAt(0); if (outgoing != null) { outgoing.save(outgoingView); } frame.removeAllViews(); } View incomingView = inflate(layoutResIdForKey(incoming), ...); frame.addView(inView); incoming.restore(inView); } -
Inflating screens @Override void changeKey(State outgoing, State incoming, Direction direction,
...) { if (frame.getChildCount() > 0) { View outgoingView = frame.getChildAt(0); if (outgoing != null) { outgoing.save(outgoingView); } frame.removeAllViews(); } View incomingView = inflate(layoutResIdForKey(incoming), ...); frame.addView(inView); incoming.restore(inView); } -
Inflating screens @Override void changeKey(State outgoing, State incoming, Direction direction,
...) { if (frame.getChildCount() > 0) { View outgoingView = frame.getChildAt(0); if (outgoing != null) { outgoing.save(outgoingView); } frame.removeAllViews(); } View incomingView = inflate(layoutResIdForKey(incoming), ...); frame.addView(inView); incoming.restore(inView); } -
Inflating screens @Override void changeKey(State outgoing, State incoming, Direction direction,
...) { if (frame.getChildCount() > 0) { View outgoingView = frame.getChildAt(0); if (outgoing != null) { outgoing.save(outgoingView); } frame.removeAllViews(); } View incomingView = inflate(layoutResIdForKey(incoming), ...); frame.addView(inView); incoming.restore(inView); } -
Inflating screens @Override void changeKey(State outgoing, State incoming, Direction direction,
...) { if (frame.getChildCount() > 0) { View outgoingView = frame.getChildAt(0); if (outgoing != null) { outgoing.save(outgoingView); } frame.removeAllViews(); } View incomingView = inflate(layoutResIdForKey(incoming), ...); frame.addView(inView); incoming.restore(inView); } -
Inflating screens @Override void changeKey(State outgoing, State incoming, Direction direction,
...) { if (frame.getChildCount() > 0) { View outgoingView = frame.getChildAt(0); if (outgoing != null) { outgoing.save(outgoingView); } frame.removeAllViews(); } View incomingView = inflate(layoutResIdForKey(incoming), ...); frame.addView(inView); incoming.restore(inView); } -
Inflating screens @Override void changeKey(State outgoing, State incoming, Direction direction,
...) { if (frame.getChildCount() > 0) { View outgoingView = frame.getChildAt(0); if (outgoing != null) { outgoing.save(outgoingView); } frame.removeAllViews(); } View incomingView = inflate(layoutResIdForKey(incoming), ...); frame.addView(inView); incoming.restore(inView); } -
Screen routing FYI: changeKey() gets called on every app resume.
Make sure you only change Views when needed. https://github.com/square/flow/issues/173 -
Sending screen results class DashboardScreen extends FrameLayout { void onAttachedToWindow()
{ gifButton.setOnClickListener(o -> screenRouter.push(GifPickerScreen.KEY); ); screenRouter.streamResults() .ofType(GifPickerResult.class) .takeUntil(lifecycle.onDetach()) .subscribe(result -> { gifImageView.setImage(result.gif()); }); } } -
Sending screen results class DashboardScreen extends FrameLayout { void onAttachedToWindow()
{ gifButton.setOnClickListener(o -> screenRouter.push(GifPickerScreen.KEY); ); screenRouter.streamResults() .ofType(GifPickerResult.class) .takeUntil(lifecycle.onDetach()) .subscribe(result -> { gifImageView.setImage(result.gif()); }); } } -
Sending screen results class GifPickerScreen extends FrameLayout { void onGifSelected(Gif
gif) { GifPickerResult result = GifPickerResult.create(gif); screenRouter.sendResultAndPop(result); } } -
Sending screen results class GifPickerScreen extends FrameLayout { void onGifSelected(Gif
gif) { GifPickerResult result = GifPickerResult.create(gif); screenRouter.sendResultAndPop(result); } } -
Sending screen results class DashboardScreen extends FrameLayout { void onAttachedToWindow()
{ gifButton.setOnClickListener(o -> screenRouter.push(GifPickerScreen.KEY); ); screenRouter.streamResults() .ofType(GifPickerResult.class) .takeUntil(lifecycle.onDetach()) .subscribe(result -> { gifImageView.setImage(result.gif()); }); } } -
Sending screen results class DashboardScreen extends FrameLayout { void onAttachedToWindow()
{ gifButton.setOnClickListener(o -> screenRouter.push(GifPickerScreen.KEY); ); screenRouter.streamResults() .ofType(GifPickerResult.class) .takeUntil(lifecycle.onDetach()) .subscribe(result -> { gifImageView.setImage(result.gif()); }); } } Observable<Result> -
Sending screen results class DashboardScreen extends FrameLayout { void onAttachedToWindow()
{ gifButton.setOnClickListener(o -> startActivityForResult(GifPickerActivity.intent(), REQ_CODE_GIF); ); screenRouter.streamResults() .ofType(ActivityResult.class) .filter(result -> result.requestCode() == REQ_CODE_GIF) .flatMap(result -> GifPickerActivity.extractGif(result.intentData()) .takeUntil(lifecycle.onDetach()) .subscribe(gif -> { gifImageView.setImage(gif); }); } } -
Sending screen results class DashboardScreen extends FrameLayout { void onAttachedToWindow()
{ gifButton.setOnClickListener(o -> startActivityForResult(GifPickerActivity.intent(), REQ_CODE_GIF); ); screenRouter.streamResults() .ofType(ActivityResult.class) .filter(result -> result.requestCode() == REQ_CODE_GIF) .flatMap(result -> GifPickerActivity.extractGif(result.intentData()) .takeUntil(lifecycle.onDetach()) .subscribe(gif -> { gifImageView.setImage(gif); }); } } -
Sending screen results class DashboardScreen extends FrameLayout { void onAttachedToWindow()
{ gifButton.setOnClickListener(o -> startActivityForResult(GifPickerActivity.intent(), REQ_CODE_GIF); ); screenRouter.streamResults() .ofType(ActivityResult.class) .filter(result -> result.requestCode() == REQ_CODE_GIF) .flatMap(result -> GifPickerActivity.extractGif(result.intentData()) .takeUntil(lifecycle.onDetach()) .subscribe(gif -> { gifImageView.setImage(gif); }); } } -
Sending screen results class TheActivity extends Activity { @Override void
onActivityResult(int requestCode, int resultCode, Intent data) { screenRouter.sendResult(ActivityResult.create(requestCode, resultCode, data)); } } -
Sending screen results class DashboardScreen extends FrameLayout { void onAttachedToWindow()
{ gifButton.setOnClickListener(o -> startActivityForResult(GifPickerActivity.intent(), REQ_CODE_GIF); ); screenRouter.streamResults() .ofType(ActivityResult.class) .filter(result -> result.requestCode() == REQ_CODE_GIF) .flatMap(result -> GifPickerActivity.extractGif(result.intentData()) .takeUntil(lifecycle.onDetach()) .subscribe(gif -> { gifImageView.setImage(gif); }); } } -
Life is synchronous again DashboardScreen dashboardScreen = inflate(...); dashboardScreen.title.setText(...); class
DashboardScreen { TextView title; @Override void onFinishInflate() { title = findViewById(...); } } -
Life is synchronous again DashboardScreen dashboardScreen = inflate(...); dashboardScreen.title.setText(...); class
DashboardScreen { TextView title; @Override void onFinishInflate() { title = findViewById(...); } } -
Life is synchronous again DashboardScreen dashboardScreen = inflate(...); dashboardScreen.title.setText(...); class
DashboardScreen { TextView title; @Override void onFinishInflate() { title = findViewById(...); } } -
Life is synchronous again DashboardScreen dashboardScreen = inflate(...); dashboardScreen.title.setText(...); class
DashboardScreen { TextView title; @Override void onFinishInflate() { title = findViewById(...); } } -
Consistent Lifecycle class Activity { @Override void onCreate(Bundle b) {
super.onCreate(); setContentView(...); } @Override void onRestoreInstanceState(Bundle b) { // Views get restored here. super.onRestoreInstanceState(b); } } -
Intercepting View inflation class DaggerLayoutFactory implements LayoutInflater.Factory2 { private DaggerComponent
component; public View onCreateView(View parent, String name, ...) { View view = inflate(parent, name); if (view instanceOf ViewWithDaggerDependencies) { view.injectDependencies(component); } } } -
Intercepting View inflation class DaggerLayoutFactory implements LayoutInflater.Factory2 { private DaggerComponent
component; public View onCreateView(View parent, String name, ...) { View view = inflate(parent, name); if (view instanceOf ViewWithDaggerDependencies) { view.injectDependencies(component); } } } -
Intercepting View inflation class DaggerLayoutFactory implements LayoutInflater.Factory2 { private DaggerComponent
component; public View onCreateView(View parent, String name, ...) { View view = inflate(parent, name); if (view instanceOf ViewWithDaggerDependencies) { view.injectDependencies(component); } } } -
Google Maps class MapsScreen extends FrameLayout { @Inject TheActivity activity;
void onAttachedToWindow() { activity.getFragmentManager() .beginTransaction() .add(getId(), MapFragment.newInstance()) .commitNowAllowingStateLoss(); } void onDetachedFromWindow() { activity.getFragmentManager() .beginTransaction() .remove(mapFragment) .commitNowAllowingStateLoss(); } } -
Google Maps class MapsScreen extends FrameLayout { @Inject TheActivity activity;
void onAttachedToWindow() { activity.getFragmentManager() .beginTransaction() .add(getId(), MapFragment.newInstance()) .commitNowAllowingStateLoss(); } void onDetachedFromWindow() { activity.getFragmentManager() .beginTransaction() .remove(mapFragment) .commitNowAllowingStateLoss(); } } #YOLO -
If Views don’t work out @Override void changeKey(State outgoing, State
incoming, Direction direction, ...) { if (frame.getChildCount() > 0) { View outgoingView = frame.getChildAt(0); if (outgoing != null) { outgoing.save(outgoingView); } frame.removeAllViews(); } View incomingView = inflate(layoutResIdForKey(incoming), ...); frame.addView(inView); incoming.restore(inView); } -
If Views don’t work out @Override void changeKey(State outgoing, State
incoming, Direction direction, ...) { if (frame.getChildCount() > 0) { View outgoingView = frame.getChildAt(0); if (outgoing != null) { outgoing.save(outgoingView); } frame.removeAllViews(); } View incomingView = inflate(layoutResIdForKey(incoming), ...); frame.addView(inView); incoming.restore(inView); } -
If Views don’t work out @Override void changeKey(State outgoing, State
incoming, Direction direction, ...) { Fragment incomingFrag = ScreenFragment.create(incoming.key()); fragmentManager .beginTransaction() .replace(frame.getId(), incomingFrag) .commitNow(); } -
Summary • Use Conductor for an existing app • Flow
is recommended for a new app