0

I have an activity that passes updated information from my EditBook activity to my MainActivity via an Intent.

Button for EditBook:

FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String title = mTitle.getText().toString();
            String author =  mAuthor.getText().toString();
            String publisher = mPublisher.getText().toString();

            Intent replyIntent = new Intent();
            replyIntent.putExtra(EXTRA_ID, getIntent().getIntExtra(EXTRA_ID, -1));
            replyIntent.putExtra(EXTRA_TITLE, title);
            replyIntent.putExtra(EXTRA_AUTHOR, author);
            replyIntent.putExtra(EXTRA_PUBLISHER, publisher);
            replyIntent.putExtra(EXTRA_ISBN, getIntent().getStringExtra(BookView.EXTRA_ISBN));
            setResult(RESULT_OK, replyIntent);
            finish();

        }
    });

And my receiving end in OnActivityResult:

if (data.getIntExtra(EditBook.EXTRA_ID, -1) != -1) {

            Book book = new Book(data.getIntExtra(EditBook.EXTRA_ID, -1), data.getStringExtra(EditBook.EXTRA_TITLE),
                    data.getStringExtra(EditBook.EXTRA_AUTHOR), data.getStringExtra(EditBook.EXTRA_PUBLISHER),
                    "1", data.getStringExtra(EditBook.EXTRA_ISBN));

            try {
                asyncWait = mBookViewModel.update(book);
                spinner.setVisibility(View.GONE);

                Toast.makeText(getApplicationContext(), R.string.book_saved, Toast.LENGTH_LONG).show();

                Intent intent = getIntent();
                finish();
                startActivity(intent);

            } catch (ExecutionException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

The asyncWait variable simply collects the result from AsyncTask so that my UI will update.

The book I am trying to edit has a title come back as "1984 (signetClassic)". In my EditBook activity, I change this to "1984" and click the save floating action button. When I step through the debugger, title does show the correct title, the replyIntent mMap shows the correct title, but when I inspect the data in the MainActivity, the title has reverted back to "1984 (signetClassic)". Any ideas why the Intent extras are changing when sent?

Let me know if there is any more information you need.

Xaraphena
  • 71
  • 1
  • 9
  • Pass Data Using Bundle – Sagar Poshiya Feb 22 '19 at 17:55
  • 1
    @SagarPoshiya the data still changes from "1984" in the `EditBook` activity to "1984 (signetClassic)" in the `MainActivity` with the bundle – Xaraphena Feb 22 '19 at 18:05
  • 1
    @SagarPoshiya yes. And my onActivityResult method works fine with my other activities with the proper Intent extras – Xaraphena Feb 22 '19 at 18:13
  • Go here to more complete response https://stackoverflow.com/questions/920306/sending-data-back-to-the-main-activity-in-android – Sagar Poshiya Feb 22 '19 at 18:13
  • Alright, apparently I just wasn't paying very good attention. When passing the data back, it passes through a third activity that reset the title back to what it was before I edited it. I was referencing the wrong key. – Xaraphena Feb 22 '19 at 18:53

0 Answers0