In a recent interview with android developer guru JakeWharton, he offered a controversial and unexplained piece of advice: an App only needs one Activity, and you can use Fragments, just don’t use Fragments to rollback.

In response to this comment, a question about the reasoning behind JakeWharton’s proposal quickly went viral on Reddit’s Android development channel.

Many Android developers have offered their own insights. One of the most praised was even praised by JakeWharton himself.

Let’s take a look at what this answer says, what insight you have into the love-hate relationship between activities and fragments, and why you should be a JakeWharton favorite.


Because an Activity is a program entry. You can think of it as a main function of your app. From a user’s standpoint, there are usually several ways you can enter an app:

  • Launcher desktop program (main function entry);

  • Notification bar from the entry of the parameterized main function, and navigate to the specified location of the app;

  • If you’re making a camera app, you’ll need intents to handle image requests;

  • If you’re working on a social product, you’ll need to handle the intents of share requests.

It’s kind of like these scenarios.

However, if you really don’t need to share and intents from your app, and the only entry point to your application is the desktop launcher, don’t create a new entry point for every page. There’s no point in doing that. Why doesn’t it make sense? Because in this scenario, the process dies and the launcher launches any Activity page in your application.

Fragments are view controllers that handle life cycle events, and they’re great. However, the rollback of Fragments is garbage; The fallback stack change listener is always called abnormally (1 transaction 3 calls?). , without telling you what to call, and without knowing which fragments are available when resuming a transaction.

You can tag the transaction and pop the action off the stack, but just having a main -> Events -> Details(ID =123) is pretty tedious.

Similarly, once you put a Fragment in a rollback stack, I personally don’t know what its life cycle starts to do. I once had a fragment in the background called onCreateView() four times and I didn’t even know what was going on. Fragments that are not in the fallback stack are expected. Their animation support is a bit weird, but at least they work.

So if you want to know which Fragments you can work with and which views you are displaying that are in your own navigation state control, you should handle the navigation yourself. Abstracting “application logic” into a Presenter sounds great, but are you losing touch with what’s really going on in the application view layer?

But what are the advantages of a single activity?

Simpler lifecycle handling (for example, you only need to handle the onStop method when the app goes into the background), less error space, and more control. Similarly, you can move navigational state outside the view layer to the Domain layer, or at least to presenter. You don’t need too much view.Navigatetodetail (songId) or something like that, All you need to do is use backstack.goto (songkey.create (songId)) in your Presenter or ViewModel or whatever trendy usage. With a library in place, onResume will queue those navigation calls automatically when you get to it without causing the fragment transaction to crash, which is great.

I have a taste for animation with commitNow(), although the cases Google gives are also using commitAllowingStateLoss(). In my opinion, the visible benefit of a single activity is the ability to share views between pages instead of repeating views across 18 layout files using


That was the answer that struck the heart of JakeWharton. That being said, there are a lot of unanswered questions about system fragments, or potholes you might not expect. If forced to choose between fragments for multiple activities and fragments for a single activity, I think not only me, but many people would not hesitate to choose the former.

For more discussion, see:

www.reddit.com/r/androidde…

Note: I’ve written a summary article on the use of Fragments before, so if you’re interested, check out my blog.

About me: Yifeng, blog address: Yifeng. Studio /, Sina Weibo: IT Yifeng

Scan the QR code on wechat, welcome to follow my personal public account: Android Notexia

Not only share my original technical articles, but also the programmer’s workplace reverie

Easter egg: public number reply keyword “interview information”, obtain BAT interview bull for you to prepare a full set of interview information!