Advertisement
Guest User

Untitled

a guest
Jul 10th, 2016
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.88 KB | None | 0 0
  1. public class SampleActivity extends FragmentActivity {
  2.  
  3.     @Retain
  4.     Object mObjectToRetain;
  5.  
  6.     //objects marked with @NonNull and @Retain will be checked for null during save()/restore() calls
  7.     //and if they are null, NullPointerException will be thrown
  8.     @NonNull
  9.     @Retain
  10.     Object mNonNullObjectToRetain;
  11.  
  12.     @Override
  13.     protected void onCreate(Bundle savedInstanceState) {
  14.         super.onCreate(savedInstanceState);
  15.         BeRetained.onCreate(this);
  16.  
  17.         if(!BeRetained.restore(this)) {
  18.             //oops, couldn't restore! Looks like it's a first start or app was killed
  19.             mObjectToRetain = new Object();
  20.             mNonNullObjectToRetain = new Object();
  21.         }
  22.     }
  23.  
  24.     @Override
  25.     protected void onSaveInstanceState(Bundle outState) {
  26.         super.onSaveInstanceState(outState);
  27.         BeRetained.save(this);
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement