Android: Transferring strings / values within Tabs, each tab having its own activity (other than Getters-Setters)
I have a tabbed layout in my application, and want the settings part to be another tab in the UI. The settings involve entering a quantity / number of entities present (e.g.: number of balls). This data is then required to be transferred to the other tabs, which process the input data, and process the display within the respective tabs accordingly (say the number of balls= 3, then there should be 3 buttons in the 2nd tab, if balls= 1, then only 1 button, and so on...). I want to avoid the data being sent to the parent activity, and then to the respective child activities, which requires the whole app to restart [startActivity()], and individual transfer is not possible, since startActivity() for child activities would result in only the child activity contents being displayed. In which case I have to ultimately use the Getters and Setters. Is there any other / better meth开发者_Go百科od which I can follow in this case...?
You can try onPause/onResume methods.
When tab changed, activity goes to onpause. I override onPause method for save variables to different public class. Than load back variables onResume and onCreate methods.
@Override
public void onPause()
{
super.onPause();
fileC.fileFrom = fileFrom;
fileC.fileName = fileName;
fileC.fileTo = fileTo;
fileC.Process = fileProcess;
Log.w("onPause", "onPause!!!!!!!!");
}
@Override
public void onResume()
{
super.onResume();
if( fileC.Process != null )
{
fileFrom = fileC.fileFrom;
fileTo = fileC.fileTo;
fileName = fileC.fileName;
fileProcess = fileC.Process;
}
Log.w("onResume", "onResume!!!!!!!!");
}
Singletons are not the best choice for Android application because Dalvik VM may load singleton class twice and destroy all your stored data.
Also storing data in your own Application object not suitable because system may kill this object on low memory and does not restore its state on resume.
The best way, imho, is sending data via Intent object or getters/setteres, storing in at onPause method.
Why can't you use a singleton class and static class variables to pass this data?.
精彩评论