1 package com.davemorrissey.labs.subscaleview.test; 2 3 import android.app.ActionBar; 4 import android.os.Bundle; 5 import android.support.annotation.Nullable; 6 import android.support.v4.app.FragmentActivity; 7 import android.view.MenuItem; 8 9 import java.util.List; 10 11 public abstract class AbstractFragmentsActivity extends FragmentActivity { 12 13 private static final String BUNDLE_PAGE = "page"; 14 15 private int page; 16 17 private final int title; 18 private final int layout; 19 private final List<Page> notes; 20 21 protected abstract void onPageChanged(int page); 22 23 protected AbstractFragmentsActivity(int title, int layout, List<Page> notes) { 24 this.title = title; 25 this.layout = layout; 26 this.notes = notes; 27 } 28 29 @Override 30 protected void onCreate(@Nullable Bundle savedInstanceState) { 31 super.onCreate(savedInstanceState); 32 setContentView(layout); 33 ActionBar actionBar = getActionBar(); 34 if (actionBar != null) { 35 actionBar.setTitle(getString(title)); 36 actionBar.setDisplayHomeAsUpEnabled(true); 37 } 38 if (savedInstanceState != null && savedInstanceState.containsKey(BUNDLE_PAGE)) { 39 page = savedInstanceState.getInt(BUNDLE_PAGE); 40 } 41 } 42 43 @Override 44 protected void onResume() { 45 super.onResume(); 46 updateNotes(); 47 } 48 49 @Override 50 protected void onSaveInstanceState(Bundle outState) { 51 super.onSaveInstanceState(outState); 52 outState.putInt(BUNDLE_PAGE, page); 53 } 54 55 @Override 56 public boolean onOptionsItemSelected(MenuItem item) { 57 finish(); 58 return true; 59 } 60 61 public void next() { 62 page++; 63 updateNotes(); 64 } 65 66 public void previous() { 67 page--; 68 updateNotes(); 69 } 70 71 private void updateNotes() { 72 if (page > notes.size() - 1) { 73 return; 74 } 75 ActionBar actionBar = getActionBar(); 76 if (actionBar != null) { 77 actionBar.setSubtitle(notes.get(page).getSubtitle()); 78 } 79 onPageChanged(page); 80 } 81 82 } 83