Android auto reset of mediaplayer when activity starts - java

Following on from previous question which it appears that no one can provide a solution, is it possible to reset the mediaplayer automatically, I do not want the user to press buttons to reset, when new activity starts.
Currently it appears that it is not.

if (mediaplayer!= null) {
mediaplayer.reset();
mediaplayer.release();
mediaplayer= null;
}
Apply this code in below activity:
1)this code will apply on destroy of previous activity/pause of this activity.
2)on create method of new activity.(using intent)

Related

Start activity in onstop() method without showing it on the screen in the background until user re-open app

I want to start an activity in the onStop() method without the activity being loaded popping up on the screen which will disturb the user, I want it to just load the activity with the onstop() method in the background so that when user return to the app later on, user could see the activity being loaded, is there any way to make this possible?
I have read so many of the answers about this matter and tried LifeCycleObserver but the intent still pop up, I want it to stay in the background until user open the app again. Thank you in advance
in your_Application class create variable:
Boolean stateOn = false;
in your onStop() set value true,
like
MyApplication.stateOn = true;
in onDestroy() make it false again, (before calling super.onDestroy())
MyApplication.stateOn = false;
and in on onResume() check it, if it was true , start another activity
if (MyApplication.stateOn) {
startActivity({intent});
}
you can crate your_AppCompatActivity class and do above code in there, to appling this config in all activities who extends your_AppCompatActivity

Android - app goes to launcher after calling CAPTURE_IMAGE intent and taking picture (doesn't return to the fragment)

So I have a Fragment that calls the following method which launches the camera:
private void launchCamera() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
And I expect to receive the picture data in this method in my fragment:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (!(resultCode == RESULT_OK)) return;
if (requestCode == REQUEST_IMAGE_CAPTURE || requestCode == REQUEST_GALLERY_IMAGE) {
Uri imageURI = data.getData();
// do something
}
}
However, after I take a picture and confirm it, the app goes to my launcher. After setting breakpoints in the onActivityResult method, the app never even reaches this method before crashing. I've granted made sure to grant all permissions in both the manifest and at runtime.
There are also no outputs to Logcat with this crash, both in the app logs and the device logs. I have also tested on both my device (Moto G5 Plus) & Pixel XL API 26 emulator; both have the same result.
I think you need to call super.onActivityResult().
The fragment is the one making the startActivityForResult() call, but the activity gets the first shot at handling the result so you need to implement super.onActivityResult() to make the fragment handle the result.
the app immediately crashes to my launcher
No, the camera app immediately brings up the launcher. Apparently, the developers of this camera app wrote it to bring up the home screen when the user is done taking the picture. This is a bug, of course, but there is little that you can do about it.
Use ACTION_IMAGE_CAPTURE when you would like a picture but do not mind if you do not get it, due to bugs in some of the hundreds of camera apps that you are integrating with. Otherwise, use a camera library (Fotoapparat, CameraKit-Android, etc.) to take the picture directly in your own app.
Also, and FWIW, ACTION_IMAGE_CAPTURE does not return a Uri, so your onActivityResult() code will not work anyway. Given your particular ACTION_IMAGE_CAPTURE Intent configuration, in onActivityResult(), data.getParcelableExtra("data") will return a Bitmap representing a thumbnail-sized image.
I believe this is yet another manifestation of the Android: Activity getting Destroyed after calling Camera Intent.
The bottom line is, the system restarts your app from scratch and your activity is created but has a chance to restore its state.
So you must implement onRestoreInstanceState(). I am not sure you can guarantee that the fragment will be ready to receive onActiviyResult() timely, so to be in the safe side, I prefer to handle the captured image in activity itself.
So this issue was actually because of an intent flag I had attached to the activity. My activity was started using the NO_HISTORY intent flag, which apparently prevented it from being recreated when returned from a startActivityForResult call.

Android Back Button deleting activity for good

I am making a game, and when a level is complete I have a pop-up activity which tells you your score.
When I press the back button I want this instance of the pop-up activity to be deleted for good.
Currently I get the behaviour;
Finish level, pop-up appears
I press back button to get rid of it
Start a new level, finish it and new pop-up appears
If I press the back button twice, the new pop-up disappears and the old one re-appears.
I want to delete the old pop-up as soon as it is hidden.
I am using the following code in my pop-up activity;
#Override
public void onBackPressed() {
finish();
super.onBackPressed();
}
Any help would be much appreciated.
Thanks!
You can use this code. It clears off the PopupActivity form the Top Activity Stack so that it will not appear again.
#Override
public void onBackPressed() {
Intent removeIntent = new Intent(GameActivity.this, PopUpActivity.class);
removeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(removeIntent);
}

How to destroy an activity

In fact, I am new to Android App Development. In my application, I have a couple of activities and I have provided my users with an exit option menu to be able to leave the application. But there is a problem. When they hit the Exit button, they are able to leave the application but when they enter the application for the second time, the page that they left off the last time will be launched.
Here comes my code:
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case 0 :
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
Toast.makeText(this, "Goodbye Dear", Toast.LENGTH_LONG).show();
break;
Android Activity has two methods onPause and onDestroy where you can do the necessary cleanup.
http://developer.android.com/reference/android/app/Activity.html
Instead of using finish(), use System.exit(0);.
You have to override onPause and/or onDestroy methods inside your activity and delete your view within these methods.
The problem in your code is that Intent.FLAG_ACTIVITY_NEW_TASK doesn't remove your current Task. Read more about it here: Task and Back Stack | Android Developers.
Try using Intent.FLAG_ACTIVITY_CLEAR_TOP. From the documentation we can see that this gives the desired behavior.
If set, and the activity being launched is already running in the
current task, then instead of launching a new instance of that
activity, all of the other activities on top of it will be closed and
this Intent will be delivered to the (now on top) old activity as a
new Intent.

How to stop mediaplayer when home button is pressed

I've tried overriding onPause and everything but the mp3 file continues to play even after the home button is pressed.
I have logged the onPause method and it does seem to be working, however it is not stopping the media player.
I believe that when the home button is pressed the object in which my mediaplayer is held in is set to NULL. Is there anyway to stop the music when the home button is pressed?
User870380 is correct, the home key cannot be overridden, and for good reason. The MediaPlayer is quite erratic and buggy, so seeing your exact code will be very useful. Your code will probably look something like this:
#Override
protected void onPause() {
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
}
You may also need to check to see if it throws an IllegalStateException. The MediaPlayer does that. A lot.
Are you getting any errors in your logcat when you try to stop the MediaPlayer?
Are you running the MediaPlayer in a service?

Categories