package com.simplemobiletools.notes; import android.appwidget.AppWidgetManager; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import android.widget.Toast; import butterknife.BindView; import butterknife.ButterKnife; public class MainActivity extends AppCompatActivity { private SharedPreferences prefs; @BindView(R.id.notes_view) EditText notesView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); prefs = getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE); final String text = prefs.getString(Constants.TEXT, ""); notesView.setText(text); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.save: saveText(); return true; case R.id.about: final Intent intent = new Intent(getApplicationContext(), AboutActivity.class); startActivity(intent); return true; default: return super.onOptionsItemSelected(item); } } private void saveText() { final String text = notesView.getText().toString().trim(); prefs.edit().putString(Constants.TEXT, text).apply(); Toast.makeText(this, "Text saved", Toast.LENGTH_SHORT).show(); hideKeyboard(); updateWidget(); } private void hideKeyboard() { final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(notesView.getWindowToken(), 0); } private void updateWidget() { final Context context = getApplicationContext(); final AppWidgetManager widgetManager = AppWidgetManager.getInstance(context); int[] ids = widgetManager.getAppWidgetIds(new ComponentName(context, MyWidgetProvider.class)); final Intent intent = new Intent(this, MyWidgetProvider.class); intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids); sendBroadcast(intent); } }