I'am trying to build a little voice recording app with 3 Tabs as Fragments. I learned that 2 Tabs will be created with onCreateView when i start the app. At first i expected that onCreatView in PlaceHolderFragment will be called for the new Fragment to be displayed each time switching the Fragment. (It's the simple Fragment Acitivty Demo in Android Studio which i extended for voice recording).
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView;
Integer section = getArguments().getInt(ARG_SECTION_NUMBER);
switch (section) {
case 1:
rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
final TextView timeView;
timeView = (TextView) rootView.findViewById(R.id.timeView);
//Button start_stop;
final Button start_stop = (Button) rootView.findViewById(R.id.button);
start_stop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MainActivity mainAct = (MainActivity) getActivity();
if (recorder == null) {
recorder = new MediaRecorder();
}
if (mainAct.running) {
recorder.stop();
start_stop.setText("start recording");
mainAct.running = false;
mainAct.time = 0;
} else {
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
//recorder.setOutputFile(Environment.getExternalStorageDirectory().getAbsolutePath() + "/myrecording.mp3");
recorder.setOutputFile(getActivity().getApplicationContext().getFilesDir() + "/audio.mp3");
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
try {
recorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
recorder.start();
mainAct.running = true;
mainAct.initThread(timeView);
start_stop.setText("stop recording");
}
}
});
break;
case 2:
rootView = inflater.inflate(R.layout.fragment_sound, container, false);
break;
case 3:
rootView = inflater.inflate(R.layout.fragment_pics, container, false);
break;
default:
rootView = null;
break;
}
return rootView;
}`
The recorded duration will be displayed within a TextView. The time is updated within a new Thread.
public void initThread(final TextView timeView) {
refreshThread = new Thread(new Runnable() {
public void run() {
while (running) {
time = time + 0.1;
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
}
runOnUiThread(new Runnable() {
public void run() {
timeView.setText(getString(R.string.time_string, String.format("%.1f", time)));
}
});
}
}
});
refreshThread.start();
}
so far everything works fine - recording is running when I'm on the first tab (which displays the duration) - even when i switch to the second tab. But when i switch to the third tab and go back to the first tab the duration isn't displayed any longer.
exact: When i got from Tab 1 to Tab 2 and back to Tab 1 time is still updating as expected. Going from Tab 1 to Tab 2 to Tab 3 and back to Tab 2 and to Tab 1 everything seems to be initial on this screen. Time won't be updated anye longer - the TextView has the inital text. I debugged the app, when going back from Tab 3 the Threat is still running, but the TextView won't get the current duration.
Can somebody please give me a hint for this behavior? I think when going to Tab 3 the fragment for Tab 1 will be destroyed - but it will be built again when switching to Tab 1.
How to solve this issue?
Thx