Skip to content

Instantly share code, notes, and snippets.

@alokomkar
Created November 2, 2022 08:01
Show Gist options
  • Select an option

  • Save alokomkar/b80f17c89e3dd4b4f43cd3fe24e80694 to your computer and use it in GitHub Desktop.

Select an option

Save alokomkar/b80f17c89e3dd4b4f43cd3fe24e80694 to your computer and use it in GitHub Desktop.
Step 5 : HomeFragment with start and stop service
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.AppCompatButton
import androidx.fragment.app.Fragment
import com.alokomkar.javacollections.R
import com.alokomkar.service.AudioLoopService
class HomeFragment: Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_home, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.apply {
findViewById<AppCompatButton>(R.id.startButton).setOnClickListener {
context.startService(Intent(
context,
AudioLoopService::class.java
))
}
findViewById<AppCompatButton>(R.id.stopButton).setOnClickListener {
context.stopService(Intent(
context,
AudioLoopService::class.java
))
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment