0

In Activity A, I click a button to go to Activity B. I bring data using Bundle back to the Activity A and I managed to change a String value in the ListView. Then I go to another Activity C to manipulate another data. The problem is, when I get back again to the Activity A, the data from the Activity C is reflected in the ListView, but the previous data from the Activity B is gone.

Here's a code to the Activity B from the Activity A.

private void gotoRepeatWeeklyOptionActivity() {
    Intent intent = new Intent(CreateNewAlarmActivity.this, RepeatWeeklyOptionActivity.class);
    startActivity(intent);
    finish();
}

This one is the method from the Activity A to the Activity C.

private void openAlarmToneOptionDialog() {
    Intent intent = new Intent(CreateNewAlarmActivity.this, ChooseAlarmToneActivity.class);
    startActivity(intent);
}

And this is a code that leads to the Activity A from the Activity B.

@Override
public void onBackPressed() {
    Intent intent = new Intent(this, CreateNewAlarmActivity.class);

    ArrayList<Integer> repeatStatusIntList = new ArrayList<>();
    intent.putExtra(Keys.OPTION, Keys.OPTION_REPEAT);

    if(cbRepeatEveryDay.isChecked()) {
        super.onBackPressed();
        intent.putExtra(Keys.VALUE, Keys.EVERY_DAY);
        Toast.makeText(this, getString(R.string.alarm_will_repeat_every_day), Toast.LENGTH_SHORT).show();
    } else if(allUnchecked()) {
        super.onBackPressed();
        intent.putExtra(Keys.VALUE, Keys.NO_REPEAT);
        Toast.makeText(this, getString(R.string.alarm_will_never_repeat), Toast.LENGTH_SHORT).show();
    } else {
        for(int i=0; i<repeatStatusList.size(); i++) {
            super.onBackPressed();
            if(repeatStatusList.get(i).isChecked()) {
                repeatStatusIntList.add(getRepeatStatusInt(repeatStatusList.get(i)));
                intent.putIntegerArrayListExtra(Keys.KEY_REPEAT_STATUS_INTEGERS_LIST, repeatStatusIntList);
            }

            intent.putExtra(Keys.VALUE, Keys.ON);
        }
        Toast.makeText(this, getString(R.string.alarm_will_repeat) + getString(R.string.every) +
                getAllRepeatDays(repeatStatusList), Toast.LENGTH_SHORT).show();
    }
    startActivityForResult(intent, RESULT_OK);
    finish();
}

Finally, this is a code from the Activity C, which leads back to the Activity A.

    btnSelectTone.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getApplicationContext(), CreateNewAlarmActivity.class);
            intent.putExtra(Keys.OPTION, Keys.OPTION_ALARM_TONE);
            intent.putExtra(Keys.VALUE, ringtone);

            if(mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
                mediaPlayer.release();
            }
            startActivityForResult(intent, RESULT_OK);
            finish();
        }
    });

Here's a sample screen from the Activity A. enter image description here

marshallslee
  • 645
  • 1
  • 8
  • 22

1 Answers1

0

what Atif has suggested is one solution which is most desirable in your case. Else you can use Shared Preferences like below. Get the SP instance and commit the data you want to share in editor

// We need an Editor object to make preference changes.
      // All objects are from android.context.Context
      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
      SharedPreferences.Editor editor = settings.edit();
      editor.putBoolean("silentMode", mSilentMode);

      // Commit the edits!
      editor.commit();
    }

And this is how you can retrieve the shared pref data

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
       boolean silent = settings.getBoolean("silentMode", false);

Personally i would suggest you to surf more on developer.android.com or visit below link https://developer.android.com/guide/topics/data/data-storage.html#pref

In future if you have large amount of data then above solution wont work.

Please follow below link for more options

How to pass the values from one activity to previous activity

Community
  • 1
  • 1
rmdroid
  • 889
  • 1
  • 6
  • 7