Mark
Mark

Reputation: 440

Whether or not to use multiple instances of an Activity on Android?

If I have an app that refills a ListView with different data in response to a user selecting an item. Should I start a new Activity or just change the data, and call Adapter.notifyDataSetChanged()?

It seems like calling notifyDataSetChanged() is simpler, and would minimize my potential number of activities (and memory usage), but I want the Up/Back navigation to work.

My app works like File Explorer where there is a list of folders, and clicking on a folder changes the list to show the contents of the new folder. If the user clicks on a folder, I want the Up/Back buttons to take them back to the prior folder. I have implemented this with a single instance of the activity and using notifyDataSetChanged(), but can't get the Up/Back buttons working as desired. I am thinking I need to either override those somehow, or use multiple instances of the same activity. Any direction here would be appreciated.

Thanks

Upvotes: 1

Views: 72

Answers (1)

Bhullnatik
Bhullnatik

Reputation: 1312

Using multiple instances of the same Activity is highly unproductive.

To solve your problem, the best way to do it would be to use a fragment using multiple instances of it, each one representing a folder in your arborescence. Then in the onBackPressed() of your activity, you can just use popBackStack() while there is a fragment in the backStack, then call super.onBackPressed() to resume default behavior.


Upvotes: 1

Related Questions