gpt4 book ai didi

Android Fragments 由于 onDestroy 被调用两次,onViewStateRestored() 无法正常工作

转载 作者:行者123 更新时间:2023-11-29 14:49:31 24 4
gpt4 key购买 nike

我正在编写一个调谐器。调谐器是一个 fragment 。我的目标是当手机在横向和纵向模式之间切换时保持调谐器运行。因此,我使用 onSaveInstanceState() 保存实际状态并使用 onViewStateRestored() 恢复它。这实际上工作得很好。但我有一个大问题: fragment 重建时onDestroy()、onCreateView()和onViewStateRestored()会被第二次执行。这会导致崩溃。有什么解决办法吗?

代码下方,请询问是否需要更详细的代码:

public class TunerFragment extends BasicFragment
{
//View Elemente
public boolean controlButtonUnclicked = true;
private static final String LOG_TAG = "FFTTEST";
private final Semaphore bufferZugriff = new Semaphore(1, true);


//Objecte zur Aufnahme
private int channel_config = AudioFormat.CHANNEL_IN_MONO;
private int format = AudioFormat.ENCODING_PCM_16BIT;
private int sampleRate = 44100;
private int bufferSize = AudioRecord.getMinBufferSize(sampleRate, channel_config, format)*8; // je größer der Buffer, desto genauer die FFT!!
private AudioRecord audioInput = null; //new AudioRecord(AudioSource.MIC, sampleSize, channel_config, format, bufferSize);
private short[] audioBuffer = new short[bufferSize];

//Attribute zur Steuerung
private Thread readingThread = null,writingThread=null;
private boolean isRecording = false;




@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view;

view = inflater.inflate(R.layout.fragment_tuner, container, false);

TextView tv = (TextView) view.findViewById(R.id.textview_referencenote);

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.getActivity());

tv.setText(sharedPrefs.getString("value_a", "440"));

Button mButton = (Button) view.findViewById(R.id.controlButton);
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
onControlButton(v);
}
});
return view;
}

@Override
public void onDestroy() {
super.onDestroy();
if(isRecording)
{
isRecording = false;
while(this.readingThread.isAlive());

try
{
audioInput.stop();
audioInput.release();
}
catch (Exception e)
{}
while(this.writingThread.isAlive());

audioInput= null;
readingThread = null;
writingThread = null;

}
}

@Override
public void onStop() {
super.onStop();
if(isRecording)
{
isRecording = false;
while(this.readingThread.isAlive());
try
{
audioInput.stop();
audioInput.release();
}
catch (Exception e)
{}
while(this.writingThread.isAlive());

audioInput= null;
readingThread = null;
writingThread = null;

}
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("isRecording", isRecording);
}

@Override
public void onViewStateRestored (Bundle savedInstanceState) {
super.onViewStateRestored (savedInstanceState);
if(savedInstanceState!=null)
{
if(savedInstanceState.getBoolean("isRecording"))
{
Button mPlayButton = (Button) getView().findViewById(R.id.controlButton);
//Recorder starten
controlButtonUnclicked = false;
mPlayButton.setText("Stop");
startRecording();
isRecording = true;
}
}
}

public void onControlButton(View v)
{
//...


}


private void onPlay(boolean start) {
//starts and stops the tuner, depending on the actual state

}



private void startRecording()
{

//prepares and starts the worker threads (Thread for reading the Mic.-buffer and Thread for processing the signal)
}



private int maxFrequenzToIndex (int MaxFreq)
{
return (MaxFreq * 1 * bufferSize) / (1 * sampleRate);
}

private void calculate()
{
while(isRecording)
{
//processing the signal ...



//post result to main thread
final String notennamef = notenname;
final int differenzf=differenz;
((TextView) getView().findViewById(R.id.editNote)).post(new Runnable() {

@Override
public void run() {
try
{
TextView View = (TextView) getView().findViewById(R.id.editNote);

View.setText(""+notennamef);

View = (TextView) getView().findViewById(R.id.editAbweichung);
View.setText(""+differenzf);
}
catch (NullPointerException e)
{
System.out.println("Exception in calculate() -> run()");
System.out.println(e);
}
}
});

try
{
Thread.sleep(100); //Wie oft wird die Anzeige aktualisiert?
}
catch (InterruptedException e)
{}
}
// restore neutral view
((TextView) getView().findViewById(R.id.editNote)).post(new Runnable() {

@Override
public void run() {
try
{
TextView View = (TextView) getView().findViewById(R.id.editNote);
View.setText("");



View = (TextView) getView().findViewById(R.id.editAbweichung);
View.setText("");
}
catch (NullPointerException e)
{
System.out.println("Exception in calculate() -> nach while");
System.out.println(e);
}
}
});
}

private int calculateCent(double referenz, double frequenz)
{
return (int) (1200*(Math.log(frequenz/referenz)/Math.log(2))); // die Formel entspricht 1200*log2(frequenz/referenz)
}

private void readAudioToBuffer()
{
while(isRecording)
{

try
{
bufferZugriff.acquire();
audioInput.read(audioBuffer, 0,bufferSize);
bufferZugriff.release();
}
catch (Exception e )
{
System.out.println("Fehler beim schreiben in den Audiobuffer");
}

}

}


private void stopRecording()
{
isRecording = false;
try
{
audioInput.stop();
audioInput.release();
}
catch (Exception e)
{}
audioInput= null;
readingThread = null;
writingThread = null;
getView().findViewById(R.id.controlButton).setKeepScreenOn(false);
}
}

最佳答案

问题在于,当手机在横向和纵向模式之间切换时,会重新创建 Activity。

所以你需要确保在 Activity 重新创建时,你不会通过覆盖 Activity 的 onCreate 方法来创建新 fragment ,如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);

if (savedInstanceState == null) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();

Fragment fragment = new TunerFragment();
transaction.add(R.id.fragment_container, fragment);
transaction.commit();
}
}

关于Android Fragments 由于 onDestroy 被调用两次,onViewStateRestored() 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23183989/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com