2

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!

Joakim
  • 3,224
  • 3
  • 29
  • 53

1 Answers1

0

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.

auracool
  • 172
  • 8