因为onDestroy而导致的Android片段被调用两次,onViewStateRestored()无法正常工作

时间:2014-04-20 15:10:28

标签: android android-fragments

我正在编程一个调谐器。调谐器是一个片段。我的目标是在手机在横向和纵向模式之间切换时保持调谐器运行。因此,我使用onSaveInstanceState()保存实际状态,并使用onViewStateRestored()恢复它。这实际上非常好。但我有一个大问题: 当片段在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);
    }
}

1 个答案:

答案 0 :(得分:6)

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

所以你需要通过覆盖活动的onCreate方法来确保你没有创建新片段的活动娱乐,如下所示:

@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();
    }
}