0

I've been trying to get this working for a while now with no success.I'm running out of options and hope you guys can help. I want to save the text in the edit text using sharedprefernce. Then send that text to my appwidgetprovider,which sends the text back for editing,when the widget is clicked.When editing is done and i save it it should send the text back to the appwidgetprovider and so on.

But that not the case. The edit text opens up with previous saved text,but once I've saved it doesn't update the shared preference. The value of the shared preference in the app-widget provider remains the same.

Here's the class which sends the text to the appwidgetprovider.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(widget_handler);

    editText = (EditText) findViewById(R.id.widget_note);
    editText.requestFocus();
    final InputMethodManager imm2 = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm2.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_widget);
    setSupportActionBar(toolbar);
    editText.setText(getIntent().getStringExtra("Name"));



  toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
    toolbar.setNavigationOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v) {
    String newEntry = editText.getText().toString();
        final SharedPreferences sharedPreferences=getSharedPreferences("Key",WidgetHandler.MODE_PRIVATE);
        final SharedPreferences.Editor editor=getSharedPreferences(newEntry,WidgetHandler.MODE_PRIVATE).edit();
        sharedPreferences.edit().putString("Name",newEntry).apply();
        editor.putString("Name",newEntry);
        editor.apply();
        Intent intent3= getIntent();
        Bundle extras=intent3.getExtras();

        if (extras!= null) {
            mAppWidgetId = extras.getInt(
                    AppWidgetManager.EXTRA_APPWIDGET_ID,
                    AppWidgetManager.INVALID_APPWIDGET_ID);
        }


            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
            RemoteViews remoteViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.new_app_widget);

            remoteViews.setTextViewText(R.id.appwidget_text, newEntry);
            appWidgetManager.updateAppWidget(mAppWidgetId, remoteViews);
           final  Intent intent = new Intent();
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
            intent.putExtra("Name",newEntry);

        setResult(RESULT_OK, intent);


            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
            finish();
            Intent home = new Intent(Intent.ACTION_MAIN);
            home.addCategory(Intent.CATEGORY_HOME);
            home.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(home);


    }
});

Here's my appwidgetprovider which is supposed to receive the updated text.

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                            int appWidgetId) {

}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    for (int appWidgetId : appWidgetIds) {
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);

       SharedPreferences sharedPreferences=context.getSharedPreferences("Key",Context.MODE_PRIVATE);
        String name=sharedPreferences.getString("Name","");
        Log.e("name",name);

        final Intent clickIntent = new Intent(context, WidgetHandler.class);
        clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetId);
        clickIntent.putExtra("Name",name);
        PendingIntent clickPI = PendingIntent.getActivity(context, 0, clickIntent,PendingIntent.FLAG_UPDATE_CURRENT);

        views.setOnClickPendingIntent(R.id.appwidget_text,clickPI);

        // Instruct the widget manager to update the widget
        appWidgetManager.updateAppWidget(appWidgetId, views);




    }
    super.onUpdate(context, appWidgetManager, appWidgetIds);
}





@Override
public void onEnabled(Context context) {

    // Instruct the widget manager to update the widget


}

@Override
public void onDisabled(Context context) {


    // Enter relevant functionality for when the last widget is disabled
}

Thanks in advance :).

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
CharlieKid
  • 31
  • 4

1 Answers1

0

Try editor.commit() instead of editor.apply()