33import android .appwidget .AppWidgetManager ;
44import android .content .ComponentName ;
55import android .content .Context ;
6+ import android .content .DialogInterface ;
67import android .content .Intent ;
78import android .content .SharedPreferences ;
89import android .content .res .Resources ;
910import android .os .Bundle ;
11+ import android .support .v7 .app .AlertDialog ;
1012import android .view .Menu ;
1113import android .view .MenuItem ;
1214import android .view .inputmethod .InputMethodManager ;
@@ -33,8 +35,7 @@ protected void onCreate(Bundle savedInstanceState) {
3335 ButterKnife .bind (this );
3436
3537 mPrefs = getSharedPreferences (Constants .PREFS_KEY , Context .MODE_PRIVATE );
36- final String text = mPrefs .getString (Constants .TEXT , "" );
37- mNotesView .setText (text );
38+ mNotesView .setText (getSavedNote ());
3839 }
3940
4041 @ Override
@@ -51,6 +52,16 @@ protected void onPause() {
5152 }
5253 }
5354
55+ @ Override
56+ public void onBackPressed () {
57+ if (mConfig .getShouldPromptAutosave () && !getCurrentNote ().equals (getSavedNote ())) {
58+ mConfig .setShouldPromptAutosave (false );
59+ displayAutosavePrompt ();
60+ } else {
61+ super .onBackPressed ();
62+ }
63+ }
64+
5465 @ Override
5566 protected void onDestroy () {
5667 super .onDestroy ();
@@ -86,8 +97,23 @@ public boolean onOptionsItemSelected(MenuItem item) {
8697 }
8798 }
8899
100+ private void displayAutosavePrompt () {
101+ final AlertDialog .Builder alertDialog = new AlertDialog .Builder (this );
102+ alertDialog .setTitle (getString (R .string .unsaved_changes ));
103+ alertDialog .setMessage (getString (R .string .autosave_prompt_msg ));
104+
105+ alertDialog .setNegativeButton (R .string .cancel , null );
106+ alertDialog .setPositiveButton (R .string .enable_autosave , new DialogInterface .OnClickListener () {
107+ @ Override
108+ public void onClick (DialogInterface dialog , int which ) {
109+
110+ }
111+ });
112+ alertDialog .create ().show ();
113+ }
114+
89115 private void saveText (boolean showToast ) {
90- final String text = mNotesView . getText (). toString (). trim ();
116+ final String text = getCurrentNote ();
91117 mPrefs .edit ().putString (Constants .TEXT , text ).apply ();
92118
93119 if (showToast ) {
@@ -99,7 +125,7 @@ private void saveText(boolean showToast) {
99125 }
100126
101127 private void shareText () {
102- final String text = mNotesView . getText (). toString (). trim ();
128+ final String text = getCurrentNote ();
103129 if (text .isEmpty ()) {
104130 Utils .showToast (getApplicationContext (), R .string .cannot_share_empty_text );
105131 return ;
@@ -115,6 +141,14 @@ private void shareText() {
115141 startActivity (Intent .createChooser (sendIntent , shareTitle ));
116142 }
117143
144+ private String getCurrentNote () {
145+ return mNotesView .getText ().toString ().trim ();
146+ }
147+
148+ private String getSavedNote () {
149+ return mPrefs .getString (Constants .TEXT , "" );
150+ }
151+
118152 private void hideKeyboard () {
119153 final InputMethodManager imm = (InputMethodManager ) getSystemService (Context .INPUT_METHOD_SERVICE );
120154 imm .hideSoftInputFromWindow (mNotesView .getWindowToken (), 0 );
0 commit comments