Search code examples
androidbundlepublic-method

What are the pros on passing variables using Bundle instead of publics methods?


I'm taking the developing android app's course on udacity.

I was wondering if I am right in an alternative solution for Lesson5: Handle List Item Click.

the challenge when we have 2 panels and we clicked an item from the List, Uri should pass directly to DetailFragment from MainActivity without calling DetailActivity

I've seen that the official solution is: use Bundle and pass it as argument in fragment.setArguments(args);

But instead of above Why not create a public method?, I tried with creating a public method such as setmUri(Uri uri) to set member variable mUri in DetailFragment and then call it from MainActivity, and this worked well

But my final question is: What are the pro's on passing variables using Bundle instead of publics methods for this particular case?

thanks a lot.


Solution

  • I tried with creating a public method such as setmUri(Uri uri) to set member variable mUri in DetailFragment and then call it from MainActivity, and this worked well

    By default, that will not handle a configuration change. When the user rotates the screen, changes locales, connects a keyboard, etc., your activity and its fragments will be destroyed and recreated by default.

    It also will not handle the all-too-common process termination scenario:

    • User is in your app
    • User presses HOME, moving your app to the background
    • 5 minutes later, while you are still in the background, Android terminates your process to free up system RAM for other processes
    • 5 minutes after that, the user returns to your app

    Anything that is part of the saved instance state will not be lost during either of those changes (configuration change or process termination). For input into a fragment, the easiest way to have that information be part of the saved instance state is via the arguments Bundle.