code to turn on transition setting on phone within app - java

I have this app where when it navigates to another activity it does a transition animation. Now I have already got that working properly but, to get it to work i have to turn the animation setting on my phone on. I have been searching on Google and I can"t seem to find solution to my problem. So my problem is this,is it possible to turn this setting on from within the app, with a code, instead of having to manually do it?
Here is the code but as i have said above this works perfectly and I'm using a Telstra next G Huawei phone, just want to no if i can code it so i can turn the animation setting on if i decide to put it on a different phone.
//Button to restart from beginning:
//Declaring the button for OnClickListener and EditText to send data:
private void Restart() {
Button Next = (Button)findViewById(R.id.restart3);
Next.setOnClickListener(new View.OnClickListener() {
//The action the button takes when clicked:
#Override
public void onClick(View v) {
//Goes back to the start of app:
Intent i = new Intent(Height.this, Page_1.class);
//clears the stack of all Activities that were open before this one so that they don't stack up:
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
//The transition animation when going onto next page:
overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
// to end the current activity:
finish();
}
});
}

Related

Activity is destroyed when I start another activity without finishing the first activity

I'm struggling with this issue and I want to know if there is any option to know why an activity is being closed, I have checked if it was a low memory issue with Runtime.getRuntime().freeMemory(); but everything looks fine.
UPDATE
Here is how I call the activities and when suddenly all the background activities are closed. In all the way from MainActivity to SketchActivity I'm not calling the finish(); method but when I start the SketchActivity from BoulderProfileActivity, all the activies are destroyed, except SketchActivity :
MainActivity:
private void SendUserToFindBoulderActivity(){
Intent boulderFindIntent = new Intent(MainActivity.this, FindBouldersActivity.class);
startActivity(boulderFindIntent);
}
FindBouldersActivity:
holder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent boulderProfileIntent = new
Intent(FindBouldersActivity.this,BoulderProfileActivity.class);
boulderProfileIntent.putExtra("BoulderKey", BoulderKey);
startActivity(boulderProfileIntent);
}
});
BoulderProfileActivity:
public void SendUserToSketchUpActivity(){
Intent sketchIntent = new Intent(BoulderProfileActivity.this, SketchActivity.class);
sketchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
if(sustituir==false) {
sketchIntent.putExtra("BoulderKey", BoulderKey);
sketchIntent.putExtra("Uri", ImageUri);
sketchIntent.putExtra("sustituir", sustituir);
}else{
sketchIntent.putExtra("BoulderKeyForSwitchFirstImage", BoulderKey);
sketchIntent.putExtra("Uri", ImageUri);
sketchIntent.putExtra("photoKey", photoKey);
sketchIntent.putExtra("sustituir", sustituir);
}
startActivity(sketchIntent);
}
When you start SketchActivity you are using flags
sketchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
and these flags close all previous activities and start the activity in a new task.
so to keep old previous activities please remove this line.
if you don't know what is a task, the task is the container for activities, so when you start new task this means that the activity is the first activity if you want to read more about tasks follow this link :
https://developer.android.com/guide/components/activities/tasks-and-back-stack
and you can find more information about intent flags here
https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_CLEAR_TASK
Any chance you enabled developer mode (System -> Developer options), and it is force-closing the Activity?
There's an option (Developer Options, under 'apps', I'm using a Pixel 3) called 'Don't keep Activities' which should be disabled.

How to capture picture from 2 cameras at the same time?

I know this question has been asked many times before and there has been no solution. But with the new Camera2 API, I think so this is possible to do and I did not find any sample code/solution.
I am trying to capture the images from two cameras on Huawei P20. It has a total of 4 cameras (3 rear, 1 front). I am using one normal camera and one wide angle one. I am able to capture pictures from two cameras using the activity switch i.e. have a button to switch to another activity and then open the camera using a different ID there and then capture the picture and then use the button and shift to main activity and so on.
I have two activities one for wide angle and one for the normal camera. I have two buttons for each activity. One for capturing the picture and one for switching between activities.
So, this is how it looks like on MainActivitiy.java.
textureView = findViewById(R.id.texture);
clickBtn = findViewById(R.id.captureBtn);
nextBtn = findViewById(R.id.depthBtn);
clickBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
takePicture();
}
});
nextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
refreshCamera();
Intent i = new Intent(MainActivity.this, WideCamera.class);
startActivity(i);
}
});
The refreshCamera() is for releasing the camera resource. This works fine. However, I am unable to show the preview side-by-side. Is there any way I can show the preview side-by-side and capture images without having to switch activities? Is it even possible?
Thanks.

how to close main activity after closing another activity in android?

I am developing an android application. So in there I have button called "Aboutus". when I click that one it starts another activity and show corresponding view.
Here is the code for button Aboutus click event.
aboutus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this,AboutUs.class);
startActivity(intent);
}
});
}
And then in that aboutus activity I have back button.when we press that back button it will go to the main activity again.
back button key event goes like this..
back.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(AboutUs.this,MainActivity.class);
intent.putExtra("aboutus", true);
startActivity(intent);
finish();
System.exit(0);
}
});
here i put some intent extras for some other purposes.In this case they do not matter. So and then again in mainactivity i have exit button. which should kill whole app.
and my exit button code goes like this.
exit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
finish();
System.exit(0);
}
});
Exit button works perfectly (it ends the app) except for one scenario. if we click on aboutus and go to that activity and then press back button on that aboutus activity and then again come back to main activity and finally when i want to exit it won't kill whole app, instead it again goes to the about activity.
in conclusion,
MainActivity---> click aboutus button(no problem in here.this will start aboutus activity)
Aboutus----->click back button(this also works fine. go back to main activity)
MainActivity--> Exit button (not working .it goes to aboutus activity again)
So how to fix this problem?
i dont see why you have to start MainActivity.java ...the user can just hit the back button as long as its on the activity stack. Also The system.exit(0) is bad. Just let the app finish gracefully with finish().
if you want to pass something to the activity when aboutUS is done, you can look up onNewIntent() method.
use intent flag ACTIVITY_CLEAR_TOP to clear all activities on top of it:
Intent intent = new Intent(AboutUs.this,MainActivity.class);
intent.putExtra("aboutus", true);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(intent);
FLAG_ACTIVITY_CLEAR_TOP From the doc:
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.

Android backround music across multiple activities; How to catch Home button presses

I want to play an audio file in the background of my app. Easy enough. I want the music to persist and NOT stop or pause while switching between activities in my app. Also fairly easy and accomplished simply by doing this in the onCreate method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
mp = MediaPlayer.create(MainActivity.this, R.raw.lostmexicancity);
mp.setLooping(true);
mp.start();
}
The problem? Getting the music to stop when I press the HOME button.
Killing the sound when the user presses the back button seems easy. Here's what I have for that and works great:
public void onPause() {
if(this.isFinishing()){ //BACK was pressed from this activity
mp.stop();
}
super.onPause(); }
Not complicated, but this does not catch presses of the HOME button. If the Home button is pressed, the music keeps playing even while the user no longer sees my app.
I have seen answers that involve setting permission in the manifest to Get Tasks which I shouldn't have to do and appears dangerous to users. Besides that, the solution didn't even work. I've seen solutions that involve using a service, but none of those work either because the home button STILL plays the music just like before because there doesn't seem to be a way to catch it and it doesn't 'finish' the app (not to mention that every time someone suggest using a service for this task multiple people come in and state that this is not a proper use for services)
It seems the only way to kill the music when the Home button is pressed is to use a non-conditional stop() within onPause, but that's no good because that's called when I swap activities with intents, causing the music to end between activities which is no good.
I have trouble imagining that such a common function like background music is this hard, but I've seen post after post with the same issue as me and no proper answers other than ones that would kill the music between activities within the app.
How do all the other apps on the Google play store accomplish this and yet there appears to be no clear answer online? I could just stop and start the music with each onPause(), but that would cause unprofessional gaps in audio not to mention it would start the background audio from the beginning over and over again which is unacceptable.
I'm a bit new to Android Programming (few months) and today, I faced the same problem you did (maybe you still do?)
I made it work as the following :
Lets say I have MainActivity, and in MainActivity I have Btn2 which leads to SecondActivity, and Btn3 which leads to ThirdActivity.
I declared at the beginning of MainActivity :
public static boolean shouldPlay = false;
I then implemented my onStop() method :
public void onStop() {
super.onStop();
if (!shouldPlay) { // it won't pause music if shouldPlay is true
player.pause();
player = null;
}
}
If the boolean shouldPlay is set to true, then my onStop() won't be called entirely and my music won't turn off. I then have to decide when I set it to true. When I switch from MainActivity to SecondActivity, I do it through an Intent and that's when I'll set shouldPlay to true :
Button Btn2 = (Button) findViewById(R.id.Btn2);
Btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
shouldPlay = true;
startActivity(intent);
}
});
And the same is done for Btn3.
Now, the last thing we want to be looking for is that if I was to go back to MainActivity after visiting SecondActivity or ThirdActivity, shouldPlay would then have been set to true. The first thing I tried was to set it to false as soon as Second and ThirdActivity are called (in their onCreate()) but it want to work, maybe because the onStop() from Main and onCreate() from others are called simultaneously (frankly I don't really get life cycle for now).
What worked is simply to set shouldPlay to false every time we launch onCreate() of Main :
shouldPlay = false;
This works properly for me.
Let me know if it does for you,
Cheers,
bRo.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_HOME){
Log.d("Jorgesys", "Home button pressed!!!");
}
return super.onKeyDown(keyCode, event);
}
but hey! This no longer works as of 4.0 + , Read this: Capture Home Key Event
Try with Back button:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK ) {
Log.d("Jorgesys", "Back button pressed!!!");
}
return super.onKeyDown(keyCode, event);
}
To stop the media player, you are using the method:
isFinishing(): if the activity is finishing, returns true; else
returns false, but you are only pausing the activity not finishing the
activity.
public void onPause() {
if(this.isFinishing()){ //INCORRECT, Here you are pausing you activity not finishing.
mp.stop();
}
super.onPause();
}
so change to:
public void onPause() {
if(mp.isPlaying())
{
mp.stop();
}
super.onPause();
}
When your activity is on pause, evaluates if your MediaPlayer is playing, if this is true then stops the audio.

How to get fast output in which calculation part has to be done by clicking button in Android?

My Problem is while clicking the button it goes to next Activity during that time it takes nearly 10-12 secs for loading the values in next activity.Please help me from this issue and suggestions will be appreciable.
Here is the code:
Button mButton = (Button)findViewById(R.id.pay);
mButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
Intent i=new Intent(MainActivity.this,second.class);
i.putExtra("rate", rate);
startActivity(i);
}
});
so during clicking the button the calculation part is also working. Help me out from this.
No sure what you are trying to do....
In your onClick() method, you are starting new activity so obviously it will go to new Activity. Why don't you do the calculations in onClick method itself and update the values displayed?

Categories