artzy
artzy

Reputation: 49

Android: Keep fragment running in the background

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");
        }
    }

Upvotes: 2

Views: 1417

Answers (2)

Emin Guliev
Emin Guliev

Reputation: 188

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!

Upvotes: 1

Dan Eisenhut
Dan Eisenhut

Reputation: 194

If you minimize the app while running, the android operating system can terminate your app and restart it when the user returns to it. You cannot guarantee it will remain in memory. If there is any data that needs to be maintained, you are responsible for saving it in the state handlers.

If you wish to have some process continue in the background, you'll need to start an Android Service and your fragment will need to communicate with it. The Fragment is just the UI piece of the app.

Upvotes: 1

Related Questions