Issue
I'm new to android studio, so sorry if this a very noobish question, but I can't find the answer anywhere online. Any help is much appreciated!
I've built a small app that has two screens: a main screen, a screen two. The main screen is just for navigation to screen two. From the other screen, you can play/pause/stop a specific song, or go back to the main screen.
The problem I'm encountering is when I navigate to screen two, when I press the "back" button to go back to the main screen, I cannot figure out how to add the "stop" method to this button. I've followed some tutorials online to set up the mediacontroller, which works perfectly, but here I'm stuck.
So, all I need is to add the onStop() method to the back button, so that when the user navigates away from the mediaplayer screen, the playing stops.
This is my code:
Main activity (Play, pause, stop, stopPlayer, onStop methods)
public void play(View v){
if(player == null){
player = MediaPlayer.create(this, R.raw.testmeditatie);
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stopPlayer();
}
});
}
player.start();
}
public void pause(View v){
if(player!= null){
player.pause();
}
}
public void stop(View v){
stopPlayer();
}
private void stopPlayer(){
if(player!=null){
player.release();
player = null;
Toast.makeText(this, "MediaPlayer released", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onStop() {
super.onStop();
stopPlayer();
}
First fragment code:
public class FirstFragment extends Fragment {
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_first, container, false);
}
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
super.onStop();
view.findViewById(R.id.button_first).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
NavHostFragment.findNavController(FirstFragment.this)
.navigate(R.id.action_FirstFragment_to_SecondFragment);
}
});
}
Second fragment:
public class SecondFragment extends Fragment {
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_second, container, false);
}
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.button_second).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
NavHostFragment.findNavController(SecondFragment.this)
.navigate(R.id.action_SecondFragment_to_FirstFragment);
}
});
}
Buttons in second fragment.xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
android:onClick="play"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:onClick="pause"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"
android:onClick="stop"/>
<Button
android:id="@+id/button_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/previous"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
Solution
Just override onBackPressed()
and call stopPlayer() to release the media resources.
@Override
public void onBackPressed() {
stopPlayer();
super.onBackPressed();
}
UPDATE:
As you are using navigation components, so the onBackPressed()
won't be called from the second fragment unless you call it explicitly.
To do that you can create a listener interface that is implemented by the second fragment:
public interface IOnBackPressed {
void onBackPressed();
}
Then implement it in the Second Fragment:
public class SecondFragment extends Fragment implements IOnBackPressed {
@Override
public void onBackPressed() {
stopPlayer();
}
// rest of your code
}
Then you need to explicitly call onBackPressed()
of the second fragment whenever the back button is pressed; but this is triggered in onBackPressed()
in the MainActivity
as following:
public class MainActivity extends AppCompatActivity {
@Override
public void onBackPressed() {
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().getPrimaryNavigationFragment();
if (navHostFragment != null) {
// Getting the current fragment in the navGraph
Fragment currentFragment = navHostFragment.getChildFragmentManager()
.getFragments().get(0);
/* Check if the Current fragment implements IOnBackPressed so it
needs to consume the back before the MainActivity */
if (currentFragment instanceof IOnBackPressed)
((IOnBackPressed) currentFragment).onBackPressed();
}
super.onBackPressed();
}
// rest of your code
}
Answered By - Zain
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.