Pregunta

This is more of a structure/design philosophy question than anything.

I have main host activity that currently saves and loads profiles for viewing by the user. The profiles' credentials are displayed within Fragment A (user name, birthday, etc). This is done by passing the profile as an argument to the fragment before using the fragment transaction to display it.

If the user leaves or rotates their screen, how should I go about saving that profile data? Should I do the saving from the onPause within Fragment A or its HostActivity?

And what if Fragment A also allows for profile editing? When the user confirms their changes, shall I let its HostActivity know to update the main profile being held from the activity?

Or would it be better to just wait until a FragmentA.onPause is called? I suppose I could wait until something forces the fragment to call onPause, at which point I can save both the state of the fragment as well as the profile activity from the host activity.

The main thing that confuses me is: Do I need to be managing two Profile objects? The HostActivity and its FragmentA both use it. It gets a bit confusing having to run back and forth between saving, loading, and editing. Can't I just handle all of this from one class?

Okay, I have two very good and viable answers. Which one is better for my purposes though? Should I use an sqlLite database or a global Java singleton to handle my profile? Only one profile can be active per session. Saving, loading, and editing of the profile must also be taken into consideration.

¿Fue útil?

Solución

If there can only ever be one profile per user in one session (as you have suggested above) why not use a Java singleton class.

 private ProfileOject() {
    // Exists only to defeat instantiation.
}

public static ProfileObject getInstance() {
    if(instance == null) {
        instance = new ProfilerObject();
    }
    return instance;
}

Now use getters and setters as normal.

In each activity you can get the Profile like so:

profile = ProfileObject.getInstance();

This will mean that if you make an update in Fragment A, when the activity is called, it will fetch the updated values from the profile object.

In regards to onRotate/Pause/Resume use savedInstanceState, see example below:

//Use onSaveInstanceState(Bundle) and onRestoreInstanceState

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {

  // Save UI state changes to the savedInstanceState.   
  // This bundle will be passed to onCreate if the process is  
  // killed and restarted.

  savedInstanceState.putBoolean("MyBoolean", true);  
  // etc.  
  super.onSaveInstanceState(savedInstanceState);  
}  
//onRestoreInstanceState  
    @Override  
public void onRestoreInstanceState(Bundle savedInstanceState) {  
  super.onRestoreInstanceState(savedInstanceState);  
  // Restore UI state from the savedInstanceState.  
  // This bundle has also been passed to onCreate.  
  boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");  
}

Otros consejos

Why don't you use a sqlite database for storing user profile data? Everytime you need access to user profile data you can just query it.

You can save all the relevant data in the onPause method. Using a database will ensure data persistence, resisting any fragment/activity recreation!

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top