I need to show one of my view when onPause is called from an Android activity.
Would it be a good practice to do? Is there any issues if I show it from the onPause?
This view will be shown as a background when my activity is partially visible.
Basically it is not good practice to do so.
The Activity is still slightly visible (link), but the user may not even notice if you change anything.
Also everything in onPause should be fast.
The documentation says:
When activity B is launched in front of activity A, this callback will be invoked on A. B will not be created until A's onPause() returns, so be sure to not do anything lengthy here.
So it will slow down the start of the next Activity, also the Activity animation might be a little laggy if you are updating the UI in onPause()
You may use onResume() to show your view.
Related
I need to detect whenever the activity that houses a View I'm creating has been pushed to background (i.e., no longer visible to the users).
I've tried overriding onDetachedFromWindow() but it didn't brought the result I was hoping to see despite what its official docs say:
protected void onDetachedFromWindow()
This is called when the view is detached from a window. At this point it no longer has a surface for drawing.
I for one assumed that by going into the background, the View will no longer has a surface for drawing (as it is now covered by another View). But this turned out not the case.
Is there any other way to achieve this?
After digging out the docs for View, I've finally found my solution.
Being pushed to the background doesn't instantly mean that a view will be detached from its window. (It very well might, but just not the second you/your user trigger an activity change.)
One method that would always be called the moment your view is being overlaid by another view (like when you change activities) is onWindowFocusChanged().
You could rely on this option for things like unsubscribing Rx Observables (like what I did) and the likes.
You need to override the onStop() method on the activity.
I recently started refactoring my Android application by replacing "all" activities with fragments. In the state it is right now, it behaves a lot worse than before...
My problems are in the area of "up navigation", backstack behaviour and general "repainting" of fragments when they are brought to front.
So I have created a logical hierarcy of fragments in my ui and when the user is in the main menu, the "up" button shouldn't be displayed. When the user is in any of the other fragments the up button should take the user back to the main menu.
When I started implementing this, I just put a
activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
In my fragment's onResume method. This works as expected until the main menu is brought to front from the back stack (or the home navigation). Then the onResume() method isn't called.
Wanting to "repaint" the fragment when the user have been somewhere else in the app seems like the kind of thing that a large share of developers want to do. I have read some solutions to this that basically include listening to backstack changes and then calling onResume() for the fragment that is about to come back. This solution feels like a ugly hack that you shouldn't have in a real application. So how do developers of large applications handle this? What is the best practice? Or have I missed some principle saying how to not code myself into this corner at all?
I must say that I think the Android dev page for fragments is almost lying about the lifecycle:
"Managing the lifecycle of a fragment is a lot like managing the lifecycle of an activity"
"Resumed: The fragment is visible in the running activity."
This information implies that onResume() should be called when the fragment goes from invisible to visible.
Also, my solution to providing up navigation is obviously not correct, any tips on how to get proper behaviour?
Thanks for you help!
OK it was very unclear the actual problem so i will try and break it down.
Problem
Home Screen's onResume Method is not being called.
In Looking at the activity lifecycle and fragment lifecycle:
It highlights "The fragments onResume() or onPause() will be called only when the Activities onResume() or onPause() is called."
Go here: 2nd paragraph
This should give you a big idea as to why your onResume() method in the home Screen isnt being called. I wouldnt implement this myself but for sake of giving a solution i would clear the backstack (for memory sake) and instantiate a new homeScreen Activity when the user tries to get to the main screen/homescreen or my own solution would be to rewrite part of your application again you do not need to use fragments for the sake of it.
Programming practices
Be careful when using many fragments use of a fragment unless your highly skilled and knowledgeable of the android API should be used for showing mutiple views in one screen. Use cases include larger screens (tables,extra information etc.
Many white screen issues have been reported from bombarding an app with fragments and popping to the back stack so be weary.
I am starting off some animations as soon as my Activity is created.
However, by the time the Activity is fully visible the animation has already half completed.
I originally had it in onCreate but have now moved in into onWindowFocusChanged and only start the activity once I know onResume has also been called (I'm setting a boolean in onResume)
Is there anyway of knowing when an Activity is fully visible? Or am I going to have to set a 1 second delay? (This seems extremely hacky and potentially still won't work on slower phones/tablets)
If your intention is to display your animations to the user, one way you could guarantee they see the entire animation is by triggering it with an onClickListener() for the whole screen, and just wait for them to touch it?
for layout animations you can use LayoutAnimation, here is the link here
As of API level 21, you can implement the Activity#onEnterAnimationComplete() callback. Unfortunately, it seems like there's no AppCompat equivalent at this point.
I have a back button that will work exactly like that.
I know using back button is not a good android practice since android device have one. But my situation badly need it.
I cannot finish my activity and its tough to Keep track of the activity stack.
Since you know that overriding BACK button function is not a good practice, just follow it. Tracking activity stack and promising the BACK behavior can be solved by properly setting the launch mode of each Activity. (please see: Launch Mode in Android Official Site)
If you insist to do something like pressing the BACK button, it's super.onBackPressed().
its better for you to user super.onBackPressed()
Refer Go back to previous activity
I wonder why you wouldn't want to finish your activity. Could you move whatever it is in your activity you don't want to destroy into another object?
BTW: the activity will also be destroyed and re created if the screen is rotated.
My app intiates an activity. On the click of a button, the app opens up the browser with a webpage. When I hit the back button, it comes back to my initial activity screen, but does not resume or restart the activity.
When I put all the layout code and activity code in onResume instead of onCreate, the activity gets restarted.
My question is whether this is the right way to go about it? Can I use onResume to draw my layout and initiate the activity, or is this poor design? When the browser fires up, does the initial activity forget its layout?
Please let me know what you suggest.
Thanks
Chris
Mostly you should read about the Activity Life Cycle.
It is fine to initialize in onResume as long as you only do it once. Either have a dedicated hasInitialized member or check some other value that will have equivalent meaning, and do not initialize again if it is set.