1

I'm trying to start a new activity using intent and on finishing the activity, I want my previous activity to retain its data. I already have other activities passing some data into this main activity but on starting this new activity, when I go back, all that data disappears and gives me a null pointer exception.

Is it possible to retain the data from the previous activity? I searched about it and found I could make a singleton class but it's usage is still unclear to me.

P.S.: The real problem is I'm trying to pass a String ArrayList from this activity to the main activity but on doing so, the previous data (being passed on from previous activities) on the main activity disappears.

Shubham Kumar
  • 129
  • 1
  • 9

2 Answers2

0

If usage is unclear then you can use SharedPreference for now.

Saved the values in the shared preferences on receiving Intent data.

Then when you navigate to next screen and comeback, in the onResume of your activity load the data saved in the shared preferences.

0

Check that link https://inthecheesefactory.com/blog/understand-android-activity-launchmode/en

I think "single top" activity will solve your problem.

Also you can send data from any another android activity or fragment to your single top activity through intents and handle it in main activity in that way:

@Override
    protected void onNewIntent(Intent intent) {
        ... = intent.getStringArrayExtra(KEY)
        ...
}

Also you can check that answer related to single top activities https://stackoverflow.com/a/1715405/6193843

Link182
  • 733
  • 6
  • 15