Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8586081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:13:26+00:00 2026-06-11T22:13:26+00:00

The documented Android fragment samples ( FragmentBasics , NewsReader violate core principles of object

  • 0

The documented Android fragment samples (FragmentBasics, NewsReader violate core principles of object oriented design. There are redundant conditionals to establish the currently showing view type, tightly coupling the FragmentActivity with the view types and fragment types. MainActivity is coupled to every class (including the XML):

enter image description here

One might be able to make the excuse for the authors that they are trying to “keep it simple,” or that readers might be confused. If this is the intent–I think readers can handle it; instead, it’s teaching bad programming practices that will result in less maintainable applications.

How can Fragments be implemented such that the views and fragments aren’t tightly coupled to the FragmentActivity?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:13:27+00:00Added an answer on June 11, 2026 at 10:13 pm

    Starting with the simpler FragmentBasics demo, gut MainActivity as follows:

    public class MainActivity extends FragmentActivity
          implements OnHeadlineSelectedListener {
       AbstractNewsView abstractNewsView;
    
       @Override public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.news_articles);
          abstractNewsView = new AbstractNewsViewProvider(this).get();
          abstractNewsView.onCreate(savedInstanceState);
       }
    
       @Override public void onArticleSelected(int position) {
          abstractNewsView.onArticleSelected(position);
       }
    }
    

    Now its dependency diagram looks like this. You can now add all the news_articles view variants you’d like for different device types and MainActivity doesn’t need to change.

    enter image description here

    Add a new class AbstractNewsViewProvider whose sole responsibility is to determine which type of view (single or double pane) is being used for the given device. If you’re using Guice or RoboGuice for dependency injection, this would instead be a Provider method in your binding module.

    public class AbstractNewsViewProvider {
       private final FragmentActivity fragmentActivity;
    
       public AbstractNewsViewProvider(FragmentActivity activity) {
          this.fragmentActivity = activity;
       }
    
       public AbstractNewsView get() {
          if (fragmentActivity.findViewById(R.id.fragment_container) != null) {
               return new SinglePaneNewsView(fragmentActivity);
            } else {
               return new DoublePaneNewsView(fragmentActivity);
            }
       }
    }
    

    Add two new classes SinglePaneNewsView and DoublePaneNewsView that implement AbstractNewsView as shown below. These two classes are responsible for the setup of the initial fragment(s) within the respective view type. They are also responsible for handling transitions between fragments, if any.

    interface AbstractNewsView extends OnHeadlineSelectedListener {
       public void onCreate(Bundle savedInstanceState);
       @Override public void onArticleSelected(int position);
    }
    
    
    public class SinglePaneNewsView implements AbstractNewsView {
    
       private final FragmentActivity fragmentActivity;
    
       public SinglePaneNewsView(FragmentActivity fragmentActivity) {
          this.fragmentActivity = fragmentActivity;
       }
    
       @Override public void onCreate(Bundle savedInstanceState) {
          // However, if we're being restored from a previous state,
          // then we don't need to do anything and should return or else
          // we could end up with overlapping fragments.
          if (savedInstanceState != null) {
             return;
          }
    
          // Create an instance of ExampleFragment
          HeadlinesFragment firstFragment = new HeadlinesFragment();
    
          // In case this activity was started with special instructions from an
          // Intent,
          // pass the Intent's extras to the fragment as arguments
          firstFragment.setArguments(fragmentActivity.getIntent().getExtras());
    
          // Add the fragment to the 'fragment_container' FrameLayout
          fragmentActivity.getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, firstFragment).commit();
       }
    
       @Override public void onArticleSelected(int position) {
          // If the frag is not available, we're in the one-pane layout and must
          // swap frags...
    
          // Create fragment and give it an argument for the selected article
          ArticleFragment newFragment = new ArticleFragment();
          Bundle args = new Bundle();
          args.putInt(ArticleFragment.ARG_POSITION, position);
          newFragment.setArguments(args);
          FragmentTransaction transaction =
                fragmentActivity.getSupportFragmentManager().beginTransaction();
    
          // Replace whatever is in the fragment_container view with this fragment
          // Add the transaction to the back stack so the user can navigate back
          transaction.replace(R.id.fragment_container, newFragment);
          transaction.addToBackStack(null);
    
          // Commit the transaction
          transaction.commit();
       }
    
    
    public class DoublePaneNewsView implements AbstractNewsView {
    
       private final FragmentActivity fragmentActivity;
    
       public DoublePaneNewsView(FragmentActivity fragmentActivity) {
          this.fragmentActivity = fragmentActivity;
       }
    
       @Override public void onCreate(Bundle savedInstanceState) {
       }
    
       @Override public void onArticleSelected(int position) {
          ((ArticleFragment) fragmentActivity.getSupportFragmentManager()
             .findFragmentById(R.id.article_fragment)).updateArticleView(position);
       }
    
    }
    

    You can find the complete source on Google code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any document visualization toolkit available for android?
Are there documented pylons or turbogears2 widgets for generating SVG markup as components and
Is there any standard/documented ways to implement two-finger tap event in WP7? If not,
Is there a documented list of desktop browsers and versions which support Ember.js apps?
Have any well-documented or open source projects targeted iPhone , Blackberry , and Android
What documentation is there on the extras will be provided with Android intents? Update
Hi there Im new to mobile application development. I had developed Android apps using
Android documents starting the email intent for sending emails with Intent.ACTION_SEND. Is there an
Is there any well tested and documented wrapper around InApp Billing API? What Google
It's well documented that Android's camera preview data is returned back in NV21 (YUV

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.