Android: Keep fragment running in the background

artzy :

I have a fragment that starts a count and changes an icon status. After opening the app there is a count - 00:00 and a button that says START.

After clicking START the counts starts and the button changes to STOP.

After clicking STOP the count stops and the button changes to START. Pretty basic.

The thing is that after clicking START and minimalizing the app and opening it back (putting app in background and back) the count and button is always reverted back to START and 00:00.

So the question is: How can I keep the fragment alive after minimalizing the app?

Code:

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View recordView = inflater.inflate(R.layout.fragment_record, container, false);
        ButterKnife.bind(this, recordView);
        return recordView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        btnPause.setVisibility(View.GONE);
        recordBtn.setColorPressed(getResources().getColor(R.color.colorPrimary));
    }

    @OnClick(R.id.btnRecord)
    public void recordAudio(){
        onRecord(mStartRecording);
        mStartRecording = !mStartRecording;
    }

    private void onRecord(boolean start) {
        Intent intent = new Intent(getActivity(), RecordingService.class);

        if(start){
            recordBtn.setImageResource(R.drawable.ic_media_stop);

            //Toast.makeText(getContext(), "Started recording", Toast.LENGTH_LONG).show();

            chronometer.setBase(SystemClock.elapsedRealtime());
            chronometer.start();

            getActivity().startService(intent);
            getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

            recordingStatusTxt.setText("Recording now...");
        } else {
            recordBtn.setImageResource(R.drawable.ic_mic_white);
            chronometer.stop();
            chronometer.setBase(SystemClock.elapsedRealtime());
            timeWhenPaused = 0;

            getActivity().stopService(intent);

            recordingStatusTxt.setText("Click the button to start recording");
        }
    }
Emin Guliev :

You cannot keep your fragment alive. System handles it itself. It can kill it anytime it wants to.

The thing is that after clicking START and minimalizing the app and opening it back (putting app in background and back) the count and button is always reverted back to START and 00:00.

The reason behind it actually pretty simple - your fragment basically recreates. And this is an expected behaviour. To continue you should learn Activity and Fragment lifecycle

And as I understand this is a Recorder app so you should get time elapsed from that service. For example, you could Override onResume() method of your Fragment with setting time and button status using information gained from your service.

Good luck!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=356537&siteId=1