I have 3 activities let's say MainActivity, Activity 2, and Activity 3.
In the MainActivity I have a button (start/stop) when I click on this button an animation start but when I go to activity 2 or activity 3 and I comeback to MainActivity the animation stopped by itself.
But me I want that the animation of the MainActivity never stop until I click the button stop.
I tried this code in AndroidManifest.xml but it doesn't work.
android:launchMode="singleInstance"
If you go back from MainActivity then your Activity will be Destroyed if you do not fire an Intent in onbackPressed() regardless of launchMode.
launchMode are instruction for how the activity should be launched. On stack pop it will destroy anyway. Read launchModes .
Solution:- Solution to your problem SharedPreference. Save the current state of animation and restart it with previous state when Activity relaunched.
Related
How do I open another activity with the same button I clicked earlier? For example, I click button A and goes to Activity 1 and I click home and click button A again and must go to Activity 2.
I tried putting id on button in xml and call different activity on its java.
Imagine if the button is from HomeActivity,let the launcher activity pass an ExtraKey to the HomeActivity and again another ExtraKey at the OnBackPressed of the Activity you are navigating to after clicking the button.Set the onClickListner according to the String passed by those activities.
I am navigating from Activity A to Activity B without finishing Activity A (because i want to go back on it and have some variables values).
In Activity B I launch camera and save captured image but the problem is, after capture camera the Activity A is re-create and resumed, causing re initialization of my variables.
How to stop it?
Note: The problem occurs only in Nougat Version.
The thing you want to know here is about the complete activity life cycle. Basically to summerize them they are
OnCreate, - called when activity is created
OnStart, - called when activity starts
OnREsume, - when activity gets back
OnPause, - when activity is overlapped
OnStop, - when activity is closes
and onDestroy - when finish() is called.
Though you have not mentioned how you started an activity and got back to the same activity, the correct way to get back to previous activity is by calling
finish()
on camera activity.
I guess you have got back to activity A by using
Intent i= new Intent(this, ActvityA.class)
startActivity(i)
which is the the correct way to do.
Just call the finish() when you want to get back to Activity A from B (Here B is in top of A).
Additional
If you want to pass data from activity B to A, just put something called as Intent Extra or Bundle
Activity A is create again and resume, so my variables are initialize again. How to stop it.
Yes that is normal. First rule with Android programming is that your activity can be killed at any time. So to not loose your variables you have to save them at the right moment.
You would do that to override onSaveInstanceState() putting your variables in the bundle.
Then you can retrieve them in onCreate() from the function parameter.
I read along the documentation in this page. And obtaining clear picture of one activity starts in android. Just like this screenshot below:
Here is my case.
Activity -A called Intent to start Activity B-.
And then from Activity -B the user click Back button.
So then it returned to Activity -A.
my question is .... since now we're in that Activity -A.
Does onPaused() method called back or onResumed() method
that is called now?
When ActivityB takes the foreground, Activity A is put into the stopped state (onPause and onStop will be called). When ActivityA comes back, it will get onStart, followed by onResume.
If I have Activity A and Launch Activity B from it, then press home I go back to home screen. If I launch the activity again from the recent apps, it goes back to B as it should, but when back is pressed the Activity send me back to the Home screen not Activity A. Activity A and be are alive for the entire time. How can I make the back button function as expected? Thanks.
Don't call finish() on Activity A if you want the back button to go from Activity B to Activity A...
Pressing the Home button empties the Activity stack.
You can capture the Back button and make it do what you want, but you would have to implement your own stack of previous activities.
I have an Activity which displays an alert dialog priot to the content.
Also a search activity is called before this activity.
Now when i press back on this activity i am lead to a black screen (which i assume is eighter because of the dialog [unlikely] or from the search activity). Only when i press BACK twice i get to the activity i was before.
How can i get rid of this?
Thank you.
This phenomenon was caused by the search activity. to avoid this i simply added android:noHistory="true" for the Search Activity in the Manifest.